我试图与pywin32一起尝试将CherryPy XML-RPC-Server作为Windows服务启动。这适用于Windows 7,但不适用于Windows XP。 该脚本在调试模式下运行正常(即不作为服务),但作为服务,我得到以下日志输出:
2015-08-14 16:55:18,187 INFO BuildServerService Start Service... 2015-08-14 16:55:18,187 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Bus STARTING 2015-08-14 16:55:18,187 ERROR cherrypy.error [14/Aug/2015:16:55:18] ENGINE Set handler for console events. 2015-08-14 16:55:18,187 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Started monitor thread '_TimeoutMonitor'. 2015-08-14 16:55:18,218 ERROR cherrypy.error [14/Aug/2015:16:55:18] ENGINE Error in HTTP server: shutting down Traceback (most recent call last): File "C:\Programme\WinPython-32bit-3.3.5.0\python-3.3.5\lib\site-packages\cherrypy\process\servers.py", line 206, in _start_http_thread self.httpserver.start() File "C:\Programme\WinPython-32bit-3.3.5.0\python-3.3.5\lib\site-packages\cherrypy\wsgiserver\wsgiserver3.py", line 1648, in start raise socket.error(msg) OSError: No socket could be created -- (('0.0.0.0', 9000): [WinError 10106] Der angeforderte Dienstanbieter konnte nicht geladen oder initialisiert werden) 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Bus STOPPING 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE HTTP Server cherrypy._cpwsgi_server.CPWSGIServer(('0.0.0.0', 9000)) already shut down 2015-08-14 16:55:18,218 ERROR cherrypy.error [14/Aug/2015:16:55:18] ENGINE Removed handler for console events. 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Stopped thread '_TimeoutMonitor'. 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Bus STOPPED 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Bus EXITING 2015-08-14 16:55:18,218 INFO cherrypy.error [14/Aug/2015:16:55:18] ENGINE Bus EXITED 2015-08-17 08:50:16,665 INFO cherrypy.error [17/Aug/2015:08:50:16] ENGINE Console event 0: shutting down bus
有趣的是部分 OSError:无法创建套接字,但为什么在服务中无法实现?
我的实施如下:
import builtins, os, win32event, win32serviceutil, win32service, servicemanager, socket, logging
from UseCases.ProductionEngineering.Development.Ctt.Deployment.BuildServer.BuildXMLRPCInterface import BuildXMLRPCInterface as Interface
class BuildServerService(win32serviceutil.ServiceFramework):
'''
classdocs
'''
_svc_name_ = "BuildServerService"
_svc_display_name_ = "Build Server Service"
_WORKING_DIRECTORY = "C:\BuildServerService\Sources"
_svc_deps_ = None # sequence of service names on which this depends
# Only exists on Windows 2000 or later, ignored on Windows NT
_svc_description_ = "Build Server Service"
def __init__(self, args, debug=False):
self.debug = debug
if not self.debug:
builtins.RUNNING_AS_SERVICE = True
# disable user interfaces in services because this causes errors during startup
builtins.NO_UserInterface = True
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
self.hSockEvent = win32event.CreateEvent(None, 0, 0, None)
socket.setdefaulttimeout(60)
logging.basicConfig(filename=os.path.join(self._WORKING_DIRECTORY, "service.log"), level=logging.DEBUG, format='%(asctime)s\t%(levelname)s\t%(name)s\t%(message)s')
self.logger = logging.getLogger(__name__)
self.logger.log(1, 'Load ' + __file__)
def SvcDoRun(self):
import os.path, cherrypy
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_, ''))
path = os.path.dirname(os.path.abspath(__file__))
os.chdir(path)
self.logger.info("Start Service...")
config = {
'global' : {
'server.socket_host' : '0.0.0.0',
'server.socket_port' : 9000,
'server.thread_pool' : 8,
'log.screen': True,
'engine.autoreload.on': False,
'engine.SIGHUP': None,
'engine.SIGTERM': None,
},
'/' : {
'tools.xmlrpc.on': True,
'tools.xmlrpc.allow_none' : True
}
}
cherrypy.quickstart(Interface(), '/api', config)
def SvcStop(self):
import cherrypy
# close server and call inherited class method
self.logger.info("Stop Service...")
win32event.SetEvent(self.hWaitStop)
cherrypy.engine.exit()
if self.debug:
return
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STOPPED,
(self._svc_name_, '. Bye'))
if __name__ == '__main__':
#sc = BuildServerService(None, debug=True)
#sc.SvcDoRun()
win32serviceutil.HandleCommandLine(BuildServerService)
我使用带有python 3.3.5的winpython发行版和以下模块:
CherryPy==3.8.0
Cython==0.20.1
Django==1.7
Jinja2==2.7.2
MarkupSafe==0.18
Pillow==2.4.0
Polygon==3.0.5
PyOpenGL==3.0.2
PyQtdesignerplugins==1.1
PyQtdoc==4.8.4
PySide==1.2.1
PyWavelets==0.3.0
Pygments==1.6
SQLAlchemy==0.9.4
Scidoc==1.8.0
SocksiPy-branch==1.02
Sphinx==1.2.2
TTFQuery==1.0.4
VPython==5.74
astroid==1.0.1
certifi==2015.04.28
chardet==2.3.0
colorama==0.2.7
comtypes==1.1.1
cx-Freeze==4.3.2
django-pyodbc-azure==1.2.1
docutils==0.11
docx==0.2.4
fonttools==2.3
formlayout==1.0.15
genicam==0.0.0-20150331112308-pylon-4.2.1.4845
guidata==1.6.1
guiqwt==2.3.2
h5py==2.2.1
ipython==2.0.0
logging==0.4.9.6
logilab-common==0.61.0
lxml==3.4.4
mahotas==1.1.0
matplotlib==1.3.1
mpld3==0.2
networkx==1.8.1
nose==1.3.1
numexpr==2.3.1
numpy==1.8.1
opencv3-python==3.0.0.dev0
pandas==0.13.1
patsy==0.2.1
psutil==2.1.0
pyhdf==0.8.3
pylint==1.1.0
pylon==0.0.0-20150331112313-pylon-4.2.1.4845
pyodbc==3.0.7
pyparsing==2.0.1
pyreadline==2.0
pyserial==2.7
python-dateutil==2.2
python-docx==0.8.5
pytz==2013.9
pywin32==218.5
pyzmq==14.0.1
regex==2015.06.24
reportlab==3.0
requests==2.5.0
scikit-image==0.9.3
scikit-learn==0.14.1
scipy==0.13.3
simplejson==3.3.3
six==1.6.1
spyder==2.3.0beta2
statsmodels==0.5.0
sympy==0.7.5
tables==3.1.1
tornado==4.2.1
vispy==0.2.1
wheel==0.24.0
winpython==1.0
xlrd==0.9.3
xml-diff==0.6.0
您认为pywin32或CherryPy问题是什么? 提前谢谢