最近一段时间用PYTHON写一个异步的SOCKET,刚开始用twisted框架,虽然可以在收到数据后使用异步方式处理会阻塞线程的工作,但始终无法实现一边发数据一边收数据,最后使用了asyncore,大概代码如下,如果中文注释乱码,请加#coding=utf-8
server端
# -*- coding: utf-8 -*-
import socket
import asyncore
import threading
MAX_REV = 4069
#负责接收client的连线
class AgentServer(asyncore.dispatcher):
def __init__(self,port):
#asyncore.dispatcher's init
asyncore.dispatcher.__init__(self)
#client socket
self.clientSocket = None
#server port
self.port = port
#建立等待连接的socket
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.bind(("",self.port))
self.listen(5)
def handle_accept(self):
#接收client的连线
self.clientSocket, address = self.accept()
print "new client from:",address[0]
self.clientSocket = ClientAgent(self.clientSocket)
#自定的client连线socket
class ClientAgent(asyncore.dispatcher):
def __init__(self,socket):
#要送出的data
self.SendData=""
#从client收到的data
def handle_read(self):
self.RecvData = self.recv(MAX_RECV)
if len(self.RecvData)>0:
print "recv:",self.RecvData
#送出data到client
def handle_write(self):
send_byte = self.send(self.SendData)
#可能发不完
if send_bute >0:
send_out = self.SendData[:send_byte]
self.SendData = self.SendData[send_byte:]
self.handle_write()
else:
print "send all!!"
self.SendData=""
#不自动执行write
def writable(self):
return False
def handle_close(self):
print "close connection : " + self.getpeername()[0]
self.close()
#等待client连线的线程
class listen_client_thread(threading.Thread):
def __init__(self,port):
self.agentServer = AgentServer(port)
threading.Thread.__init__ ( self )
def run(self):
print "Listen Client ..."
asyncore.loop()
#产生处理输入的线程
class input_thread(threading.Thread):
def __init__(self,listen_thread):
self.listen_thread = listen_thread
threading.Thread.__init__ ( self )
def run(self):
while 1:
send_data = raw_input()
self.listen_thread.agentServer.clientSocket.SendData = send_data
self.listen_thread.agentServer.clientSocket.handle_write()
if __name__ == "__main__":
#产生等待client连接的线程
listen_thread = listen_client_thread(111)
listen_thread.start()
#产生处理输入的线程
input_thread(listen_thread).start()
client端
# -*- coding: utf-8 -*-
import asyncore, socket
import threading
#收到数据库的最大长度
MAX_RECV = 4096
#连线server的socket
class client(asyncore.dispatcher):
def __init__(self,host,port):
asyncore.dispatcher.__init__(self)
self.SendData = ""
self.RecvData = ""
#和server建立连接
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.connect((host,port))
def handle_connect(self):
print "connect!!"
def handle_close(self):
print "connect close!"
self.close()
#收到的data
def handle_read(self):
self.RecvData = self.recv(MAX_RECV)
if len(self.RecvData)>0:
print "recv:",self.RecvData
#送出data
def handle_write(self):
send_byte = self.send(self.SendData)
if send_byte > 0:
send_out = self.SendData[:send_byte]
self.SendData = self.SendData[send_byte:]
print "send : " + send_out
self.handle_write()
else:
print "send all!!"
self.SendData=""
#自动侦听送出永远失败
def writable(self):
return False
#等待server傳送訊息的thread
class send_server_thread(threading.Thread):
def __init__(self,host,port):
self.client = client(host, port)
threading.Thread.__init__ ( self )
def run(self):
try:
asyncore.loop()
except:
pass
#等待user input的thread
class input_thread(threading.Thread):
def __init__(self,client_thread):
self.client_thread = client_thread
threading.Thread.__init__ ( self )
def run(self):
while 1:
send_data = raw_input()
self.client_thread.client.SendData = send_data
self.client_thread.client.handle_write()
#主程式if __name__ == "__main__":
#建立和server連線, 並且等待溝通
client_thread = send_server_thread("localhost",111)
client_thread.start()
#等待user input
input_thread(client_thread).start()