我的代码示例如下。我想在程序的各个点任意发送数据。Twisted 似乎非常适合聆听然后做出反应,但我如何简单地发送数据。
from twisted.internet.protocol import DatagramProtocol
from twisted.internet import reactor
import os
class listener(DatagramProtocol):
def __init__(self):
def datagramReceived(self, data, (host, port)):
print "GOT " + data
def send_stuff(data):
self.transport.write(data, (host, port))
reactor.listenUDP(10000, listener())
reactor.run()
##Some things happen in the program independent of the connection state
##Now how to I access send_stuff