Lambda the Ultimate

inactiveTopic Twisted Matrix Networking for Python
started 12/18/2003; 6:44:22 PM - last post 12/22/2003; 5:49:55 PM
Mark Evans - Twisted Matrix Networking for Python  blueArrow
12/18/2003; 6:44:22 PM (reads: 7947, responses: 11)
Twisted Matrix Networking for Python

Twisted Matrix is a Python framework for networking. The best summary is the paper quoted below. Twisted offers tantalizing extras which reflect inspirations from E and vague similarities to Oz: the Perspective Broker and Deferred objects ("A Deferred instance is a promise that a function will at some point in the future have a result...").

Twisted is a high-level networking framework that is built around event-driven asynchronous I/O. It supports TCP, SSL, UDP and other network transports. Twisted supports a wide variety of network protocols (including IMAP, SSH, HTTP, DNS). It is designed in a way that makes it easy to use with other event-driven toolkits such as GTK+, Qt, Tk, wxPython and Win32. Implemented mostly in Python, Twisted makes it possible to create networked applications without having to worry about low-level platform specific details. However, unlike many other network toolkits, Twisted still allows developers to access platform specific features if necessary.

There is a large amount of freely-available networking code on the internet today.... However, few frameworks endeavor to Twisted's scope and generality, and none that they authors are aware of provide as much integrated functionality as it does.


Posted to Python by Mark Evans on 12/18/03; 6:45:02 PM

Mark Evans - Re: Twisted Matrix Networking for Python  blueArrow
12/18/2003; 7:09:58 PM (reads: 815, responses: 0)

Experienced C programmers might scoff at Python as a serious protocol development tool. However low-level work is possible in Python via its struct module, which gives precise bytewise control over data layout, complete with endianness control, and with full platform independence. (The hexlify function from the binascii module helps debugging.) C-to-Python interfacing is also possible, but I found Python's struct module adequate to translate between binary packets and Python-friendly data structures. Twisted also sports low-level access to Internet headers, if you need them.

Of course, I advocate text protocols, not binary ones. Twisted easily supports them. A common category is that of line-oriented command handling (LineReceiver).

Frank Atanassow - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 4:31:27 AM (reads: 733, responses: 1)
Of course, I advocate text protocols, not binary ones.

Why?

Bart van der Werf (Bluelive) - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 4:44:25 AM (reads: 723, responses: 1)
Most internet protocols are textbased, and thats seems to work pretty well. It makes developing clients and servers easier, because you can just read the stream in plain text and see whats going on. And aslong as we keep textencoding out of the picture, text protocols are system independant, no endian problems for example.

Daniel Yokomiso - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 5:21:33 AM (reads: 720, responses: 0)
And aslong as we keep textencoding out of the picture, text protocols are system independant, no endian problems for example.


And as long as we keep endianess out of the picture, binary protocols are system independent, no ascii->ebcdic problems for example ;)

Seriously, if the protocol is well designed it'll be as simple as possible to parse, doesn't matter if it's binary or text-base. OTOH lame protocols are awful in both forms. The nice thing about text protocols is that often they provide can be parsed visually, while binary protocols usually can't. IMHO while it's nice to glance a stream of data and see if there's something malformed, our perception usually hides/corrects incorrect data (e.g. missing duplicated subsequent words separated by line breaks, insert end tokens), so I always end up writing a simple parser to check and pretty-print the stream, as I do with binary protocols.

Sjoerd Visscher - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 8:42:10 AM (reads: 660, responses: 0)

Of course, I advocate text protocols, not binary ones.

Why?

http://www.faqs.org/docs/artu/ch05s01.html

Luke Gorrie - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 10:46:36 AM (reads: 618, responses: 0)
Many binary protocols can be "dissected" and presented in a nice way by ethereal ("Sniffing the glue that holds the internet together"). This is a really nice tool, particularly since (as noted by Eric Raymond) it really does make sense to make some protocols compact and binary (e.g. X11, ethernet, IP).

