Archive

Archive for the ‘twisted’ Category

Getting started with Twisted on Windows

March 10, 2012 Leave a comment

Twisted is an excellent networking framework for python applications, and like most of python, your code is easily portable from one platform to another. As an event-driven framework, you can handle very high loads without creating a large number of threads, so it scales quite nicely. Twisted comes preinstalled on recent versions of Mac OS X, and on Linux distros you can get it with your system’s package manager. Windows is a bit more work, so I’ll go through the steps.

The prerequisites:

Python – http://python.org/download
Setuptools – http://pypi.python.org/pypi/setuptools (needed to install the Zope Interface egg)
Zope Interface – http://pypi.python.org/pypi/zope.interface#download (used by twisted for its service interfaces)
Twisted – http://twistedmatrix.com

1. Install Python from http://python.org/download/. There are Windows installers aplenty.
2. Install setuptools. There is an installer for 32-bit windows. For 64-bit, just download the ez_setup.py locally, and then run “c:\Python27\python.exe c:\Users\me\Downloads\ez_setup.py”
3. Zope is a little tricky. When you installed setuptools, it added the easy_install.exe under your python\scripts directory. To install the python .egg file containing zope, you need to get the URL for the zope egg you want to download and pass that as a parameter to easy_install.exe, like this:

c:\Python27\Scripts\easy_install.exe http://pypi.python.org/packages/2.4/z/zope.interface/zope.interface-3.8.0-py2.4-win32.egg

If all goes well, you should be ready to install and run twisted.

4. Install twisted from http://twistedmatrix.com

At this point, you should be able to fire up python and run the sample from the Twisted home page:

from twisted.internet import protocol, reactor

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

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

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

I hope this helps you getting started with the excellent twisted network framework for your python apps.

Advertisements
Categories: python, twisted Tags: , ,