本文整理汇总了Python中six.moves.socketserver.TCPServer方法的典型用法代码示例。如果您正苦于以下问题:Python socketserver.TCPServer方法的具体用法?Python socketserver.TCPServer怎么用?Python socketserver.TCPServer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类six.moves.socketserver
的用法示例。
在下文中一共展示了socketserver.TCPServer方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: udp_server_process
# 需要导入模块: from six.moves import socketserver [as 别名]
# 或者: from six.moves.socketserver import TCPServer [as 别名]
def udp_server_process():
s1 = socketserver.UDPServer(
("127.0.0.1", random.randint(20000, 30000)),
UdpEcho1Handler)
server_addr1 = s1.server_address
p1 = Process(target=s1.serve_forever)
p1.start()
s2 = socketserver.TCPServer(
("127.0.0.1", random.randint(20000, 30000)),
TcpEchoHandler)
server_addr2 = s2.server_address
p2 = Process(target=s2.serve_forever)
p2.start()
yield (server_addr1, server_addr2)
p1.terminate()
p2.terminate()
示例2: tcp_server_process
# 需要导入模块: from six.moves import socketserver [as 别名]
# 或者: from six.moves.socketserver import TCPServer [as 别名]
def tcp_server_process():
server = socketserver.TCPServer(
("127.0.0.1", random.randint(20000, 30000)),
TcpEchoHandler)
server_addr = server.server_address
pcs = Process(target=server.serve_forever)
pcs.start()
yield server_addr
os.kill(pcs.pid, signal.SIGINT)
pcs.join()
示例3: test_arecv_zero
# 需要导入模块: from six.moves import socketserver [as 别名]
# 或者: from six.moves.socketserver import TCPServer [as 别名]
def test_arecv_zero():
server = socketserver.TCPServer(
("127.0.0.1", random.randint(20000, 30000)),
TcpEchoHandler)
server_addr = server.server_address
pcs = Process(target=server.serve_forever)
pcs.start()
io_engine = ioloop.get_ioloop("select")
client = connection.TCPConnection(io_engine=io_engine)
def on_recved(_, data, err):
client.close()
client.stop()
assert not data
def on_sent(conn, err):
assert err.errcode == connection.E_OK
os.kill(pcs.pid, signal.SIGTERM)
conn.arecv(2048, on_recved)
pcs.join()
def on_connected(conn, err):
assert err.errcode == connection.E_OK
conn.asend(b'x' * 2048, on_sent)
time.sleep(1)
client.aconnect(server_addr, on_connected)
client.run()
示例4: forward_ports
# 需要导入模块: from six.moves import socketserver [as 别名]
# 或者: from six.moves.socketserver import TCPServer [as 别名]
def forward_ports(pair_ports, udid=None, threaded=True, bufsize=128):
'''iOS真机设备的端口转发
:param pair_ports: 端口对的数组,每对端口中前一个代表远程端口,后一个代表本地端口,例如:["8100:8100", "8200:8200"]
:type pair_ports: list
'''
servers=[]
if threaded:
serverclass = ThreadedTCPServer
else:
serverclass = TCPServer
for pair_port in pair_ports:
rport, lport = pair_port.split(":")
rport = int(rport)
lport = int(lport)
print("Forwarding local port %d to remote port %d"%(lport, rport))
server = serverclass(("localhost", lport), TCPRelay)
server.rport = rport
server.bufsize = bufsize
server.udid = udid
servers.append(server)
alive = True
while alive:
try:
rl, wl, xl = select.select(servers, [], [])
for server in rl:
server.handle_request()
except:
alive = False
示例5: http_503_server
# 需要导入模块: from six.moves import socketserver [as 别名]
# 或者: from six.moves.socketserver import TCPServer [as 别名]
def http_503_server():
class RequestHandler503(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""A custom http handler that checks for duplicate 'Host' headers from the most
recent http request, and always returns a 503 http code"""
def do_POST(self):
# Ensure that only one 'Host' header is contained in the request before responding.
request_headers = None
host_hdr_count = 0
if six.PY2:
# The unfortunately named self.headers here is an instance of mimetools.Message that
# contains the request headers.
request_headers = self.headers.headers
host_hdr_count = sum([header.startswith('Host:') for header in request_headers])
if six.PY3:
# In Python3 self.Headers is an HTTPMessage.
request_headers = self.headers
host_hdr_count = sum([header[0] == 'Host' for header in request_headers.items()])
assert host_hdr_count == 1, "need single 'Host:' header in %s" % request_headers
# Respond with 503.
self.send_response(code=http_client.SERVICE_UNAVAILABLE, message="Service Unavailable")
self.end_headers()
self.wfile.write("extra text".encode('utf-8'))
class TestHTTPServer503(object):
def __init__(self):
self.HOST = "localhost"
self.PORT = get_unused_port()
self.httpd = socketserver.TCPServer((self.HOST, self.PORT), RequestHandler503)
self.http_server_thread = threading.Thread(target=self.httpd.serve_forever)
self.http_server_thread.start()
server = TestHTTPServer503()
yield server
# Cleanup after test.
if server.httpd is not None:
server.httpd.shutdown()
if server.http_server_thread is not None:
server.http_server_thread.join()