O'Reilly logo

Twisted Network Programming Essentials by Abe Fettig

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 2. Building Simple Clients and Servers

To develop with Twisted, you’ll need to learn how to use several new classes and objects. These classes are at the core of Twisted, and you’ll use them over and over in your applications. They also represent the steepest part of the Twisted learning curve. Understand how to use them, and the rest of Twisted will be easy; otherwise, you’ll struggle (or write lots of unnecessary code).

This chapter shows how to write simple clients and servers with Twisted. Along the way, it introduces Twisted’s basic classes, explains how they work, and demonstrates how to use them.

Starting the Twisted Event Loop

Twisted is an event-driven framework. This means that instead of having the program’s functions called in a sequence specified by the program’s logic, they are called in response to external actions, or events. For example, a GUI program might have code for responding to the “button pressed” event. The designer of the program can’t be sure exactly when such an event will occur; but she writes a function to respond to this event whenever it does happen. Such a function is known as an event handler .

Every event-driven framework includes a special function called an event loop . Once started, an event loop runs indefinitely. While it’s running, it waits for events. When an event occurs, the event loop triggers the appropriate event handler function.

Using an event loop requires a different mindset on the part of the programmer than traditional sequential ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required