高阶Twisted (High-Level Twisted) -- 令狐虫

TableOfContents

工具的使用(Using the utilities)

Application

Twisted程序通常和twisted.application.service.Application协作。这个类通常管理着一个运行中的服务器的所有持久化配置信息 —— 需要绑定的端口、必须被保持或者被尝试的连接位置、需要周期性完成的动作,以及几乎所有的一切。它是服务树中的根对象,实现了IService接口。

Twisted programs usually work with twisted.application.service.Application. This class usually holds all persistent configuration of a running server -- ports to bind to, places where connections to must be kept or attempted, periodic actions to do and almost everything else. It is the root object in a tree of services implementing IService.

其他的HOWTO们描述了如何为一个应用编写客户代码,但是本节描述的是如何使用已经写好的代码(这些代码可能是Twisted的一部分,也可能来自一个第三方的Twisted插件开发者)。Twisted的发布中包含了用户建立和维护应用所需的各种各样的工具。

Other HOWTOs describe how to write custom code for Applications, but this one describes how to use already written code (which can be part of Twisted or from a third-party Twisted plugin developer). The Twisted distribution comes with an assortment of tools to create and manipulate Applications.

应用是一种Python对象,可以像其他对象一样被建立和维护。特别的一点是,它们可以被序列化到文件中。Twisted支持几种序列化格式。

Applications are just Python objects, which can be created and manipulated in the same ways as any other object. In particular, they can be serialized to files. Twisted supports several serialization formats.

序列化(Serialization)

mktap和tapconvert(mktap and tapconvert)

mktap(1)工具是建立TAP(或者TAX或者TAS)文件的主要途径。它可以用来建立所有主流Twisted服务器类型——比如web、ftp或IRC——的应用。它同时也支持插件,因此当你安装一个Twisted插件(其实就是将它解压到你PYTHONPATH所指定的目录中)它会自动检测并在所有支持该插件的Twisted应用中使用它。它可以生成上述的任何一种应用格式。

The mktap(1) utility is the main way to create a TAP (or TAX or TAS) file. It can be used to create an Application for all of the major Twisted server types like web, ftp or IRC. It also supports plugins, so when you install a Twisted plugin (that is, unpack it into a directory on your PYTHONPATH) it will automatically detect it and use any Twisted Application support in it. It can create any of the above Application formats.

想了解哪些服务器类型是可用的,使用 mktap --help。mktap --help <名称> 显示一个给定服务器的可能的配置选项。mktap支持一些配置应用所需的通用选项——要了解完整的细节,可以查看man page。

In order to see which server types are available, use mktap --help. For a given server, mktap --help <name> shows the possible configuration options. mktap supports a number of generic options to configure the application -- for full details, read the man page.

有一个重要的选项是 --append <文件名>。它用于向一个已经被序列化的Twisted应用中增加一个服务器。举例来说,它可以用来增加一个telnet服务器,使你可以通过telnet来检测和重新配置应用。

One important option is --append <filename>. This is used when there is already a Twisted application serialized to which a server should be added. For example, it can be used to add a telnet server, which would let you probe and reconfigure the application by telnetting into it.

另一个有用的工具是tapconvert(1),它用于在三种应用格式之间进行转换。

Another useful utility is tapconvert(1), which converts between all three Application formats.