greecket - a python not block socket module base the greenlet and thread - Google Project Hosting

11155 단어 project
greecket - a python not block socket module base the greenlet and thread - Google Project Hosting
greecket, greenlet 과 threading 을 기반 으로 하 는 python 비 차단 모드 socket 모듈.
the greecket uer mail list:
web: http://groups.google.com/group/greecket
mail:[email protected]
example
greecket_test.py

#!/usr/bin/env python
#!coding=utf-8

import greecket
import sys



def onConnecting(evt):
   
print 'connecting',evt.AsList

def onConnected(evt):
   
print 'connected',evt.AsList
    evt
.Data.send('dfsdfsfsfff')
   
pass

def onReceive(event):
   
print 'data receive:',event.AsList

def onError(evt):
   
print 'error:',evt.AsList
   
print evt.Data.args[1]
   
pass

def onClose(evt):
   
print 'close',evt.AsList

def runasserver(address):#host='localhost',port=5423):
    factory
=greecket.Factory(address)
   
print 'server',factory
    factory
.bind(greecket.GREE_CONNECTING,onConnecting)
    factory
.bind(greecket.GREE_CONNECTIONMADE,onConnected)
    factory
.bind(greecket.GREE_DATARECEIVED,onReceive)
    factory
.bind(greecket.GREE_CONNECTIONLOST,onClose)
    factory
.bind(greecket.GREE_ERROR,onError)
    factory
.listen()
   
return factory

def client(address):
    factory
=greecket.Factory(address)
   
print 'client',factory
    factory
.bind(greecket.GREE_CONNECTING,onConnecting)
    factory
.bind(greecket.GREE_CONNECTIONMADE,onConnected)
    factory
.bind(greecket.GREE_DATARECEIVED,onReceive)
    factory
.bind(greecket.GREE_CONNECTIONLOST,onClose)
    factory
.bind(greecket.GREE_ERROR,onError)
    factory
.bind(greecket.GREE_DATARECEIVED,onReceive)
    factory
.bind(greecket.GREE_ERROR,onError)
    factory
.connect()
   
return factory

if __name__=='__main__':
   
print sys.argv
   
if len(sys.argv)>2:
        d
=tuple((sys.argv[-2],int(sys.argv[-1])))
   
else:
        d
=('localhost',5423)
   
if 'server' in sys.argv:
        f
=runasserver(d)
   
else:
        f
=client(d)
    f
.mainLoop()

좋은 웹페이지 즐겨찾기