本文整理匯總了Python中socketserver.BaseRequestHandler方法的典型用法代碼示例。如果您正苦於以下問題:Python socketserver.BaseRequestHandler方法的具體用法?Python socketserver.BaseRequestHandler怎麽用?Python socketserver.BaseRequestHandler使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類socketserver
的用法示例。
在下文中一共展示了socketserver.BaseRequestHandler方法的12個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: handle
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def handle(self):
# 創建一個鏈接,繼承於socketserver中的BaseRequestHandler類
conn = self.request
conn.sendall(bytes("Welcome to login...", encoding="utf-8"))
print("Client connect...")
while True:
# 輸出等待客戶端發送內容
print("Waitting for recving message...")
# 接收客戶端發送過來的內容
message = conn.recv(1024)
# 輸出用戶發送過來的內容
print(message.decode('utf-8'))
# 如果用戶輸入的是q
if message == "exit":
break
# 給客戶端發送內容
data = input("Reply message:")
conn.sendall(data.encode('utf-8'))
示例2: setUp
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def setUp(self):
self.registry = CollectorRegistry()
self.data = ''
class TCPHandler(SocketServer.BaseRequestHandler):
def handle(s):
self.data = s.request.recv(1024)
server = SocketServer.TCPServer(('', 0), TCPHandler)
class ServingThread(threading.Thread):
def run(self):
server.handle_request()
server.socket.close()
self.t = ServingThread()
self.t.start()
# Explicitly use localhost as the target host, since connecting to 0.0.0.0 fails on Windows
address = ('localhost', server.server_address[1])
self.gb = GraphiteBridge(address, self.registry, _timer=fake_timer)
示例3: requestHandlerFactory
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def requestHandlerFactory(data_handler):
class ThreadedTCPRequestHandler(socketserver.BaseRequestHandler):
def __init__(self, *args):
self.data_handler = data_handler
super().__init__(*args)
def handle(self):
while True:
data = receive(self.request)
if data:
response = self.data_handler(self.request, data)
if response:
send(self.request, response)
else:
break
log('Handler Exiting')
return ThreadedTCPRequestHandler
示例4: tcp_server
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def tcp_server(request):
"""Start a TCP server in a thread."""
data = list()
class Getter(object):
def __init__(self, t, s, d):
self.thread = t
self.server = s
self._data = d
@property
def data(self):
for i in range(50):
if self._data:
break
time.sleep(0.1)
return self._data
class TCPHandler(socketserver.BaseRequestHandler):
def handle(self):
data.append(self.request.recv(25))
server = socketserver.TCPServer(('', 0), TCPHandler)
thread = threading.Thread(target=server.serve_forever)
thread.daemon = True
thread.start()
def fin():
server.socket.close()
server.shutdown()
for _ in range(5):
if not thread.is_alive():
break
time.sleep(0.2)
assert not thread.is_alive()
request.addfinalizer(fin)
return Getter(thread, server, data)
示例5: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, *args, **kwargs):
self.server = args[2]
self.chain_host = self.server.remote_server[0]
self.chain_port = self.server.remote_server[1]
self.ssh_transport = self.server.ssh_transport
socketserver.BaseRequestHandler.__init__(self, *args, **kwargs)
示例6: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, sock, addr, svr):
svr.current_handler = self ## cgt xxx
SocketIO.__init__(self, sock)
socketserver.BaseRequestHandler.__init__(self, sock, addr, svr)
示例7: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, request, clientaddress, server ):
self.locals = {}
self.globals = globals().copy()
self.wfile = request.makefile("w")
self.rfile = request.makefile("r")
socketserver.BaseRequestHandler.__init__(self, request, clientaddress,
server )
示例8: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, request, client_address, server):
socketserver.BaseRequestHandler.__init__(self, request, client_address, server)
示例9: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, request, client_address, server):
self.__socksServer = server
self.__ip, self.__port = client_address
self.__connSocket= request
self.__socksVersion = 5
self.targetHost = None
self.targetPort = None
self.__NBSession= None
socketserver.BaseRequestHandler.__init__(self, request, client_address, server)
示例10: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self, globalData: GlobalData,
serverAddress: str,
RequestHandlerClass: socketserver.BaseRequestHandler):
# get reference to global data object
self.globalData = globalData
socketserver.TCPServer.__init__(self,
serverAddress,
RequestHandlerClass)
# this class is used for incoming local client connections (i.e., web page)
示例11: __init__
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def __init__(self,
globalData: GlobalData,
serverAddress: Tuple[str, int],
RequestHandlerClass: Type[socketserver.BaseRequestHandler]):
# get reference to global data object
self.globalData = globalData
socketserver.TCPServer.__init__(self,
serverAddress,
RequestHandlerClass)
# this class is used for incoming client connections
示例12: test_logging
# 需要導入模塊: import socketserver [as 別名]
# 或者: from socketserver import BaseRequestHandler [as 別名]
def test_logging(self):
"""
Test logging.
"""
messages = []
class SyslogHandler(socketserver.BaseRequestHandler):
"""
Save received messages.
"""
def handle(self):
messages.append(self.request[0].strip().decode())
syslog = socketserver.UDPServer(('0.0.0.0', 0), SyslogHandler)
try:
threading.Thread(target=syslog.serve_forever).start()
env = os.environ.copy()
env['SYSLOG_SERVER'] = syslog.server_address[0]
env['SYSLOG_PORT'] = str(syslog.server_address[1])
env['SYSLOG_PROTO'] = 'udp'
mydir = os.path.dirname(__file__)
supervisor = subprocess.Popen(
['supervisord', '-c', os.path.join(mydir, 'supervisord.conf')],
env=env,
)
try:
sleep(3)
pid = subprocess.check_output(
['supervisorctl', 'pid', 'messages']
).decode().strip()
sleep(8)
self.assertEqual(
list(map(strip_volatile, messages)),
['<14>DATE HOST messages[{pid}]: Test {i} \n\x00'.format(
pid=pid,
i=i)
for i in range(4)]
)
finally:
supervisor.terminate()
finally:
syslog.shutdown()