我想在一个 python 程序中制作 3 个 SocketServer:
import threading
import SimpleHTTPServer
import SocketServer
class PolicyHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.data = self.request.recv(128).strip()
print self.data
if self.data == '<policy-file-request/>\0':
self.request.sendall('message 1')
class LiveRequestHandler(SocketServer.BaseRequestHandler):
def handle(self):
self.request.sendall('message 2')
def main():
httpd = SocketServer.TCPServer(('',80),\
SimpleHTTPServer.SimpleHTTPRequestHandler)
httpdThr = threading.Thread(target=httpd.serve_forever)
httpdThr.daemon = True
httpdThr.start()
liveServer = SocketServer.TCPServer(('',8090), LiveRequestHandler)
livThr = threading.Thread(target=liveServer.serve_forever)
livThr.daemon = True
livThr.start()
policyServer = SocketServer.TCPServer(('',834), PolicyHandler)
policyThr = threading.Thread(target=policyServer.serve_forever)
policyThr.daemon = True
policyThr.start()
if __name__ == '__main__':
main()
我运行它,输出如下:
Finding files... done.
Importing test modules ... done.
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
奇怪的是它立即退出程序,所以我的客户端程序无法连接到三套接字服务器。如何防止它退出程序?