## page was renamed from Twisted
= Twisted =
Twisted is an event-driven networking engine written in Python and licensed under the open source  MIT license.

== 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 ==
{{{
# 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()
}}}