欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Python下的twisted框架入门指引

程序员文章站 2023-11-14 14:27:04
什么是twisted? twisted是一个用python语言写的事件驱动的网络框架,他支持很多种协议,包括udp,tcp,tls和其他应用层协议,比如http,smtp...

什么是twisted?

twisted是一个用python语言写的事件驱动的网络框架,他支持很多种协议,包括udp,tcp,tls和其他应用层协议,比如http,smtp,nntm,irc,xmpp/jabber。 非常好的一点是twisted实现和很多应用层的协议,开发人员可以直接只用这些协议的实现。其实要修改twisted的ssh服务器端实现非常简单。很多时候,开发人员需要实现protocol类。

一个twisted程序由reactor发起的主循环和一些回调函数组成。当事件发生了,比如一个client连接到了server,这时候服务器端的事件会被触发执行。
用twisted写一个简单的tcp服务器

下面的代码是一个tcpserver,这个server记录客户端发来的数据信息。

==== code1.py ====
import sys
from twisted.internet.protocol import serverfactory
from twisted.protocols.basic import linereceiver
from twisted.python import log
from twisted.internet import reactor

class cmdprotocol(linereceiver):

  delimiter = '\n'

  def connectionmade(self):
    self.client_ip = self.transport.getpeer()[1]
    log.msg("client connection from %s" % self.client_ip)
    if len(self.factory.clients) >= self.factory.clients_max:
      log.msg("too many connections. bye !")
      self.client_ip = none
      self.transport.loseconnection()
    else:
      self.factory.clients.append(self.client_ip)

  def connectionlost(self, reason):
    log.msg('lost client connection. reason: %s' % reason)
    if self.client_ip:
      self.factory.clients.remove(self.client_ip)

  def linereceived(self, line):
    log.msg('cmd received from %s : %s' % (self.client_ip, line))

class myfactory(serverfactory):

  protocol = cmdprotocol

  def __init__(self, clients_max=10):
    self.clients_max = clients_max
    self.clients = []

log.startlogging(sys.stdout)
reactor.listentcp(9999, myfactory(2))
reactor.run()

下面的代码至关重要:

from twisted.internet import reactor
reactor.run()

这两行代码会启动reator的主循环。

在上面的代码中我们创建了"serverfactory"类,这个工厂类负责返回“cmdprotocol”的实例。 每一个连接都由实例化的“cmdprotocol”实例来做处理。 twisted的reactor会在tcp连接上后自动创建cmdprotocol的实例。如你所见,protocol类的方法都对应着一种事件处理。

当client连上server之后会触发“connectionmade"方法,在这个方法中你可以做一些鉴权之类的操作,也可以限制客户端的连接总数。每一个protocol的实例都有一个工厂的引用,使用self.factory可以访问所在的工厂实例。

上面实现的”cmdprotocol“是twisted.protocols.basic.linereceiver的子类,linereceiver类会将客户端发送的数据按照换行符分隔,每到一个换行符都会触发linereceived方法。稍后我们可以增强linereceived来解析命令。

twisted实现了自己的日志系统,这里我们配置将日志输出到stdout

当执行reactor.listentcp时我们将工厂绑定到了9999端口开始监听。

user@lab:~/tmp$ python code1.py
2011-08-29 13:32:32+0200 [-] log opened.
2011-08-29 13:32:32+0200 [-] __main__.myfactory starting on 9999
2011-08-29 13:32:32+0200 [-] starting factory <__main__.myfactory instance at 0x227e320
2011-08-29 13:32:35+0200 [__main__.myfactory] client connection from 127.0.0.1
2011-08-29 13:32:38+0200 [cmdprotocol,0,127.0.0.1] cmd received from 127.0.0.1 : hello server

使用twisted来调用外部进程

下面我们给前面的server添加一个命令,通过这个命令可以读取/var/log/syslog的内容

import sys
import os

from twisted.internet.protocol import serverfactory, processprotocol
from twisted.protocols.basic import linereceiver
from twisted.python import log
from twisted.internet import reactor

