`

使用Twisted Application 框架

阅读更多

翻译by:pako

email/gtalk:zealzpc@gtalk.com

目标:

 

介绍twisted Application 结构

介绍如何使用.tac文件和twistd部署twisted application

介绍twisted services

 

 

 

概要

 

Twisted application框架负责启动和停止你的应用程序。使用application框架中已经实现了的那些工具可以使你方便的实现 daemo,logging,选择一个reactor等功能,而不用为此再做那些枯燥的工作。

 

 

Twisted applications 最主要的工具是一个命令行的组件叫twistd。Twistd 实现了跨平台,在运行Twisted applications时强烈推荐使用它。

 

Twisted Application 框架最重要的组件就是 twisted.application.service.Application类,代表了你的应用程序。当然,Application类不会提供所有你想实现的功能。事实上,Application类更像一个services的容器,里面可以包容若干个service。你所要做的就是用过Application这个框架来实现里面的一个个services。

 

对于“service”,我们是指那些可以启动和停止的程序。通常来说service包括web servers ,FTP servers,和ssh 客户端。你的Application对象可以容纳许多service,通过IServiceCollections类甚至可以是一个有结构层次的service。

 

下面是一个简单的Application结构例子,它实现了一个echo tcp服务跑在7001端口上。

 

 

 from twisted.application import internet, service
 from somemodule import EchoFactory
 port = 7001
 factory = EchoFactory()
 
 # 下面是最重要的一行,没有Application类,就无法欲行
 application = service.Application("echo")  # 创建Application对象
 echoService = internet.TCPServer(port, factory) # 创建echo 服务
 # 将echo服务放置到application里面
echoService.setServiceParent(application)
 

 

 

 

这个简单的例子的结构:

 

   application

   |

   `- echoService

 

更复杂的层次结构可以通过IServiceCollection来实现。你将会喜欢上这样去管理一个依赖其他服务的服务。比如,一个Twisted Application代理希望他的服务总是在相应的客户端启动后启动。

 

 

 

使用Application

twistd and tac

twistd和tac文件

 

对于处理启动和配置你的Twisted application,Twisted Application框架使用.tac文件。.tac文件也是python文件,它

 

下面这个简单的例子是一个.tac文件

 

 

"""
This is an example .tac file which starts a webserver on port 8080 and
serves files from the current working directory.

The important part of this, the part that makes it a .tac file, is
the final root-level section, which sets up the object called 'application'
which twistd will look for
"""

import os
from twisted.application import service, internet
from twisted.web import static, server

def getWebService():
    """
    Return a service suitable for creating an application object.

    This service is a simple web server that serves files on port 8080 from
    underneath the current working directory.
    """

    #创建一个resource对象,用来服务静态文件
    fileServer = server.Site(static.File(os.getcwd()))
    return internet.TCPServer(8080, fileServer)

# this is the core part of any tac file, the creation of the root-level
# application object
application = service.Application("Demo application")

# attach the service to its parent application
service = getWebService()
service.setServiceParent(application)
 

 

 

#你可以用过 twistd -ny service.tac 运行这个文件

 

 

Twistd是一个用来执行 Twisted applications 的.tac文件的程序。运行它最简单的方式是 通过twistd 命令 加变量 -y 和一个tac文件名。比如你可以运行上面的server通过 twistd -y service.tac 命令。

 

在tac应用中定制 twistd 日志

 

定制日志的行为可以在.tac文件中通过api来访问。ILogObserver组件可以被设置在一个Application中,提供给twistd使用。

 

下面是如何使用DailyLogFile的例子,它会每天记录一次。

 

 

from twisted.application.service import Application
from twisted.python.log import ILogObserver, FileLogObserver
from twisted.python.logfile import DailyLogFile

application = Application("myapp")
logfile = DailyLogFile("my.log", "/tmp")
application.setComponent(ILogObserver, FileLogObserver(logfile).emit)
 

 

 

invoking twistd -y my.tac will create a log file at /tmp/my.log.

 

执行 twistd -y my.tac命令时会创建一个日志文件/tmp/my.log。

 

Twisted 提供的service

 

Twisted提供了一些你可能需要的service

 

每一个Twisted提供了的service(TimerService除外)都会有 connect 或 listen方法在reactor里,并且这些service和reactor都使用一样的方法。

 

connect方法是提供给客户端使用,listen方法提供给服务端。比如,TCPServer相应的使用 reactor.listenTCP监听客户端的连接 而 TCPClient使用 reactor.connectTCP去连接服务器端。

 

 

TCPServer

TCPClient

Services which allow you to make connections and listen for connections on TCP ports.

listenTCP

connectTCP

 

允许你在TCP端口上连接和监听连接。

UNIXServer

UNIXClient

Services which listen and make connections over UNIX sockets.

listenUNIX

connectUNIX

一个通过UNIX sockets 连接和监听连接的service。

 

SSLServer

SSLClient

Services which allow you to make SSL connections and run SSL servers.

listenSSL

connectSSL

一个通过SSL 连接和监听连接的service。

 

UDPServer

UDPClient

Services which allow you to send and receive data over UDP

listenUDP

允许你通过UDP发送和接受数据。

 

UNIXDatagramServer

UNIXDatagramClient

Services which send and receive data over UNIX datagram sockets.

listenUNIXDatagram

connectUNIXDatagram

允许你通过UNIX datagram sockets发送和接受数据。

 

MulticastServer

A server for UDP socket methods that support multicast.

listenMulticast

一个UDP socket service,支持多点广播。

 

TimerService

A service to periodically call a function.

一个server周期性的执行一个方法。

 

IServiceCollection 对象里面包含着IService对象(上面提到的都是 IService对象)。IService可以被添加到IServiceCollection对象中 通过setServiceParent方法,通过disownServiceParent去除一个IService对象。

 

 

 

from twisted.application import internet, service
from twisted.names import server, dns, hosts

port = 53

# Create a MultiService, and hook up a TCPServer and a UDPServer to it as
# children.
dnsService = service.MultiService()
hostsResolver = hosts.Resolver('/etc/hosts')
tcpFactory = server.DNSServerFactory([hostsResolver])
internet.TCPServer(port, tcpFactory).setServiceParent(dnsService)
udpFactory = dns.DNSDatagramProtocol(tcpFactory)
internet.UDPServer(port, udpFactory).setServiceParent(dnsService)

# Create an application as normal
application = service.Application("DNSExample")

# Connect our MultiService to the application, just like a normal service.
dnsService.setServiceParent(application)
 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics