0
最近,我已經採取了我的第一個刺在Twisted/Python構建一個應用程序,從TCP端口迴應傳入的UDP字符串。我認爲這將會非常簡單,但我一直無法實現。下面的代碼是修改爲一起運行的示例TCP & UDP服務器。我只是想在兩者之間傳遞一些數據。任何幫助,將不勝感激。扭曲的UDP到TCP橋
from twisted.internet.protocol import Protocol, Factory, DatagramProtocol
from twisted.internet import reactor
class TCPServer(Protocol):
def dataReceived(self, data):
self.transport.write(data)
class UDPServer(DatagramProtocol):
def datagramReceived(self, datagram, address):
#This is where I would like the TCPServer's dataReceived method run passing "datagram". I've tried:
TCPServer.dataReceived(datagram)
#But of course that is not the correct call because UDPServer doesn't recognize "dataReceived"
def main():
f = Factory()
f.protocol = TCPServer
reactor.listenTCP(8000, f)
reactor.listenUDP(8000, UDPServer())
reactor.run()
if __name__ == '__main__':
main()
我已經看過這個例子,但是我的理解是DatagramProtocol不能/不使用工廠。當我試圖解決這個問題時,就像你分享的例子一樣,我得到了UDPServer不喜歡被共享工廠引用的錯誤。換言之,UDP打破了「一個連接,另一個輸出」的例子。 – SaranWrap