Sending arbitrary data with Twisted

An example of my code is as follows. I would like to arbitrarly send data at various points in the program. Twisted seems great for listening and then reacting, but how to I simply send data.

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


Your example already includes some code that sends data:

def send_stuff(data): self.transport.write(data, (host, port))

In other words, the answer to your question is "call send_stuff" or even "call transport.write".

In a comment you asked:

#Now how to I access send_stuff

There's nothing special about how you "access" objects or methods when you're using Twisted. It's the same as in any other Python program you might write. Use variables, attributes, containers, function arguments, or any of the other facilities to maintaining references to objects.

Here are some examples:

# Save the listener instance in a local variable network = listener() reactor.listenUDP(10000, network) # Use the local variable to connect a GUI event to the network MyGUIApplication().connect_button("send_button", network.send_stuff) # Use the local variable to implement a signal handler that sends data def report_signal(*ignored): reactor.callFromThread(network.send_stuff, "got sigint") signal.signal(signal.SIGINT, report_signal) # Pass the object referenced by the local variable to the initializer of another # network-related object so it can save the reference and later call methods on it # when it gets events it wants to respond to. reactor.listenUDP(20000, AnotherDatagramProtocol(network))

And so on.


  • Remote debugging of a Java application launched as a Windows service
  • Is it good practice to put Edge Side Includes into my templates?
  • How can I do a 301 redirect from http to https in Wildfly 8.2?
  • Moving data between processes in Spartan 3
  • Paramiko SSHException Channel Closed
  • Android NFC read Tags issue. Activity starts each time on data received
  • openssl handshake failed
  • How to resolve docker host names (/etc/hosts) in containers
  • PHP get selected value of select form
  • How secure are apple APNS push notifications?
  • Powershell Hash Table to HTML
  • Generic classes with Collection getter of other types
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Compress a file with GZipStream while maintaining its meta-data
  • Cannot get text from text area
  • How to make R's read_csv2() recognise the text characters properly
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Why cepheus don't send int without quotes to orion?
  • Security issues with PHP's Readfile method
  • Jenkins: FATAL: Could not initialize class hudson.util.ProcessTree$UnixReflection
  • Access Android Market through SSH tunnel
  • How to run “Deployd” on port 80 instead of port 5000 in webserver.
  • iOS: Detect app start via notification press
  • Email verification using google app script and google forms
  • Get data from AJAX - How to
  • Using variable in a value field in jMeter
  • Email format validation in mvc3 view
  • CSS Linear-gradient formatting issue accross different browsers
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • How to add date and time under each post in guestbook in google app engine
  • Updating server-side rendering client-side
  • How to pass list parameters for each object using Spring MVC?
  • SVN: Merging two branches together
  • Hibernate gives error error as “Access to DialectResolutionInfo cannot be null when 'hibernate.
  • -fvisibility=hidden not passed by compiler for Debug builds
  • coudnt use logback because of log4j
  • How to CLICK on IE download dialog box i.e.(Open, Save, Save As…)
  • Can Visual Studio XAML designer handle font family names with spaces as a resource?
  • need help with bizarre java.net.HttpURLConnection behavior
  • sending mail using smtp is too slow