[Twisted-Python] connecting with a FlashXML socket
Patrick Lauber
digi at treepy.com
Wed Mar 16 11:40:41 EST 2005
i use a \x00 as a terminator....
this script below has an other class changhandler that looks if there is
a new file in directory and sends that file (xml) to all connected
clients... it's used for a web radio to push the meta infos to the clients.
have fun
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import reactor
from xml.dom.minidom import parseString
import changehandler
class Radio(Protocol):
def connectionMade(self):
self.factory.clients.append(self)
self.transport.write(self.factory.msg)
self.ip=self.transport.getHost().host
print self.ip+' connected'
def dataReceived(self, data):
print data
try:
doc=parseString(data[0:-1])
if doc.nodeName == 'connect':
if doc.attributes.has_key('service'):
self.service=doc.attributes['service']._value
print "new service"+self.sevice
else:
print "connect was malformatted"
else:
pass
except:
print "parsing error"
def connectionLost(self, reason='connectionDone'):
print self.ip+' disconnected'
class XMLSocket(Factory):
clients=[]
msg='<hello>world</hello>\x00'
protocol = Radio
services = [["radio",Radio]]
def __init__(self, protocol=None):
self.protocol=protocol
reactor.callLater(0,self.sendMsg)
def sendMsg(self):
msg=changehandler.waiting()
if msg:
for client in self.clients:
client.transport.write(msg+"\x00")
reactor.callLater(0,self.sendMsg)
reactor.listenTCP(8000, XMLSocket(Radio))
reactor.run()
More information about the Twisted-Python
mailing list