Twisted is Easy!  Moshe Zadka
Do Nothing  from twisted.internet import reactor  reactor.run()
Protocol Parsing   from twisted.internet import reactor, protocol   class Echo(protocol.Protocol):       def dataReceived(...
Web Development  from twisted.internet import reactor  from twisted.web import resource, server  class Resource(resource.R...
Multi-protocol Servers (Classes)   from twisted.internet import reactor, protocol   from twisted.web import resource, serv...
Multi-protocol Servers (Set-up)   factory = protocol.Factory()   factory.protocol = ByteCount   factory.counter = Counter(...
Upcoming SlideShare
Loading in …5
×

Twisted is easy

1,870 views

Published on

Slides for lightning talk -- Twisted is easy

Published in: Technology

Twisted is easy

  1. 1. Twisted is Easy! Moshe Zadka
  2. 2. Do Nothing from twisted.internet import reactor reactor.run()
  3. 3. Protocol Parsing from twisted.internet import reactor, protocol class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) factory = protocol.Factory() factory.protocol = Echo reactor.listenTCP(1033, factory) reactor.run()
  4. 4. Web Development from twisted.internet import reactor from twisted.web import resource, server class Resource(resource.Resource): isLeaf = True users = 0 def render_GET(self, request): self.users += 1 return "Welcome user number %d" % self.users reactor.listenTCP(1080, server.Site(Resource())) reactor.run()
  5. 5. Multi-protocol Servers (Classes) from twisted.internet import reactor, protocol from twisted.web import resource, server class Counter(object): count = 0 def add(self, number): self.count += number class ByteCount(protocol.Protocol): def dataReceived(self, data): self.factory.counter.add(len(data)) class Resource(resource.Resource): isLeaf = True counter = None def render_GET(self, request): return "Received %d bytes" % self.counter.count
  6. 6. Multi-protocol Servers (Set-up) factory = protocol.Factory() factory.protocol = ByteCount factory.counter = Counter() resource = Resource() resource.counter = factory.counter reactor.listenTCP(1033, factory) reactor.listenTCP(1080, server.Site(resource)) reactor.run()

×