發布一個基於greenlet和threading的python非阻塞模式socket模塊

greecket,基於greenlet和threading的python非阻塞模式socket模塊。

the greecket uer mail list:

web:http://groups.google.com/group/greecket

mail:greecket@googlegroups.com


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 ()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值