[英]Proxy Server with Python Twisted
我正在嘗試編寫執行以下操作的代理服務器:
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
,需要向其傳遞一個對self
( Player
實例)的引用, GateFactory
可以在buildProtocol
創建它們的Gate
實例時將其傳遞給其引用。
(此外,請考慮使用HostnameEndpoint
而不是直接使用connectTCP
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.