本文整理匯總了Python中SocketServer.ThreadingMixIn方法的典型用法代碼示例。如果您正苦於以下問題:Python SocketServer.ThreadingMixIn方法的具體用法?Python SocketServer.ThreadingMixIn怎麽用?Python SocketServer.ThreadingMixIn使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類SocketServer
的用法示例。
在下文中一共展示了SocketServer.ThreadingMixIn方法的5個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: run_tcp
# 需要導入模塊: import SocketServer [as 別名]
# 或者: from SocketServer import ThreadingMixIn [as 別名]
def run_tcp(realport, fakeport, handler):
class SingleTCPHandler(SocketServer.BaseRequestHandler):
def handle(self):
srcaddr, srcport = self.request.getpeername()
print("Connection from {}:{}".format(srcaddr, srcport))
handler(self.request, fakeport)
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
daemon_threads = True
allow_reuse_address = True
def __init__(self, server_address, RequestHandlerClass):
SocketServer.TCPServer.__init__(self, server_address, RequestHandlerClass)
server = SimpleServer(('127.0.0.1', realport), SingleTCPHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
示例2: run_udp
# 需要導入模塊: import SocketServer [as 別名]
# 或者: from SocketServer import ThreadingMixIn [as 別名]
def run_udp(realport, fakeport, handler):
class SingleUDPHandler(SocketServer.BaseRequestHandler):
def handle(self):
srcaddr, srcport = self.client_address
print("Packet from {}:{}".format(srcaddr, srcport))
handler(self.request[1], self.request[0], self.client_address, fakeport)
class SimpleServer(SocketServer.ThreadingMixIn, SocketServer.UDPServer):
daemon_threads = True
def __init__(self, server_address, RequestHandlerClass):
SocketServer.UDPServer.__init__(self, server_address, RequestHandlerClass)
server = SimpleServer(('127.0.0.1', realport), SingleUDPHandler)
try:
server.serve_forever()
except KeyboardInterrupt:
sys.exit(0)
示例3: _http_python2
# 需要導入模塊: import SocketServer [as 別名]
# 或者: from SocketServer import ThreadingMixIn [as 別名]
def _http_python2(self, listen_ssl=False):
"""Listen for HTTP connections with Python 2."""
class ThreadingSimpleServer(SocketServer.ThreadingMixIn,
BaseHTTPServer.HTTPServer):
pass
server = ThreadingSimpleServer(self.listen_socket,
DeenHTTPRequestHandler)
os.chdir(self.serving_directory)
message = 'Serving HTTP at port ' + str(self.listen_port)
if listen_ssl:
message += ' (SSL)'
print(message)
try:
while 1:
sys.stdout.flush()
server.handle_request()
except KeyboardInterrupt:
server.socket.close()
示例4: start_server
# 需要導入模塊: import SocketServer [as 別名]
# 或者: from SocketServer import ThreadingMixIn [as 別名]
def start_server():
server_address = ('127.0.0.1', 0)
class DaemonThreadingMixIn(ThreadingMixIn):
daemon_threads = True
class ThreadedHTTPServer(DaemonThreadingMixIn, HTTPServer):
"""Handle requests in a separate thread."""
# not actually going to do anything with this server, so a
# do-nothing handler is reasonable
httpd = ThreadedHTTPServer(server_address, BaseHTTPRequestHandler)
server_thread = threading.Thread(target=httpd.serve_forever)
server_thread.daemon = True
server_thread.start()
return httpd, server_thread
示例5: tcp_test_main
# 需要導入模塊: import SocketServer [as 別名]
# 或者: from SocketServer import ThreadingMixIn [as 別名]
def tcp_test_main():
class Server(ThreadingMixIn, TCPServer):
pass
queue, Handler = create_length_prefixed_tcp_handler()
server = Server(('localhost', 12345), Handler)
server.allow_reuse_address = True
server.serve_forever()