Differences between revisions 1 and 14 (spanning 13 versions)
Revision 1 as of 2005-12-05 11:54:41
Size: 2400
Editor: dsl081-199-203
Comment:
Revision 14 as of 2008-09-17 18:05:21
Size: 3183
Editor: static-71-116-126-206
Comment: code in example did not work.
Deletions are marked like this. Additions are marked like this.
Line 3: Line 3:
This page maintained by ["Mathai"]. This page maintained by Jacob ["Mathai"].
Line 5: Line 5:
http://jacobmathai.blogspot.com http://www.jacobmathai.org
Line 7: Line 7:
  * Twisted is an open source network framework written entirely in Python. It allows you to create a SMTP, HTTP, proxy and ssh servers in python with minimal effort. It is Asynchronous and event driven and allows applications to respond to different network connection without the use of traditional threading models.   * [http://twistedmatrix.com/projects/core/ Twisted ] is an open source network framework written entirely in [http://www.python.org Python]. It allows you to create a SMTP, HTTP, proxy and ssh servers in [http://www.python.org Python] with minimal effort. Twisted is '''Asynchronous''' and event driven and allows applications to respond to different network connection without the use of traditional threading models.
Line 9: Line 9:
  * Be Sure to Download the [http://twistedmatrix.com/projects/core/ Twisted Sumo 2.1] (main Twisted package),[http://pyopenssl.sourceforge.net/ PyOpenSSL ], and [http://www.amk.ca/python/code/crypto.html PyCrypto] (cryptographic libraries and primitives for ssh connections) before running these Python Twisted examples.   * Be Sure to Download the '''[http://twistedmatrix.com/projects/core/ Twisted Sumo 2.1]''' (main Twisted package),'''[http://pyopenssl.sourceforge.net/ PyOpenSSL ]''', and '''[http://www.amk.ca/python/code/crypto.html PyCrypto]''' (cryptographic libraries and primitives for ssh connections) before running these Python Twisted examples.
Line 11: Line 11:
  * Install the ZopeInterface that is bundled with the Twisted Sumo download before installing Twisted. (cd ZopeInterface* && python setup.py install)   * Install the '''ZopeInterface''' that is bundled with the Twisted Sumo download before installing Twisted. (cd ZopeInterface* && python setup.py install)
Line 13: Line 13:
  * Install Twisted. (python setup.py install)   * Install '''Twisted'''. (python setup.py install)
Line 15: Line 15:
  * Install PyOpenSSL. (make sure you have base [http://www.openssl.org openssl] installed on the system) If during the PyOpenSSL install, you see any errors about Kerberos header files, try this:   * Install '''PyOpenSSL'''. (make sure you have base '''[http://www.openssl.org openssl]''' installed on the system) If during the PyOpenSSL install, you see any errors about Kerberos header files, try this:
Line 17: Line 17:
If your OpenSSL header files are not in /usr/include, you may need to supply
the -I flag to let the setup script know where to look. The same goes for the libraries of course, use the -L flag. Note that build won't accept these
flags, so you have t o run first build_ext and then build! Example:
If your OpenSSL ore kerberos header files are not in /usr/include, you may need to supply the -I flag to let the setup script know where to look. The same goes for the libraries of course, use the -L flag. Note that build won't accept these flags, so you have t o run first build_ext and then build! Example:
Line 28: Line 26:
  * Install PyCrypto (python setup.py install)
  * Verify that all the package are installed correctly by importing pyOpenSSL-0.7, twisted, Crypto from a python shell and we are ready to go.
  * Install '''Pycrypto'''. (python setup.py install)

* Verify that all the package are installed correctly by '''import'''ing '''pyOpenSSL-0.7, twisted, Crypto''' from a python shell and we are ready to go.
Line 40: Line 39:
        protocol = proxy.Proxy     def __init__(self):
        http.HTTPFactory.__init__(self)
        self.protocol = proxy.Proxy
Line 46: Line 47:
  * Execute the above Twisted python example script (python proxy.py)   * Execute the above Twisted python example script (python proxy.py) You may see a Deprecation Warning that you can ignore for this example.
Line 48: Line 49:
  * Connect your internet browser to the proxy server you just created (localhost:8080) and you have written a simple proxy server using Python and Twisted. {{{
[jacob@tux]$ python proxy.py
/usr/lib/python2.3/site-packages/twisted/web/proxy.py:22: DeprecationWarning: twisted.protocols.http has moved to twisted.web.http. See http://twistedmatrix.com/projects/web.
  from twisted.protocols import http
2005/12/06 21:47 EST [-] Log opened.
2005/12/06 21:47 EST [-] __main__.ProxyFactory starting on 8080
2005/12/06 21:47 EST [-] Starting factory <__main__.ProxyFactory instance at 0xb73577ac>

}}}

  * Connect your internet browser to the proxy server you just created '''(localhost:8080)''' and you have written a '''simple proxy server''' using Python and Twisted.

Python Twisted Examples

This page maintained by Jacob ["Mathai"].

http://www.jacobmathai.org

If your OpenSSL ore kerberos header files are not in /usr/include, you may need to supply the -I flag to let the setup script know where to look. The same goes for the libraries of course, use the -L flag. Note that build won't accept these flags, so you have t o run first build_ext and then build! Example:

  • python setup.py build_ext -I/usr/kerberos/include (or wherever your header files are) python setup.py build python setup.py install
  • Install Pycrypto. (python setup.py install)

  • Verify that all the package are installed correctly by importing pyOpenSSL-0.7, twisted, Crypto from a python shell and we are ready to go.

Simple HTTP Proxy Server (proxy.py)

from twisted.web import proxy, http
from twisted.internet import reactor
from twisted.python import log
import sys
log.startLogging(sys.stdout)
 
class ProxyFactory(http.HTTPFactory):
    def __init__(self):
        http.HTTPFactory.__init__(self)
        self.protocol = proxy.Proxy
 
reactor.listenTCP(8080, ProxyFactory())
reactor.run()
  • Execute the above Twisted python example script (python proxy.py) You may see a Deprecation Warning that you can ignore for this example.

[jacob@tux]$ python proxy.py
/usr/lib/python2.3/site-packages/twisted/web/proxy.py:22: DeprecationWarning: twisted.protocols.http has moved to twisted.web.http. See http://twistedmatrix.com/projects/web.
  from twisted.protocols import http
2005/12/06 21:47 EST [-] Log opened.
2005/12/06 21:47 EST [-] __main__.ProxyFactory starting on 8080
2005/12/06 21:47 EST [-] Starting factory <__main__.ProxyFactory instance at 0xb73577ac>
  • Connect your internet browser to the proxy server you just created (localhost:8080) and you have written a simple proxy server using Python and Twisted.

Twisted-Examples (last edited 2012-01-09 13:41:21 by wireless-72-237-55-2)

Unable to edit the page? See the FrontPage for instructions.