john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

twisted async network framework install and intro echo

Twisted (python async event driven network platform) Installation

    http://www.python.org/getit/  (http://www.python.org/ftp/python/2.7.4/python-2.7.4.amd64.msi  )
    easy_install.exe http://pypi.python.org/packages/2.7/z/zope.interface/zope.interface-4.0.5-py2.7-win-amd64.egg
    http://twistedmatrix.com/Releases/Twisted/13.0/Twisted-13.0.0.win-amd64-py2.7.msi
    (Should say python 2.7 from the registry - if you do not see this then your Python Interpreter was not detected)
    http://slproweb.com/products/Win32OpenSSL.html      (includes a win64 version but don't forget Visual C++ 2008 Redistributables (x64))
    INCOMPLETE: maybe needs gnu openssl?


# linux may require sudo apt-get install build-essential python-dev

python --version            # yay, version 2.7.3
pip install twisted     # yay, version 13  (may require apt-get install gcc python-dev) OR error: Unable to find vcvarsall.bat (use windows notes above)
python
>>> import twisted
>>> twisted.__version__     # yay, version 13

>>> import OpenSSL   # ImportError: No module named OpenSSL = sudo apt-get install openssl python-openssl
>>> import twisted.internet.ssl  # ubuntu 12.04 has this by default

# ubu 12.04 has pycrypto version 2.4.1  (import Crypto , Crypto.__version__ ) , if you need Crypto.Signature upgrade manually to 2.6
# wget http://ftp.dlitz.net/pub/dlitz/crypto/pycrypto/pycrypto-2.6.tar.gz , tar -xf pycrypto-2.6.tar.gz , python setup.py install

>>> import Crypto
>>> import twisted.conch.ssh.transport  # this fails as ubu 12.04 is not Twisted 13? Conch? ahhh, pyasn1

pip install pyasn1   # OR the hard way from source: pyasn1.sourceforge.net , maybe O'Reilly and Twisted should note this dependency
>>> twisted.conch.ssh.transport.md5  # now this line should work too!  (it's not an import so the interpreter returns <built-in function openssl_md5> )



- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
twisted-server-example.py

from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import Factory

class Echo( Protocol ):
    def dataReceived( self, data ):
        self.transport.write( data )

class EchoFactory( Factory ):
    def buildProtocol( self, addr ):
        return Echo()

reactor.listenTCP( 8000, EchoFactory() )
reactor.run()

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
twisted-client-example.py

from twisted.internet import reactor
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import ClientFactory

class EchoClient( Protocol ):
    def connectionMade( self ):
        self.transport.write( "hello world" )

    def dataReceived( self, data ):
        print "Server response:", data
        self.transport.loseConnection()

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


class EchoFactory( ClientFactory ):
    protocol = EchoClient

    def clientConnectionFailed( self, connector, reason ):
        print "Connection failed - goodbye!"
        reactor.stop()

    def clientConnectionLost( self, connector, reason ):
        print "Connection lost - goodbye!"
        reactor.stop()

####################################################

if __name__ == '__main__':
    echoFactory = EchoFactory()
    reactor.connectTCP( "localhost" , 8000 , echoFactory )
    reactor.run()

  • « Arandr save config permanent customizations
  • chef solo ruby block mysql version »

Published

Jul 24, 2013

Category

python-twisted

~305 words

Tags

  • and 29
  • async 1
  • echo 8
  • framework 3
  • install 58
  • intro 9
  • network 20
  • python 180
  • twisted 20