## page was renamed from Twisted = Twisted = Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license. * https://twistedmatrix.com/trac/wiki * https://twistedmatrix.com/documents/current/web/howto/using-twistedweb.html == Install from source == * su * cd /tmp * wget [[http://twistedmatrix.com/Releases/Twisted/13.1/Twisted-13.1.0.tar.bz2]] * tar xvif Twisted-13.1.0.tar.bz2 * cd Twisted-13.1.0 * python2.7 setup.py build * python2.7 setup.py install == Test installation == * python * from twisted.internet import protocol, reactor * quit() == Slackbuild == * su * cd /tmp * wget http://slackbuilds.org/slackbuilds/14.1/python/python-twisted.tar.gz * tar xvzf python-twisted.tar.gz * cd python-twisted * wget https://pypi.python.org/packages/source/T/Twisted/Twisted-13.2.0.tar.bz2 * ./python-twisted.SlackBuild * installpkg /tmp/python-twisted-13.2.0-x86_64-1_SBo.tgz Slackware64 14: [[attachment:python-twisted-13.2.0-x86_64-1_SBo.tgz]] == Echo server == {{{#!highlight python from twisted.internet import protocol, reactor class Echo(protocol.Protocol): def dataReceived(self, data): self.transport.write(data) def connectionMade(self): print('Connection made') def connectionLost(self,reason): print('Connection lost') class EchoFactory(protocol.Factory): def buildProtocol(self, addr): return Echo() if __name__=='__main__': reactor.listenTCP(1234, EchoFactory()) reactor.run() }}} == Echo client == {{{#!highlight python import threading import time import socket class Client (threading.Thread): def __init__(self): threading.Thread.__init__(self) #required def run(self): for x in range(1,100): HOST = 'localhost' PORT = 1234 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) print('connecting...') s.connect((HOST, PORT)) print('sending config...') s.send('test') s.close() print('complete') if __name__=='__main__': clients=[] for x in range(1,100): clients.append( Client() ) for c in clients: c.start() }}} == Sample web server == {{{#!highlight python # from https://twistedmatrix.com/trac/ from twisted.web import server, resource from twisted.internet import reactor, endpoints class Counter(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 request.setHeader(b"content-type", b"text/plain") content = u"I am request #{}\n".format(self.numberRequests) return content.encode("ascii") endpoints.serverFromString(reactor, "tcp:8080").listen(server.Site(Counter())) reactor.run() }}} * apt-get instal apache2-utils * apt-get install nginx * apt-get install links === /etc/nginx/conf.d/load-balancer.conf === {{{ upstream backend { server 172.28.128.4:8080; server 172.28.128.3:8080; server 172.28.128.6:8080; } server { listen 8080; location / { proxy_pass http://backend; } } }}} == twistedtest.py == {{{#!highlight python from twisted.web import server from twisted.web import resource from twisted.internet import reactor from twisted.internet import endpoints import time import sys class Handler(resource.Resource): isLeaf = True numberRequests = 0 def render_GET(self, request): self.numberRequests += 1 time.sleep(0.1) # wait 100 ms request.setHeader(b"content-type", b"text/plain") content = u"I am request #{}\n".format(self.numberRequests) return content.encode("ascii") if __name__=='__main__': port = sys.argv[1] print('Listening on port %s'%(port)) endpoints.serverFromString(reactor,"tcp:%s" %(port) ).listen(server.Site(Handler())) reactor.run() }}} == twistedclient.py == {{{#!highlight python import threading import urllib2 import time import sys class Client(threading.Thread): def __init__(self,urls,nrReqs): threading.Thread.__init__(self) #required self.urls=urls self.nrReqs=nrReqs self.count=0 def run(self): loop=True urlIdx=0 while loop: url=urls[urlIdx] conn=urllib2.urlopen(url) resp=conn.read() if self.count < self.nrReqs-1: self.count+=1 urlIdx+=1 if urlIdx>=len(urls): urlIdx=0 else: loop=False if __name__=='__main__': if len(sys.argv) == 4: nrUrlsToUse = int(sys.argv[1]) nrClients=int(sys.argv[2]) nrReqs=int(sys.argv[3]) start = time.time() urls=['http://localhost:8080/','http://localhost:8081/','http://localhost:8082/','http://localhost:8083/','http://localhost:8084/','http://localhost:8085/','http://localhost:8086/','http://localhost:8087/','http://localhost:8088/','http://localhost:8089/','http://localhost:8090/','http://localhost:8091/','http://localhost:8092/','http://localhost:8093/','http://localhost:8094/','http://localhost:8095/','http://localhost:8096/','http://localhost:8097/','http://localhost:8098/','http://localhost:8099/'] if nrUrlsToUse ') }}}