Skip to content
This repository
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 53 lines (40 sloc) 1.343 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
import sys

from twisted.enterprise import adbapi
from twisted.internet import ssl, reactor
from twisted.internet.protocol import Factory, Protocol
from twisted.internet import threads

class Echo(Protocol):

   def connectionMade(self):
      print "connection made"
      self.send()

   def connectionLost(self, reason):
      print "connection lost"

   def dataReceived(self, data):
      print "received: ", len(data)

   def send(self):
      html = """<!DOCTYPE html><html><body><h1>Foobar</h1></body></html>"""
      raw = html.encode("utf-8")
      response = "HTTP/1.1 200 OK\x0d\x0a"
      response += "Content-Type: text/html; charset=UTF-8\x0d\x0a"
      response += "Content-Length: %d\x0d\x0a" % len(raw)
      response += "\x0d\x0a"
      response += raw
      self.transport.write(response)


if __name__ == "__main__":

   factory = Factory()
   factory.protocol = Echo
   port = 8090

   if 'defer' in sys.argv:
      print "deferToThread"
      def foo():
         return 23
      d = threads.deferToThread(foo)
   else:
      print "no deferToThread"

   if 'ssl' in sys.argv:
      print "running SSL on", port
      reactor.listenSSL(port, factory, ssl.DefaultOpenSSLContextFactory('server.key', 'server.crt'))
   else:
      print "running plain TCP on", port
      reactor.listenTCP(port, factory)

   reactor.run()
Something went wrong with that request. Please try again.