繁体   English   中英

扭曲了Python的代理服务器

[英]Proxy Server with Python Twisted

我正在尝试编写执行以下操作的代理服务器:

  1. 从客户端接收数据。
  2. 验证数据。
  3. 转发数据到服务器。
  4. 从服务器接收数据后immediately发送回客户端。

node.js我可以使用server.pipe(client)执行#4。 在扭曲中有类似的技巧吗?

我有的:

class Player(protocol.Protocol):

    def __init__(self):
        self.server_connection = reactor.connectTCP('192.168.254.101', 1000, GateFactory())
...

class Gate(protocol.Protocol):

    def dataReceived(self, recd):
        print recd
...

class GateFactory(protocol.ClientFactory):

    def buildProtocol(self):
        return Gate()
...

class PlayerFactory(protocol.Factory):

    def __init__(self):
        self.players = {}

    def buildProtocol(self, addr):
        return Player(self.players)

...

reactor.listenTCP(1000, PlayerFactory())

我的问题

我通过执行以下操作将数据转发到服务器:

self.gate_connection.transport.write(packet)

但是我如何将响应从以下地址转发给客户端:

class Gate(protocol.Protocol):

    def dataReceived(self, recd):
        print recd

构造GateFactory ,需要向其传递一个对selfPlayer实例)的引用, GateFactory可以在buildProtocol创建它们的Gate实例时将其传递给其引用。

(此外,请考虑使用HostnameEndpoint而不是直接使用connectTCP 。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM