本文整理匯總了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()