簡體   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