One nasty (but amusing) side-effect is that by running Ethereal on your computer, you typically capture all packets received by your network card. This is bad from a security perspective, because you are running yet another C server program that is processing packets that might otherwise be ignored (dropped by kernel for being malformed or rejected by firewalling). There have been cases of buffer-overruns in ethereal which open up the risk of having your machine cracked by packets that would otherwise be dropped by your kernel.

The moral seems to be the usual one: don't write those programs in C.

An analogous problem is that adding new networking code to your kernel is very easy nowadays. It takes an afternoon to learn enough iptables to do a new form of packet filtering/mangling. Many people are doing this, and unfortunately the standard C programming mistakes they (well, we) make then cause kernel panics instead of a "mere" "Segmentation fault (core dumped)".

The moral I draw from this is that putting networking code in the kernel is usually a premature optimization. Unfortunately, it is much easier to do it this way, at least at present on Linux. And as far as I know the performance implications of doing this in user-space on Linux have not been thoroughly analysed (?).

Mark Evans - Re: Twisted Matrix Networking for Python  blueArrow
12/19/2003; 12:14:28 PM (reads: 586, responses: 0)

Hi Frank, the answer to the why question is experience. Text has proven itself superior from almost every point of view, particularly debugging, which is where protocol projects often bog down. Ethereal and similar hex dumpers leave unsolved the problem of human readability. The typical solution is to pretty-print on an ad-hoc, custom basis (per Daniel). So ultimately, one deals with text anyway, and protocols might as well begin as text. Usually the opposition to text emanates from engineers uncomfortable writing parsers or, less frequently, misguided about the nature and merits of data compression.

LtU is a language board, not a protocol board. What do folks think about the Perspective Broker for quote-unquote spreadable computing, and Deferreds? Are we in Oz territory here?

Peter Van Roy - Re: Twisted Matrix Networking for Python  blueArrow
12/20/2003; 10:34:22 AM (reads: 497, responses: 1)
Text has proven itself superior from almost every point of view, particularly debugging, which is where protocol projects often bog down.

Well, the Erlang people certainly disagree with this. There is an extension for Erlang, UBF, to support binary properly in the language (e.g., with binary pattern matching), and they are quite happy with it. See Appendix C in Joe Armstrong's thesis which explains how to do protocols with UBF.

Sjoerd Visscher - Re: Twisted Matrix Networking for Python  blueArrow
12/20/2003; 12:00:44 PM (reads: 496, responses: 0)
UBF seems to have the same problem as most binary protocols: There doesn't seem to be standard way to transmit strings in other encodings than ASCII.

Mark Evans - Re: Twisted Matrix Networking for Python  blueArrow
12/20/2003; 1:07:42 PM (reads: 483, responses: 0)

PVR - I'd rather not dwell on the binary/text debate. It was not the theme of my post. Armstrong's thesis echoes tired old arguments - ease of parsing and text verbosity. The only text alternative he examines is XML, justifying such limited scope with a breezy assertion, "Out of this mess a single universal panacea [XML] has emerged." Well, if the solution space is the set of pre-existing universal panaceas, then UBF has no standing, either. We might be stuck with, say, HDF, the last hurrah of binary do-it-all formats.

Having said all that, I respect Armstrong's effort for what it is, and have known about it for some time. Sadly, I have seen UBF virtually duplicated in half a dozen industrial settings (in the present context, see the Twisted package). Conversely, a few of my most successful projects have entailed simple parsers to facilitate textual transmission of data that was previously binary. It just makes the work go much, much faster, and no one will ever convince me otherwise. I've been there.

Rich Dougherty - Re: Twisted Matrix Networking for Python  blueArrow
12/22/2003; 5:49:55 PM (reads: 359, responses: 0)

I've had a bit of a play with the Twisted Matrix web package. I was really impressed at how easy it was to construct a simple web application. At first I used the framework that they provided, then I had a go at making my own, which was also surprisingly easy.

The component architecture seems very nice. While, I had been familiar with the Adapter pattern, I hadn't been exposed to anything like Twisted's AdapterRegistry before.

The biggest thing I will have to get used to is the fact that the framework is designed to run in a single thread (which I am not used to). However they do seem to provide a lot of support for this style of programming, so I should be ok once I am used to it.