本文整理汇总了Python中socketserver.StreamRequestHandler方法的典型用法代码示例。如果您正苦于以下问题:Python socketserver.StreamRequestHandler方法的具体用法?Python socketserver.StreamRequestHandler怎么用?Python socketserver.StreamRequestHandler使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socketserver
的用法示例。
在下文中一共展示了socketserver.StreamRequestHandler方法的14个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _prepare_server
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def _prepare_server(self, address):
class RequestHandler(socketserver.StreamRequestHandler):
# 打开 TCP_NODELAY
disable_nagle_algorithm = True
def handle(sub_self):
if self.timeout is not None:
sub_self.connection.settimeout(self.timeout)
self.connection_class(
self, self.stream_class(sub_self.connection), sub_self.client_address
).handle()
class MyServer(self.server_class):
request_queue_size = self.backlog
# 主线程退出时,所有子线程结束
daemon_threads = True
# 必须在server_bind之前
allow_reuse_address = True
self.server = MyServer(address, RequestHandler)
示例2: test_shutdown
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_shutdown(self):
# Issue #2302: shutdown() should always succeed in making an
# other thread leave serve_forever().
class MyServer(socketserver.TCPServer):
pass
class MyHandler(socketserver.StreamRequestHandler):
pass
threads = []
for i in range(20):
s = MyServer((HOST, 0), MyHandler)
t = threading.Thread(
name='MyServer serving',
target=s.serve_forever,
kwargs={'poll_interval':0.01})
t.daemon = True # In case this function raises.
threads.append((t, s))
for t, s in threads:
t.start()
s.shutdown()
for t, s in threads:
t.join()
s.server_close()
示例3: test_shutdown_request_called_if_verify_request_false
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_shutdown_request_called_if_verify_request_false(self):
# Issue #26309: BaseServer should call shutdown_request even if
# verify_request is False
class MyServer(socketserver.TCPServer):
def verify_request(self, request, client_address):
return False
shutdown_called = 0
def shutdown_request(self, request):
self.shutdown_called += 1
socketserver.TCPServer.shutdown_request(self, request)
server = MyServer((HOST, 0), socketserver.StreamRequestHandler)
s = socket.socket(server.address_family, socket.SOCK_STREAM)
s.connect(server.server_address)
s.close()
server.handle_request()
self.assertEqual(server.shutdown_called, 1)
server.server_close()
示例4: test_basics
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_basics(self):
class Handler(socketserver.StreamRequestHandler):
def handle(self):
self.server.wfile = self.wfile
self.server.wfile_fileno = self.wfile.fileno()
self.server.request_fileno = self.request.fileno()
server = socketserver.TCPServer((HOST, 0), Handler)
self.addCleanup(server.server_close)
s = socket.socket(
server.address_family, socket.SOCK_STREAM, socket.IPPROTO_TCP)
with s:
s.connect(server.server_address)
server.handle_request()
self.assertIsInstance(server.wfile, io.BufferedIOBase)
self.assertEqual(server.wfile_fileno, server.request_fileno)
示例5: test_TCPServer
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_TCPServer(self):
self.run_server(socketserver.TCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例6: test_ThreadingTCPServer
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_ThreadingTCPServer(self):
self.run_server(socketserver.ThreadingTCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例7: test_ForkingTCPServer
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_ForkingTCPServer(self):
with simple_subprocess(self):
self.run_server(socketserver.ForkingTCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例8: test_UnixStreamServer
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_UnixStreamServer(self):
self.run_server(socketserver.UnixStreamServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例9: test_ThreadingUnixStreamServer
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_ThreadingUnixStreamServer(self):
self.run_server(socketserver.ThreadingUnixStreamServer,
socketserver.StreamRequestHandler,
self.stream_examine)
示例10: test_tcpserver_bind_leak
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_tcpserver_bind_leak(self):
# Issue #22435: the server socket wouldn't be closed if bind()/listen()
# failed.
# Create many servers for which bind() will fail, to see if this result
# in FD exhaustion.
for i in range(1024):
with self.assertRaises(OverflowError):
socketserver.TCPServer((HOST, -1),
socketserver.StreamRequestHandler)
示例11: test_issue5949
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_issue5949(self):
class EOFHandler(socketserver.StreamRequestHandler):
def handle(self):
# EOF without sending a complete welcome message.
self.wfile.write(b'* OK')
with self.reaped_server(EOFHandler) as server:
self.assertRaises(imaplib.IMAP4.abort,
self.imap_class, *server.server_address)
示例12: test_InterruptServerSelectCall
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_InterruptServerSelectCall(self):
with self.mocked_select_module() as mock_select:
pid = self.run_server(socketserver.TCPServer,
socketserver.StreamRequestHandler,
self.stream_examine)
# Make sure select was called again:
self.assertGreater(mock_select.called, 1)
# Alas, on Linux (at least) recvfrom() doesn't return a meaningful
# client address so this cannot work:
# @requires_unix_sockets
# def test_UnixDatagramServer(self):
# self.run_server(socketserver.UnixDatagramServer,
# socketserver.DatagramRequestHandler,
# self.dgram_examine)
#
# @requires_unix_sockets
# def test_ThreadingUnixDatagramServer(self):
# self.run_server(socketserver.ThreadingUnixDatagramServer,
# socketserver.DatagramRequestHandler,
# self.dgram_examine)
#
# @requires_unix_sockets
# @requires_forking
# def test_ForkingUnixDatagramServer(self):
# self.run_server(socketserver.ForkingUnixDatagramServer,
# socketserver.DatagramRequestHandler,
# self.dgram_examine)
示例13: _setup
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def _setup(self, imap_handler, connect=True):
"""
Sets up imap_handler for tests. imap_handler should inherit from either:
- SimpleIMAPHandler - for testing IMAP commands,
- socketserver.StreamRequestHandler - if raw access to stream is needed.
Returns (client, server).
"""
class TestTCPServer(self.server_class):
def handle_error(self, request, client_address):
"""
End request and raise the error if one occurs.
"""
self.close_request(request)
self.server_close()
raise
self.addCleanup(self._cleanup)
self.server = self.server_class((support.HOST, 0), imap_handler)
self.thread = threading.Thread(
name=self._testMethodName+'-server',
target=self.server.serve_forever,
# Short poll interval to make the test finish quickly.
# Time between requests is short enough that we won't wake
# up spuriously too many times.
kwargs={'poll_interval': 0.01})
self.thread.daemon = True # In case this function raises.
self.thread.start()
if connect:
self.client = self.imap_class(*self.server.server_address)
return self.client, self.server
示例14: test_EOF_without_complete_welcome_message
# 需要导入模块: import socketserver [as 别名]
# 或者: from socketserver import StreamRequestHandler [as 别名]
def test_EOF_without_complete_welcome_message(self):
# http://bugs.python.org/issue5949
class EOFHandler(socketserver.StreamRequestHandler):
def handle(self):
self.wfile.write(b'* OK')
_, server = self._setup(EOFHandler, connect=False)
self.assertRaises(imaplib.IMAP4.abort, self.imap_class,
*server.server_address)