如果我的理解是正确的,那么文档中的此示例只能写一次“ hello”:
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
class Helloer(DatagramProtocol):
def startProtocol(self):
host = "192.168.1.1"
port = 1234
self.transport.connect(host, port)
print "now we can only send to host %s port %d" % (host, port)
self.transport.write("hello") # no need for address
def datagramReceived(self, data, (host, port)):
print "received %r from %s:%d" % (data, host, port)
# Possibly invoked if there is no server listening on the
# address to which we are sending.
def connectionRefused(self):
print "No one listening"
# 0 means any port, we don't care in this case
reactor.listenUDP(0, Helloer())
reactor.run()
我有一些疑问:
收到数据报后写“ hello”的好方法是什么?打电话startProtocol()
的datagramReceived()
?
假设在接收到数据报之后要写入另一个消息,例如“有人在家吗?”。是否应该AnyoneHome(DatagramProtocol)
实施课程?但是,如何将它“链接”到Helloer
反应堆并连接到反应堆呢?
谢谢
解决了。像我的容貌可能只是调用self.transport.write()
在datagramReceived()
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句