Accepting Connections from Clients

The previous labs in this chapter dealt with client connections, where an application initiated a connection to a remote server. However, Twisted can also be used for writing network servers, where the application waits for connections from clients. This lab will show you how to write a Twisted server that accepts connections from clients and interacts with them.

2.4.1. How Do I Do That?

Create a Protocol object defining your server's behavior. Create a ServerFactory object using the Protocol, and pass it to reactor.listenTCP. Example 2-7 shows a simple echo server that accepts a client connection and then repeats back all client messages.

Example 2-7.

from twisted.internet import reactor, protocol

from twisted.protocols import basic

class EchoProtocol(basic.LineReceiver):

 def lineReceived(self, line):

 if line == 'quit':


 self.transport.loseConnection( )


 self.sendLine("You said: " + line)

class EchoServerFactory(protocol.ServerFactory):

 protocol = EchoProtocol

if __name__ == "_ _main_ _":

 port = 5001

 reactor.listenTCP(port, EchoServerFactory( )) )

When you run this example, it will listen on port 5001, and report client connections as they are made:

 $ python

 Server running, press ctrl-C to stop.

 Connection from

 Connection from

In another terminal, use netcat, telnet, or the application from Example 2-6 to connect to the server. It will echo anything you type back to you. Type quit to close your connection:

 $ python localhost 5001

 Connected to server. Press ctrl-C to close connection.


 You said: hello

 twisted is fun

 You said: twisted is fun



 $ How does that work?

Twisted servers use the same Protocol classes as clients. To save some work, the EchoProtocol in Example 2-6 inherits from twisted.protocols.basic.LineReciever, which is a slightly higher-level implementation of Protocol. LineReceiver is a Protocol that automatically breaks its input into separate lines, making it easier to process a single line at a time. When EchoProtocol receives a line, it will echo it back to the clientunless the line is "quit", in which case it sends a goodbye message and closes the connection.

Next, a class called EchoServerFactory is defined. EchoServerFactory inherits from ServerFactory, the server-side sibling of ClientFactory, and sets EchoProtocol as its protocol. An instance of EchoServerFactory is then passed as the second argument to reactor.listenTCP, with the first argument being the port to listen on.

Getting Started

Building Simple Clients and Servers

Web Clients

Web Servers

Web Services and RPC


Mail Clients

Mail Servers

NNTP Clients and Servers


Services, Processes, and Logging

Twisted Network Programming Essentials
Twisted Network Programming Essentials
ISBN: 0596100329
EAN: 2147483647
Year: 2004
Pages: 107
Authors: Abe Fettig © 2008-2020.
If you may any questions please contact us: