我正在尝试使用AsyncIO的asyncio.protocol编写代理服务器,这似乎在最大程度上起作用。但是我收到一个错误,使我非常困惑。
我的代码如下所示:
import asyncio
import socket
users = {}
class UserConnection(object):
def __init__(self, client_socket, proxy_socket, peername):
self.client_socket = client_socket
self.proxy_socket = proxy_socket
self.peername = peername
class ClientProxyProtocol(asyncio.Protocol):
def __init__(self):
self.transport = None
self.session = None
server_connection = loop.create_connection(
lambda: ProxyForwardProtocol,
host="127.0.0.1",
port=25575)
task = loop.create_task(server_connection)
task.add_done_callback(self.handle_proxy_connection)
def connection_made(self, transport):
# Get peername, client socket and create proxy socket
peername = transport.get_extra_info('peername')
client_socket = transport.get_extra_info('socket')
# Get session object for a user and add in dict keyed of proxy socket
self.session = UserConnection(client_socket, None, peername)
print('Connection from {}'.format(peername))
self.transport = transport
def handle_proxy_connection(self, task):
trans, proto = task.result()
proxy_socket = trans.get_extra_info("socket")
self.session.proxy_socket = proxy_socket
users[proxy_socket] = self.session
print(trans,"\n",proto)
def data_received(self, data):
message = str(data)
print('Data received: {!r}'.format(message))
def connection_lost(self, exc):
print('Close the client socket')
self.transport.close()
class ProxyForwardProtocol(asyncio.Protocol):
def __init__(self,transport):
self.transport = transport
def connection_made(self, transport):
proxy_socket = transport.get_extra_info("socket")
print("HELLO")
self.transport = transport
def connection_lost(self, exc):
print("closing forward connection")
self.transport.close()
loop = asyncio.get_event_loop()
# Each client connection will create a new protocol instance
coro = loop.create_server(ClientProxyProtocol, '127.0.0.1', 25565)
server = loop.run_until_complete(coro)
# Serve requests until Ctrl+C is pressed
print('Serving on {}'.format(server.sockets[0].getsockname()))
try:
loop.run_forever()
except KeyboardInterrupt:
pass
# Close the server
server.close()
loop.run_until_complete(server.wait_closed())
loop.close()
运行此脚本在某种程度上可以正常工作,它接受新连接并启动新的Protocol实例,但是在尝试与我的目标服务器建立协议连接后,它会产生以下错误。它产生以下错误:
Exception in callback ProxyForwardProtocol.connection_made(<_SelectorSoc...e, bufsize=0>>) at .\asyncproto.py:53
handle: <Handle ProxyForwardProtocol.connection_made(<_SelectorSoc...e, bufsize=0>>) at .\asyncproto.py:53>
Traceback (most recent call last):
File "C:\Users\Huhn\AppData\Local\Programs\Python\Python36-32\lib\asyncio\events.py", line 126, in _run
self._callback(*self._args)
TypeError: connection_made() missing 1 required positional argument: 'transport'
我的问题如下:该句柄表明connection_made方法采用了SelectorSocketTransport,我认为这是一种传输,但仍然表示它缺少传输。这对我来说毫无意义。
如果有人能告诉我我的错误所在,我将不胜感激,因为我没有发现任何有类似问题的人,并且被困住了。如果我对代理服务器有任何架构上的误解,请随时指出。我这样做是出于教学目的,这就是为什么我不使用可能存在于其中的第三方替代品的原因。
谢谢
最佳答案
看来您只是在ClientProxyProtocol.__init__()
中打了错字。
在create_connection
中,第一个参数必须是可调用的,它返回协议对象,而您的(lambda: ProxyForwardProtocol)
返回协议类。
因此,当框架调用connection_made()
时,它将传递单个参数(传输),但第一个参数必须为self
。协议是一个类,而不是类实例->未绑定方法connection_made()
-> self
未自动提供。
删除ProxyForwardProtocol.__init__
方法并像这样更改您的呼叫:
server_connection = loop.create_connection(
ProxyForwardProtocol,
host="127.0.0.1",
port=25575)
关于python - 尽管存在传输,但由于缺少传输,Python AsyncIO无法创建协议(protocol),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50546605/