class tailprotocol(processprotocol):
  def __init__(self, write_callback):
    self.write = write_callback

  def outreceived(self, data):
    self.write("begin lastlog\n")
    data = [line for line in data.split('\n') if not line.startswith('==')]
    for d in data:
      self.write(d + '\n')
    self.write("end lastlog\n")

  def processended(self, reason):
    if reason.value.exitcode != 0:
      log.msg(reason)

class cmdprotocol(linereceiver):

  delimiter = '\n'

  def processcmd(self, line):
    if line.startswith('lastlog'):
      tailprotocol = tailprotocol(self.transport.write)
      reactor.spawnprocess(tailprotocol, '/usr/bin/tail', args=['/usr/bin/tail', '-10', '/var/log/syslog'])
    elif line.startswith('exit'):
      self.transport.loseconnection()
    else:
      self.transport.write('command not found.\n')

  def connectionmade(self):
    self.client_ip = self.transport.getpeer()[1]
    log.msg("client connection from %s" % self.client_ip)
    if len(self.factory.clients) >= self.factory.clients_max:
      log.msg("too many connections. bye !")
      self.client_ip = none
      self.transport.loseconnection()
    else:
      self.factory.clients.append(self.client_ip)

  def connectionlost(self, reason):
    log.msg('lost client connection. reason: %s' % reason)
    if self.client_ip:
      self.factory.clients.remove(self.client_ip)

  def linereceived(self, line):
    log.msg('cmd received from %s : %s' % (self.client_ip, line))
    self.processcmd(line)

class myfactory(serverfactory):

  protocol = cmdprotocol

  def __init__(self, clients_max=10):
    self.clients_max = clients_max
    self.clients = []

log.startlogging(sys.stdout)
reactor.listentcp(9999, myfactory(2))
reactor.run()

在上面的代码中,没从客户端接收到一行内容后会执行processcmd方法,如果收到的一行内容是exit命令,那么服务器端会断开连接,如果收到的是lastlog,我们要吐出一个子进程来执行tail命令,并将tail命令的输出重定向到客户端。这里我们需要实现processprotocol类,需要重写该类的processended方法和outreceived方法。在tail命令有输出时会执行outreceived方法,当进程退出时会执行processended方法。

如下是执行结果样例:

user@lab:~/tmp$ python code2.py
2011-08-29 15:13:38+0200 [-] log opened.
2011-08-29 15:13:38+0200 [-] __main__.myfactory starting on 9999
2011-08-29 15:13:38+0200 [-] starting factory <__main__.myfactory instance at 0x1a5a3f8>
2011-08-29 15:13:47+0200 [__main__.myfactory] client connection from 127.0.0.1
2011-08-29 15:13:58+0200 [cmdprotocol,0,127.0.0.1] cmd received from 127.0.0.1 : test
2011-08-29 15:14:02+0200 [cmdprotocol,0,127.0.0.1] cmd received from 127.0.0.1 : lastlog
2011-08-29 15:14:05+0200 [cmdprotocol,0,127.0.0.1] cmd received from 127.0.0.1 : exit
2011-08-29 15:14:05+0200 [cmdprotocol,0,127.0.0.1] lost client connection. reason: [failure instance: traceback (failure with no frames): <class 'twisted.internet.error.connectiondone'>: connection was closed cleanly.

可以使用下面的命令作为客户端发起命令:

user@lab:~$ netcat 127.0.0.1 9999
test
command not found.
lastlog
begin lastlog
aug 29 15:02:03 lab ssmtp[5919]: unable to locate mail
aug 29 15:02:03 lab ssmtp[5919]: cannot open mail:25
aug 29 15:02:03 lab cron[4945]: (cron) error (grandchild #4947 failed with exit status 1)
aug 29 15:02:03 lab ssmtp[5922]: unable to locate mail
aug 29 15:02:03 lab ssmtp[5922]: cannot open mail:25
aug 29 15:02:03 lab cron[4945]: (logcheck) mail (mailed 1 byte of output; but got status 0x0001, #012)
aug 29 15:05:01 lab cron[5925]: (root) cmd (command -v debian-sa1 > /dev/null && debian-sa1 1 1)
aug 29 15:10:01 lab cron[5930]: (root) cmd (test -x /usr/lib/atsar/atsa1 && /usr/lib/atsar/atsa1)
aug 29 15:10:01 lab cron[5928]: (cron) error (grandchild #5930 failed with exit status 1)
aug 29 15:13:21 lab pulseaudio[3361]: ratelimit.c: 387 events suppressed

 
end lastlog
exit

使用deferred对象

reactor是一个循环,这个循环在等待事件的发生。 这里的事件可以是数据库操作,也可以是长时间的计算操作。 只要这些操作可以返回一个deferred对象。deferred对象可以自动得在事件发生时触发回调函数。reactor会block当前代码的执行。

现在我们要使用defferred对象来计算sha1哈希。

import sys
import os
import hashlib

from twisted.internet.protocol import serverfactory, processprotocol
from twisted.protocols.basic import linereceiver
from twisted.python import log
from twisted.internet import reactor, threads

class tailprotocol(processprotocol):
  def __init__(self, write_callback):
    self.write = write_callback

  def outreceived(self, data):
    self.write("begin lastlog\n")
    data = [line for line in data.split('\n') if not line.startswith('==')]
    for d in data:
      self.write(d + '\n')
    self.write("end lastlog\n")

  def processended(self, reason):
    if reason.value.exitcode != 0:
      log.msg(reason)

class hashcompute(object):
  def __init__(self, path, write_callback):
    self.path = path
    self.write = write_callback

  def blockingmethod(self):
    os.path.isfile(self.path)
    data = file(self.path).read()
    # uncomment to add more delay
    # import time
    # time.sleep(10)
    return hashlib.sha1(data).hexdigest()

  def compute(self):
    d = threads.defertothread(self.blockingmethod)
    d.addcallback(self.ret)
    d.adderrback(self.err)

  def ret(self, hdata):
    self.write("file hash is : %s\n" % hdata)

  def err(self, failure):
    self.write("an error occured : %s\n" % failure.geterrormessage())

class cmdprotocol(linereceiver):

  delimiter = '\n'

  def processcmd(self, line):
    if line.startswith('lastlog'):
      tailprotocol = tailprotocol(self.transport.write)
      reactor.spawnprocess(tailprotocol, '/usr/bin/tail', args=['/usr/bin/tail', '-10', '/var/log/syslog'])
    elif line.startswith('comphash'):
      try:
        useless, path = line.split(' ')
      except:
        self.transport.write('please provide a path.\n')
        return
      hc = hashcompute(path, self.transport.write)
      hc.compute()
    elif line.startswith('exit'):
      self.transport.loseconnection()
    else:
      self.transport.write('command not found.\n')

  def connectionmade(self):
    self.client_ip = self.transport.getpeer()[1]
    log.msg("client connection from %s" % self.client_ip)
    if len(self.factory.clients) >= self.factory.clients_max:
      log.msg("too many connections. bye !")
      self.client_ip = none
      self.transport.loseconnection()
    else:
      self.factory.clients.append(self.client_ip)

  def connectionlost(self, reason):
    log.msg('lost client connection. reason: %s' % reason)
    if self.client_ip:
      self.factory.clients.remove(self.client_ip)

  def linereceived(self, line):
    log.msg('cmd received from %s : %s' % (self.client_ip, line))
    self.processcmd(line)

class myfactory(serverfactory):

  protocol = cmdprotocol

  def __init__(self, clients_max=10):
    self.clients_max = clients_max
    self.clients = []

log.startlogging(sys.stdout)
reactor.listentcp(9999, myfactory(2))
reactor.run()

blockingmethod从文件系统读取一个文件计算sha1,这里我们使用twisted的defertothread方法,这个方法返回一个deferred对象。这里的deferred对象是调用后马上就返回了,这样主进程就可以继续执行处理其他的事件。当传给defertothread的方法执行完毕后会马上触发其回调函数。如果执行中出错,blockingmethod方法会抛出异常。如果成功执行会通过hdata的ret返回计算的结果。
推荐的twisted阅读资料

http://twistedmatrix.com/documents/current/core/howto/defer.html http://twistedmatrix.com/documents/current/core/howto/process.html http://twistedmatrix.com/documents/current/core/howto/servers.html

api文档:

http://twistedmatrix.com/documents/current/api/twisted.html