使用python 3.6异步的UDP代理服务器

使用python 3.6异步的UDP代理服务器,python,python-3.x,udp,python-asyncio,python-3.6,Python,Python 3.x,Udp,Python Asyncio,Python 3.6,我很想知道如何使用异步模块创建UDP代理服务器。今天早些时候,我尝试了Twisted,但没有取得多大进展。我确实在这里找到了一些直接使用套接字的stackoverflow的简单解决方案,但我想知道使用async库是否可能实现这一点 为清楚起见,我要寻找的是: 步骤1:客户端A(可以是多个)与代理B对话 步骤2:代理B将请求发送到服务器C。 步骤3:服务器C响应代理B 9月4日:代理B响应客户端A 以下是我目前掌握的情况: import asyncio class EchoServerProto

我很想知道如何使用异步模块创建UDP代理服务器。今天早些时候,我尝试了Twisted,但没有取得多大进展。我确实在这里找到了一些直接使用套接字的stackoverflow的简单解决方案,但我想知道使用async库是否可能实现这一点

为清楚起见,我要寻找的是:

步骤1:客户端A(可以是多个)与代理B对话 步骤2:代理B将请求发送到服务器C。 步骤3:服务器C响应代理B 9月4日:代理B响应客户端A

以下是我目前掌握的情况:

import asyncio

class EchoServerProtocol(asyncio.DatagramProtocol):
    def connection_made(self, transport):
        self.transport = transport

    def datagram_received(self, data, addr, args=None):
        message = data
        server_addr = ('localhost', 27015)
        print('Received %r from %s' % (message, addr))
        print('Send %r to %s' % (message, server_addr))
        self.transport.sendto(data, server_addr)

loop = asyncio.get_event_loop()
print("Starting UDP server")
# One protocol instance will be created to serve all client requests
listen = loop.create_datagram_endpoint(
    EchoServerProtocol,
    local_addr=('0.0.0.0', 27016),
)
transport, protocol = loop.run_until_complete(listen)

try:
    loop.run_forever()
except KeyboardInterrupt:
    pass

transport.close()
loop.close()
以上基本上是这样的:

我所做的将使您进入第3步,但当然在第4步失败。它将其发送回服务器C

您将注意到另一个协议示例,
EchoServerClientProtocol
,为每个连接创建一个新实例。我认为我需要为每个连接创建一个新实例,因为这样我就可以在实例中存储原始地址并返回它。我假设这不适用于
create\u datagram\u endpoint
,或者我不知道怎么做


不过,可能有更好的方法。

服务器B需要一个本地端点,每个客户端a需要一个到服务器C的远程端点

这是


另外,您可能会感兴趣

服务器B应该如何知道将服务器C的回复转发到哪里?此外,UDP对于请求/应答模式没有多大意义,因为数据包可能会丢失。@Vincent我是一个UDP新手,但是否可以使用socks5保留原始发件人?我不太了解,但使用socks5中继UDP数据包需要。@JamesR我需要建立在您在这里描述的UDP代理实现之上。你能分享一下对你有用的东西吗?