twisted框架

twisted框架是python实现的基于事件的优秀的框架,适合于网络编程等。

一个server实例

# -*- coding: UTF-8 -*-  
  #Twisted MMORPG  
  from twisted.internet.protocol import Factory  
  from twisted.protocols.basic import LineOnlyReceiver  
  from twisted.internet import reactor  
  import random  
  import string  
   
  class Game(LineOnlyReceiver):      
     def lineReceived(self, data):    
         self.factory.sendAll("%s" % (data))   
     def getId(self):          
         return str(self.transport.getPeer())  
     def connectionMade(self):          
         print "New User Login:", self.getId()    
         self.transport.write("欢迎来到MMO世界!\n")          
         self.factory.addClient(self)      
     def connectionLost(self, reason):          
         self.factory.delClient(self)  
           
class GameFactory(Factory):      
     protocol = Game      
     def __init__(self):          
         self.clients = []  
         self.player = []  
         self.msg=''   
         self.x = range(100,700)  
         self.y = range(100,500)     
     def getPlayerId(self):  
         return len(self.player)  
     def addClient(self, newclient):     
            self.clients.append(newclient)   
     def delClient(self, client):          
         self.clients.remove(client)      
     def sendAll(self, data):  
         #print data  
         if data.find('<policy-file-request/>')!=-1:  
             proto.transport.write('<cross-domain-policy><allow-access-from domain="127.0.0.1" to-ports="*"/></cross-domain-policy>\0')  
         else:  
             arr = data.split(':')  
             prefix = arr[0]  
             content = arr[1]  
             if prefix.find('player')!=-1:  
                  newPlayer = [content,str(random.randrange(200, 600)),str(random.randrange(150,350)),str(random.randrange(1,5))]  
                  self.player.append(newPlayer)  
                  self.msg = ' 玩家 '+content+' 进入游戏!'   
                  #广播所有玩家的位置  
                  temp = []  
                  playerData = ':::' 
                  for pos in self.player:  
                      temp.append(string.join(pos,'---'))  
                  playerData = playerData+string.join(temp,'***')  
                  for proto in self.clients:  
                      proto.transport.write('[系统]: '+self.msg+'\n')  
                      proto.transport.write(playerData)  
             elif prefix.find('pos')!=-1:  
                 playerName,x,y = content.split('---')  
                 i = 0  
                 for p in self.player:  
                     if p[0]==playerName:  
                         p[1]=x  
                         p[2]=y  
                 for proto in self.clients:  
                     proto.transport.write(data)  
             else:  
                 self.msg = data  
                 for proto in self.clients:  
                      proto.transport.write(self.msg+'\n')  
               
reactor.listenTCP(8006, GameFactory())  
reactor.run() 

一个客户端的实现

from twisted.internet.protocol import ClientCreator, Protocol
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor
import sys

class Sender(Protocol):
    def sendCommand(self, command):
        print "invio", command
        self.transport.write(command)

    def dataReceived(self, data):
        print "DATA", data

PORT = 5005
HOST = 'localhost'

def sendCommand(command):
    def test(d):
        print "Invio ->", command
        d.sendCommand(command)
    c = ClientCreator(reactor, Sender)
    c.connectTCP(HOST, PORT).addCallback(test)


if __name__ == '__main__':
    if len(sys.argv) != 2 or sys.argv[1] not in ['stop', 'next_call', 'force']:
        sys.stderr.write('Usage: %s: {stop|next_call|force}\n' % sys.argv[0])
        sys.exit(1)
    sendCommand(sys.argv[1]+'\n')
    reactor.run()

twisted中比较好玩的是defered,就是注册回调函数,这样程序运行完,可以根据程序的运行结果来调用defered中注册的函数。


def  deferredExample():
    d  =  defer.Deferred()
    d.addCallback(failAtHandlingResult)
    d.addCallbacks(yesDecision,  noDecisionPassthru)  //注册成功/失败的回调函数
    d.addCallback(handleResult)  #  -  A  -
    d.addErrback(handleFailure)                       //注册异常的回调函数

你可能感兴趣的:(框架)