写作不只是"你好"

时间:2015-03-08 17:57:15

标签: python twisted

如果我的理解是正确的,那么文档中的这个例子只能写"你好"一旦:

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()

我有一些问题:

  1. 写什么好方法"你好"什么时候收到数据报?在startProtocol()中致电datagramReceived()

  2. 假设在收到数据报后要写入另一条消息,例如,#34;任何人回家?"是否应该实施课程AnyoneHome(DatagramProtocol)?但应该如何"链接"到Helloer并连接到反应堆?

  3. 由于

1 个答案:

答案 0 :(得分:1)

解决。看起来我可以在self.transport.write()中拨打datagramReceived()