[Twisted-Python] socket communications in flash
Bob Ippolito
bob at redivi.com
Tue Jun 17 05:29:54 MDT 2003
On Thursday, Jun 12, 2003, at 20:00 America/New_York, e okyere wrote:
> I am looking at doing low-level sock comms with flash mx and hopefully
> c++
> and upon hitting this thread:
>
> http://twistedmatrix.com/pipermail/twisted-python/2002-May/001121.html
>
> thought I should contact you to see if you have any experience in that
> line.
> If so, I'd appreciate any resources you can share or point to.
This is similar to how I've done it in the past (warning: untested, not
commented, mostly rape & paste from other code I've written):
########
import sys, random, md5
from twisted.python.compat import True, False
from twisted.python import log
from twisted.protocols.basic import LineReceiver
from twisted.web import microdom
from twisted.internet.protocol import Factory
class XMLPipe(LineReceiver):
delimiter = '\0'
def connectionMade(self):
pass
def sendMessage(self, message):
self.transport.writeSequence(('<packet>', str(message),
'</packet>', self.delimiter))
def lineReceived(self, line):
if not line:
log.msg("WARNING: Empty message from client")
return
packet = microdom.parseString(line).childNodes[0]
if packet.nodeName != 'packet':
raise ValueError, '%r is not a packet tag' % packet
self.packetReceived(packet)
def packetReceived(self, packet):
raise NotImplementedError
def connectionLost(self, reason):
pass
class FlashPipe(XMLPipe):
def connectionMade(self):
self.authenticated = False
self.user = None
self.issueChallenge()
def packetReceived(self, packet):
for node in packet.childNodes:
self.nodeReceived(node)
def nodeReceived(self, node):
return getattr(self, 'handle_'+node.nodeName,
self.xmlerror)(node)
def xmlerror(self, node):
log.msg('Unrecognized Node %r %r' % (node, node.toxml()))
self.sendMessage('<error>Unrecognized Node</error>')
def issueChallenge(self):
# some entropy
self.pendingChallenge = md5.new(''.join((
hex(id(self)),
hex(random.randrange(sys.maxint)),
))).hexdigest()
self.sendMessage(''.join(
('<challenge>', self.pendingChallenge, '</challenge>'
)))
def handle_response(self, node):
resp = node.firstChild().nodeValue.strip()
for possible, user in self.factory.authorized.iteritems():
h = md5.new(self.pendingChallenge)
h.update(possible)
if h.hexdigest() == resp:
break
else:
return
log.msg('authenticated as %s' % user)
self.user = user
self.authenticated = True
self.sendMessage('<authenticated />')
class FlashXMLFactory(Factory):
noisy = 0
protocol = FlashPipe
authorized = {'somerandomdata' : 'valid user'}
if __name__=='__main__':
from twisted.internet import reactor
log.startLogging(sys.stdout)
reactor.listenTCP(8888, FlashXMLFactory())
reactor.run()
####
A conversation would look like this:
SERVER:
'<packet><challenge>d41d8cd98f00b204e9800998ecf8427e</challenge></
packet>\0'
CLIENT:
'<packet><response>f87504a987151d223335f0753f010b74</response></
packet>\0'
SERVER: '<packet><authenticated /></packet>\0'
-bob
More information about the Twisted-Python
mailing list