-
-
[求助]求大神解决(python [socket] [win Server] )小哥哥小姐姐快来帮忙解决一下 谢
-
发表于: 2018-8-9 18:44 1961
-
大神帮忙解决.
问题描述:
1.编写了一个程序, socketServer 用于接受socket client(客户端发送过来的数据) ===>测试没问题
2.经查阅荡了一个python编写的Windows Service. (windes 系统服务.)实现的是把服务安装到Windows之后,测试启动,写入日志没问题.
那么问题来了. 这两个程序怎么整合到一起.实现 安装Windows 服务之后, 增加了 socket Server的接收功能(注意这里是socket Server, 可以多个客户端连接的)
两个程序代码如下:
谢谢大神帮忙解决. 我要详细的说明啊, 小白什么也不会.
#encoding=utf-8 import win32serviceutil import win32service import win32event import win32timezone import os class PythonServices(win32serviceutil.ServiceFramework): _svc_name_ = 'PythonServices' _svc_display_name_ = 'python serverice 程序' _svc_description_ = '这个是python练习' def __init__(self,args): win32serviceutil.ServiceFramework.__init__(self,args) self.hWaitStop = win32event.CreateEvent(None,0,0,None) self.logger = self._getLogger() self.run = True def _getLogger(self): import inspect import logging import datetime logger = logging.getLogger('[PythonService]') this_file = inspect.getfile(inspect.currentframe()) dirpath = os.path.abspath(os.path.dirname(this_file)) logName= '%s.log'%datetime.datetime.now().strftime('%Y-%m-%d') handler = logging.FileHandler(os.path.join(dirpath,logName)) formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s') handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.INFO) return logger def SvcDoRun(self): import time self.logger.info('service is run...服务启动了') while self.run: self.logger.info('service is running...') time.sleep(5) def SvcStop(self): self.logger.info('service is stop.停止了') self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) win32event.SetEvent(self.hWaitStop) self.run = False if __name__ == '__main__': import sys import servicemanager if len(sys.argv) == 1: try: evtsrc_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(PythonServices) servicemanager.Initialize('PythonServices',evtsrc_dll) servicemanager.StartServiceCtrlDispatcher() except win32service.error as details: import winerror if details == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: win32serviceutil.usage() else: win32serviceutil.HandleCommandLine(PythonServices)
import socket import socketserver import datetime import inspect import logging import os class MyTCPHandler(socketserver.BaseRequestHandler): logger = logging.getLogger("Serveice:") logger.setLevel(logging.INFO) logName= '%s.log'%datetime.datetime.now().strftime('%Y-%m-%d') fh = logging.FileHandler(logName) fh.setLevel(logging.DEBUG) formatter = logging.Formatter("%(asctime)s-%(name)s-%(levelname)s:%(message)s"+'\n') fh.setFormatter(formatter) logger.addHandler(fh) def handle(self): self.logger.info('客户端'+str(self.client_address)+'已经连接.') while True: try: self.data = self.request.recv(1024).strip() self.logger.info(str(self.data.hex().upper())) except ConnectionResetError as e: self.logger.info(':客户端'+str(self.client_address)+'断开连接.'+str(e)) break if __name__ =='__main__': HOST = socket.gethostbyname(socket.gethostname()) PORT = 9999 server = socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler) server.serve_forever()
[注意]传递专业知识、拓宽行业人脉——看雪讲师团队等你加入!
赞赏
他的文章
看原图
赞赏
雪币:
留言: