失效链接处理 |
The Twisted Documentation PDF 下载
本站整理下载:
提取码:ypj1
相关截图:
主要内容:
Introduction
1.1 The Vision For Twisted
Many other documents in this repository are dedicated to defining what Twisted is. Here, I will attempt to explain not
what Twisted is, but what it should be, once I’ve met my goals with it.
First, Twisted should be fun. It began as a game, it is being used commercially in games, and it will be, I hope, an
interactive and entertaining experience for the end-user.
Twisted is a platform for developing internet applications. While Python by itself is a very powerful language,
there are many facilities it lacks which other languages have spent great attention to adding. It can do this now;
Twisted is a good (if somewhat idiosyncratic) pure-python framework or library, depending on how you treat it, and it
continues to improve.
As a platform, Twisted should be focused on integration. Ideally, all functionality will be accessible through
all protocols. Failing that, all functionality should be configurable through at least one protocol, with a seamless
and consistent user-interface. The next phase of development will be focusing strongly on a configuration system
which will unify many disparate pieces of the current infrastructure, and allow them to be tacked together by a nonprogrammer.
Chapter 2
Getting Started
2.1 Writing Servers
2.1.1 Overview
This document explains how you can use Twisted to implement network protocol parsing and handling for TCP servers
(the same code can be reused for SSL and Unix socket servers). There is a separate document (page 125) covering
UDP.
Your protocol handling class will usually subclass twisted.internet.protocol.Protocol. Most protocol handlers inherit either from this class or from one of its convenience children. An instance of the protocol class is
instantiated per-connection, on demand, and will go away when the connection is finished. This means that persistent
configuration is not saved in the Protocol.
The persistent configuration is kept in a Factory class, which usually inherits from twisted.internet.
protocol.Factory. The buildProtocol method of the Factory is used to create a Protocol for each
new connection.
It is usually useful to be able to offer the same service on multiple ports or network addresses. This is why the
Factory does not listen to connections, and in fact does not know anything about the network. See the endpoints documentation (page 158) for more information, or twisted.internet.interfaces.IReactorTCP.listen
TCP, and the other IReactor*.listen* APIs for the lower level APIs that endpoints are based on.
This document will explain each step of the way.
2.1.2 Protocols
As mentioned above, this, along with auxiliary classes and functions, is where most of the code is. A Twisted protocol
handles data in an asynchronous manner: the protocol responds to events as they arrive from the network; the events
arrive as calls to methods on the protocol.
Here is a simple example:
from twisted.internet.protocol import Protocol
class Echo(Protocol):
def dataReceived(self, data):
self.transport.write(data)
This is one of the simplest protocols. It simply writes back whatever is written to it, and does not respond to all
events. Here is an example of a Protocol responding to another event:
from twisted.internet.protocol import Protocol
class QOTD(Protocol):
def connectionMade(self):
self.transport.write("An apple a day keeps the doctor away\r\n")
self.transport.loseConnection()
|