Однако при запуске службы Windows, выполняющей ту же операцию, время отправки истекло.
Служба работает в Windows Server 2025 Datacenter и Python 3.13.1. Вот пример сценария, демонстрирующий проблему (dispatch-test.py):
Код: Выделить всё
import win32serviceutil
import win32service
import win32event
import win32com
import win32com.client
import servicemanager
import socket
import time
import logging
import sys
def configure_logging():
logging.basicConfig(
filename = r'C:\Users\Administrator\Desktop\dispatch-test.log',
level = logging.DEBUG,
format = '[helloworld-service] %(levelname)-7.7s %(message)s'
)
def dispatch():
try:
win32com.client.Dispatch('EbsOpen.Application')
logging.info('Successful')
except Exception as e:
logging.error('Failed', exc_info=True)
class HelloWorldSvc(win32serviceutil.ServiceFramework):
_svc_name_ = "Dispatch-Test"
_svc_display_name_ = "Dispatch Test"
def __init__(self, args):
super().__init__(args)
configure_logging()
def SvcStop(self):
logging.info('Stopping service ...')
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, '')
)
logging.info('Running service ...')
dispatch()
if __name__ == '__main__':
if len(sys.argv) == 1:
configure_logging()
dispatch()
else:
win32serviceutil.HandleCommandLine(HelloWorldSvc)
Код: Выделить всё
[helloworld-service] INFO Successful
Код: Выделить всё
[helloworld-service] INFO Running service ...
[helloworld-service] ERROR Failed
Traceback (most recent call last):
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 80, in _GetGoodDispatch
IDispatch = pythoncom.connect(IDispatch)
pywintypes.com_error: (-2147221021, 'Operation unavailable', None, None)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\Administrator\DEsktop\helloworld-service.py", line 23, in dispatch
win32com.client.Dispatch('EbsOpen.Application')
~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\__init__.py", line 114, in Dispatch
dispatch, userName = dynamic._GetGoodDispatchAndUserName(dispatch, userName, clsctx)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 100, in _GetGoodDispatchAndUserName
return (_GetGoodDispatch(IDispatch, clsctx), userName)
~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^
File "C:\Users\Administrator\AppData\Local\Programs\Python\Python313\Lib\site-packages\win32com\client\dynamic.py", line 82, in _GetGoodDispatch
IDispatch = pythoncom.CoCreateInstance(
IDispatch, None, clsctx, pythoncom.IID_IDispatch
)
pywintypes.com_error: (-2146959355, 'Server execution failed', None, None)
Системные журналы показывают это примерно через две минуты после запуска службы. :
Код: Выделить всё
Log Name: System
Source: Microsoft-Windows-DistributedCOM
Date: 1/15/2025 8:04:40 PM
Event ID: 10010
Task Category: None
Level: Error
Keywords: Classic
User: SYSTEM
Computer: EC2AMAZ-M0C7SQ4
Description:
The server {F1A4BB7E-1E45-4040-ACBC-4E2600010118} did not register with DCOM within the required timeout.
Event Xml:
10010
0
2
0
0
0x8080000000000000
14925
System
EC2AMAZ-M0C7SQ4
{F1A4BB7E-1E45-4040-ACBC-4E2600010118}
Код: Выделить всё
from win32service import OpenSCManager, SC_MANAGER_ALL_ACCESS, OpenService, SERVICE_ALL_ACCESS, SERVICE_NO_CHANGE, ChangeServiceConfig
sc_manager = OpenSCManager(None, None, SC_MANAGER_ALL_ACCESS)
my_service = OpenService(sc_manager, 'Dispatch-Test', SERVICE_ALL_ACCESS)
ChangeServiceConfig(my_service, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, None, None, False, None, r'.\Administrator', '', None)
Статическая отправка/ раннее связывание также терпит неудачу.
Подробнее здесь: https://stackoverflow.com/questions/793 ... as-service
Мобильная версия