[Twisted-Python] Writing onto txWS

vinod kumar vk.86.811 at gmail.com
Fri Jun 29 10:52:06 EDT 2012

Hi All,

   I have run and edited the example provided for txWS. The writing part
from server to client browser is shown only as self.transport.write()
inside dataReceived function in websockets.py. how do I write on to the
socket i.e. on to the browser, from a different function inside server
code(server.tac). I'm just starting another xmlrpc server on a thread where
I receive some notifications. I need to forward the same to socket. The
bold point below  is where I want to write on to socket. How can we do
that? Please help me out.
import time

from twisted.internet import reactor
from twisted.application import internet
from twisted.application.service import Application, Service
import xmlrpclib
from txws import WebSocketFactory

from src.protocols.websockets import WebSocketServerFactory
from constants import *

class WebSocketService(Service):
    A simple service that listens on port 8077 for WebSockets traffic.
    def __init__(self):
        self.start_time = time.time()

    def start_service(self, application):
        Gets the show on the road. Fires up a factory, binds the port.
        echofactory = WebSocketServerFactory(self)
        factory = WebSocketFactory(echofactory)
        ws_server = internet.TCPServer(8077, factory)

    def shutdown(self):
        Gracefully shuts down the service.
        reactor.callLater(0, reactor.stop)

def listDevices(id):
print "listDevice: %(id)s" % {"id": str(id)}
 return None

def event(id, address, key, value):
*        #I need to write the below print statement on to browser *
        print "event: %(id)s: %(address)s: %(key)s = %(value)s" % {"id":
"address": str(address), "key": str(key), "value": str(value)}

def aSillyBlockingMethod(x):
    import time
    import xmlrpclib
    from SimpleXMLRPCServer import SimpleXMLRPCServer
    from SimpleXMLRPCServer import SimpleXMLRPCRequestHandler
    from constants import *
    server = SimpleXMLRPCServer((SERVER_IP, SERVER_PORT), logRequests=True,
    print "server started at %s" % str(SERVER_URL)
    print 'function also got registered'

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://twistedmatrix.com/pipermail/twisted-python/attachments/20120629/f2581a71/attachment-0001.htm 

More information about the Twisted-Python mailing list