本文整理汇总了Python中SocketServer.BaseRequestHandler方法的典型用法代码示例。如果您正苦于以下问题:Python SocketServer.BaseRequestHandler方法的具体用法?Python SocketServer.BaseRequestHandler怎么用?Python SocketServer.BaseRequestHandler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SocketServer
的用法示例。
在下文中一共展示了SocketServer.BaseRequestHandler方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: run_tcp
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [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 BaseRequestHandler [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: __init__
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def __init__(self):
self.CryptoArgs = dict()
self.CryptoArgs['INITKEY'] = "My initial key"
self.CryptoArgs['DEBUG'] = False
self.CryptoArgs['CYCLES'] = 3
self.CryptoArgs['BLOCK_SZ'] = 20
self.CryptoArgs['KEY_ADV'] = 5
self.CryptoArgs['KEY_MAGNITUDE'] = 1
SimpleCryptSocketExt.__init__(self, self.CryptoArgs)
self.InBoundData = ""
self.OutBoundData = ""
#self.salt_method = "Simple"
#self.salt_method = "NTP"
#self.salt_method = "PrevData"
#####################################################################################
#####################################################################################
#Class to handle Server coms overriding BaseRequestHandler handle method
示例4: 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)
示例5: __init__
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def __init__(self, request, client_address, server):
SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
return
示例6: __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)
示例7: __init__
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def __init__(self, request, client_address, server):
self.nick = None # Client's currently registered nickname
self.host = client_address # Client's hostname / ip.
self.status = 0 # Client's status (0=available, 1=away, 2=playing)
self.clienttype = None # can be: player(fba), spectator(fba) or client
self.previous_status = None # Client's previous status (0=available, 1=away, 2=playing)
self.opponent = None # Client's opponent
self.quark = None # Client's quark (in-game uri)
self.fbaport = 0 # Emulator's fbaport
self.side = 0 # Client's side: 1=P1, 2=P2 (0=spectator before savestate, 3=spectator after savestate)
self.port = 6009 # Client's port
self.city = "null" # Client's city
self.country = "null" # Client's country
self.cc = "null" # Client's country code
self.lastmsgtime = 0 # timestamp of the last chat message
self.challengetime = 0 # timestamp of the last challenge
self.lastmsg = '' # last chat message
self.spamhit = 0 # how many times has been warned for spam
self.useports = False # set to true when we have potential problems with NAT traversal
self.version = 0 # client version
self.warnmsg = '' # Warning message (shown after match)
self.turboflag = 0 # turbo flag helper
self.send_queue = [] # Messages to send to client (strings)
self.channel = GGPOChannel("lobby",'', "The Lobby") # Channel the client is in
self.challenging = {} # users (GGPOClient instances) that this client is challenging by host
try:
set_keepalive_linux(request)
except:
pass
SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
示例8: __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)
示例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: create_length_prefixed_tcp_handler
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def create_length_prefixed_tcp_handler():
queue = Queue()
class LengthPrefixedTcpHandler(BaseRequestHandler):
def handle(self):
#msg = _read_length_prefixed_msg(self.request)
# this will run inside a new thread
self.request.send("hello\n")
while True:
b = _read_n_bytes(self.request, 10)
self.request.send("you sent: %s" % b)
queue.put(b)
return queue, LengthPrefixedTcpHandler
示例11: 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)
示例12: __init__
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def __init__ (self, request, client_address, server):
self.logger = logging.getLogger('EchoRequestHandler')
self.logger.debug('__init__')
SocketServer.BaseRequestHandler.__init__(self, request, client_address, server)
return
示例13: setup
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def setup(self):
self.logger.debug('setup')
return SocketServer.BaseRequestHandler.setup(self)
示例14: finish
# 需要导入模块: import SocketServer [as 别名]
# 或者: from SocketServer import BaseRequestHandler [as 别名]
def finish(self):
self.logger.debug('finish')
return SocketServer.BaseRequestHandler.finish(self)
示例15: 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()