Python AsyncIO无法创建协议,因为虽然存在传输,但缺少传输

时间:2018-05-26 20:08:11

标签: python asynchronous python-asyncio

我正在尝试使用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,我认为它是一个传输,但仍然说它缺少一个传输。这对我来说毫无意义。

我真的很感激,如果有人能告诉我我的错误在哪里,因为我没有发现任何有类似问题的人,并且有点卡住了。如果我对代理有任何建筑误解,请随意指出它们。我这样做是为了教学练习,这就是我没有使用可能存在的第三方替代方案的原因。

谢谢

1 个答案:

答案 0 :(得分:3)

您似乎只是在ClientProxyProtocol.__init__()中输了一个拼写错误。

create_connection中,第一个参数必须是可调用的,它返回Protocol对象,而(lambda: ProxyForwardProtocol)返回协议类

因此,当框架调用connection_made()时,它将传递一个参数(传输),但第一个参数必须是self。协议是一个类而不是类实例 - &gt;方法connection_made()未绑定 - &gt; <{1}}未自动提供。

删除self方法并更改您的通话:

ProxyForwardProtocol.__init__