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
()