- Twisted 21.2.0
- Navigation
- Project links
- Statistics
- Maintainers
- Classifiers
- Project description
- What is this?
- Installing
- Documentation and Support
- Unit Tests
- Static Code Checkers
- Copyright
- Warranty
- Christian Long’s Blog
- Mar 25, 2015
- Twisted on Windows, 2015 Edition
- Intro¶
- Install Python¶
- Install a compiler¶
- Upgrade pip and virtualenv¶
- Set up a virtual environment¶
- The state of Python packaging in 2015¶
- Install Twisted¶
- Install dependencies from PyPI¶
- Install dependencies that are not on PyPI¶
- Twisted Application¶
- Install your application¶
- Run it¶
- Up and running¶
- Downloads
- Get Started with Pip
- Download Direct from PyPI
- Optional Dependencies
- Community
- See the code for Twisted ( and more) on GitHub
- Read our blog
- Join the discussion list
- Come chat with us on IRC
- Report a 🔒security issue
- Ask on Stack Overflow
- Follow us on Twitter
- Donate to Twisted
- Twisted Sponsors
- Golden Tree Snake
- Silver Sidewinder
- What is Twisted?
- Code Examples
- More Protocols
- Community
Twisted 21.2.0
pip install Twisted Copy PIP instructions
Released: Feb 28, 2021
An asynchronous networking framework written in Python
Navigation
Project links
- Homepage
- Documentation
- Source
- Issues
Statistics
View statistics for this project via Libraries.io, or by using our public dataset on Google BigQuery
License: MIT
Requires: Python >=3.5.4
Maintainers
Classifiers
- Programming Language
- Python :: 3
- Python :: 3 :: Only
- Python :: 3.5
- Python :: 3.6
- Python :: 3.7
- Python :: 3.8
- Python :: 3.9
Project description
For information on changes in this release, see the NEWS file.
What is this?
Twisted is an event-based framework for internet applications, supporting Python 3.5+. It includes modules for many different purposes, including the following:
- twisted.web: HTTP clients and servers, HTML templating, and a WSGI server
- twisted.conch: SSHv2 and Telnet clients and servers and terminal emulators
- twisted.words: Clients and servers for IRC, XMPP, and other IM protocols
- twisted.mail: IMAPv4, POP3, SMTP clients and servers
- twisted.positioning: Tools for communicating with NMEA-compatible GPS receivers
- twisted.names: DNS client and tools for making your own DNS servers
- twisted.trial: A unit testing framework that integrates well with Twisted-based code.
Twisted supports all major system event loops – select (all platforms), poll (most POSIX platforms), epoll (Linux), kqueue (FreeBSD, macOS), IOCP (Windows), and various GUI event loops (GTK+2/3, Qt, wxWidgets). Third-party reactors can plug into Twisted, and provide support for additional event loops.
Installing
To install the latest version of Twisted using pip:
Additional instructions for installing this software are in the installation instructions.
Documentation and Support
Twisted’s documentation is available from the Twisted Matrix website. This documentation contains how-tos, code examples, and an API reference.
Help is also available on the Twisted mailing list.
There is also a pair of very lively IRC channels, #twisted (for general Twisted questions) and #twisted.web (for Twisted Web), on chat.freenode.net.
Unit Tests
Twisted has a comprehensive test suite, which can be run by tox:
You can test running the test suite under the different reactors with the TWISTED_REACTOR environment variable:
Some of these tests may fail if you:
- don’t have the dependencies required for a particular subsystem installed,
- have a firewall blocking some ports (or things like Multicast, which Linux NAT has shown itself to do), or
- run them as root.
Static Code Checkers
You can ensure that code complies to Twisted coding standards:
Or, for speed, use pre-commit directly:
Copyright
All of the code in this distribution is Copyright (c) 2001-2021 Twisted Matrix Laboratories.
Twisted is made available under the MIT license. The included LICENSE file describes this in detail.
Warranty
THIS SOFTWARE IS PROVIDED “AS IS” WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS TO THE USE OF THIS SOFTWARE IS WITH YOU.
IN NO EVENT WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR REDISTRIBUTE THE LIBRARY, BE LIABLE TO YOU FOR ANY DAMAGES, EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
Again, see the included LICENSE file for specific legal details.
Christian Long’s Blog
Mar 25, 2015
Twisted on Windows, 2015 Edition
Intro¶
Installing and configuring Twisted on Windows has gotten easier over the years. Here is how to do it in 2015, using the latest packaging tools.
Twisted runs well on Windows, in my experience. I moved some Twisted applications from Linux to Windows for a client, and they have been running happily for months now without issue.
These instructions have been tested with Windows 8.1 and Windows Server 2012 R2. The applications I run on Windows use Twisted.Web to serve single-page web apps, talking to a database using pyodbc. My ODBC driver is 32-bit, so I’m using 32-bit Python for these instructions. However, Twisted does not depend on pyodbc, so don’t install it if you don’t need it.
These instructions assume that you already have an application that runs on Twisted. The Twisted documentation has a good explanation of how to set up your project so it runs as a Twisted Applicaion. It also includes an nice selection of example applications, if you don’t have an existing Twisted application.
Install Python¶
Twisted requires Python 2. Install the latest version of Python 2.7 (direct link to the 32-bit installer). Run the installer, and in the installer dialog, choose the option to add python to the path.
Install a compiler¶
Some of Twisted’s dependencies have C extensions, and are not available from the Python Package Index in the binary wheel format. So, we need a compiler to compile them from source. This used to be tricky, but fortunately, Microsoft now provides a free download that makes it easy. Download the Microsoft Visual C++ Compiler for Python 2.7. It may have to be installed as admin, to get around policy restrictions on compilers.
Upgrade pip and virtualenv¶
The Python 2.7.9 installer now includes pip and virtualenv, and sets them up for you by default. However, it does not come with the very latest pip and virtualenv. Here’s how to upgrade them to the latest versions.
Start an admin command prompt. On Windows 8 and newer, Win-x then a is a quick keyboard shortcut to open an admin command prompt.
Upgrade pip to the latest version.
Upgrade virtualenv to the latest version.
Now close the admin command prompt. We will be installing the rest of the packages in to a virtualenv, and that does not require admin access. The great advantage of using a virtualenv is that it keeps the packages we install isolated from each other, and from the system-wide packages.
Set up a virtual environment¶
Start a regular (non-admin) command prompt. Win-x then c is a quick keyboard shortcut for a non-admin command prompt.
This makes a new directory on the C: drive, makes a new virtualenv, and then activates the new virtualenv. You should see the name of the virtualenv in parentheses at the start of your command prompt, something like this:
When a virtualenv is activated, it looks for installed Python packages in its own site-packages directory C:\PythonEnvs\Example\Lib\site-packages , instead of looking in the system wide site-packages directory C:\Python27\Lib\site-packages . Note that we don’t need to be in the virtualenv directory for it to be active.
The virtualenvwrapper project is full of useful shortcuts for working with virtualenvs. For simplicity, I will be using only virtualenv, and not virtualenvwrapper, in this writeup. However, if you’re interested in setting up virtualenvwrapper, this patched version works on Windows with the latest version of virtualenv.
The state of Python packaging in 2015¶
Installing Python packages on Windows has gotten a lot easier over the years. The Python Package Index (PyPI) now provides pre-compiled binary installers in the wheel format for many packages.
When a wheel is not available, pip can automatically compile C extensions using this compiler that Microsoft provides at no cost.
However, there are still packages that are not available on PyPI. Many are distributed for Windows in the Windows installer format (.msi or .exe). Pip can not install these packages, but there is a way to install them in to a virtualenv.
Install Twisted¶
This will pull the latest version from PyPI. It will also install its dependencies. One dependency, zope.interface , will use the compiler to compile a C extension.
Install dependencies from PyPI¶
As of March 2015, pywin32 is available on PyPI in the wheel format. That means it can be installed by pip. Note that in order to get the PyPI version, we must tell pip to install package pypiwin32 , not pywin32 .
Install dependencies that are not on PyPI¶
For packages that are not on PyPI, the installation steps are different. If the package is distributed using a Windows binary installer (.msi or .exe) we can use the older easy_install command to install it in to a virtualenv.
One such package is pyodbc, which my application uses to talk to the database. Twisted itself does not depend on pyodbc, so there is no need to install it if your application doesn’t use it.
As of March 2015, pyodbc is not available in wheel format from PyPI. Download the Windows installer. Make sure to get the installer that matches your version of Python and your architecture. I am using this one “3.0.7 32-bit Windows Installer for Python 2.7”.
Use easy_install to install pyodbc in to the virtualenv from the executable Windows installer file.
I’ll talk about why we need —always-unzip in Part 3.
Not all installers will work with easy_install this way. See this Stack Overflow question for more details.
Twisted Application¶
In this series, I’m assuming you have your project structured as a Twisted Application. In that structure, you have a .tac file that is the connection point between your code and the Twisted server, twistd .
Here a basic .tac file, taken from the documentation for Twisted Application.
If you don’t already have a project that you run under Twisted, the documentation has a nice set of examples to get you started.
Install your application¶
Whatever Twisted application you are going to be running on this server, install it as you normally would. For example:
Now that everything has been installed, check it.
should look something like this:
Newer versions of some of these packages may since have been released.
If you are installing using pip , make sure your .tac file is included in your distribution file. For exapmple, put this line in your MANIFEST.in file:
Run it¶
Try it out. Make sure your virtualenv is activated, and type:
The virtualenv picks up the right Python path, but on Windows we have to specify the full path to the twistd.py file. This command should give you a nice help message and no errors.
Now try running your app under Twisted.
For readability, I have broken this long command in to multiple lines using ^ , the dos line-continuation character.
This command should print out some lines showing the twistd server starting up. Again, on Windows, we have to specify the full path to the app install directory when starting the twistd server. Go try out your app, and press Ctrl-c to shut down the server when you’re done.
Up and running¶
That’s it for Part 1. We have installed Python, set up a virtualenv, and gotten your Twisted app up and running. In Part 2, we will set up a Windows service to run the app, using the virtual service account that was introduced in Windows Server 2008. In Part 3, we will look at setting the right privileges for the app. In Part 4, we will package the app and its dependencies for deployment to test and production servers. Thanks for reading, and if you have any questions or suggestions, let me know. I’m on Twitter at @christianmlong.
Downloads
Get Started with Pip
Download Direct from PyPI
Optional Dependencies
Community
See the code for Twisted ( and more) on GitHub
Read our blog
Join the discussion list
Come chat with us on IRC
Report a 🔒security issue
Ask on Stack Overflow
Follow us on Twitter
Donate to Twisted
Twisted Sponsors
Participate in the Twisted Project Sponsorship Program! For Silver Sidewinder and higher-level sponsors, we will display your logo here on the front page for one year.
Golden Tree Snake
Silver Sidewinder
What is Twisted?
Twisted is an event-driven networking engine written in Python and licensed under the open source MIT license. It supports CPython 3.5+ and PyPy3.
Code Examples
Twisted makes it easy to implement custom network applications. Here’s a TCP server that echoes back everything that’s written to it:
Learn more about writing servers, writing clients and the core networking libraries , including support for SSL, UDP, scheduled events, unit testing infrastructure, and much more.
Twisted includes an event-driven web server. Here’s a sample web application; notice how the resource object persists in memory, rather than being recreated on each request:
Here’s a simple publish/subscribe server, where clients see all messages posted by other clients:
You can test this out by opening two terminals and doing telnet localhost 1025 in each, then typing things.
Twisted includes a sophisticated IMAP4 client library.
Give this a try, supplying your IMAP4 username, app password ( generate one for gmail, generate one for fastmail), and client endpoint description for your IMAP4 server. You’ll see the subject of the first message in your mailbox printed.
See the TwistedMail documentation for more information.
Twisted includes an SSH client & server, «conch» (i.e.: the Twisted Shell).
You can use this client to run «hello world» on any SSH server that your local SSH agent can authenticate to, if you pass your username, host name, and optionally port number on the command line.
More Protocols
Twisted also supports many common network protocols, including SMTP, POP3, IMAP, SSHv2, and DNS. For more information see our documentation and API reference.
Community
- Get in touch with the Twisted community through email, Stack Overflow or IRC;
- Learn about the Twisted development process and how to contribute;
- Read about software using Twisted and their success stories;
- Find out what Twisted Matrix Laboratories is;
- Learn about the individuals and organisations that aid Twisted with donations of hardware, software, hosting and other things;
- Help improve Twisted on Windows!
Human beings, please do not send messages to this email address!