本文整理汇总了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()