'asyncore'에 해당되는 글 1건

  1. python/asyncore 에코서버 2009/02/01

python/asyncore 에코서버

Posted at 2009/02/01 18:39// Posted in python/pynetworks

import asyncore
import socket

class EchoUser(asyncore.dispatcher_with_send):
def __init__(self, conn, addr):
asyncore.dispatcher_with_send.__init__(self, conn)
print "new_user:", conn, addr
self.conn = conn
self.addr = addr

def handle_read(self):
data = self.recv(1024)
if data:
self.send(data)

def handle_close(self):
print "del_user:", self.conn, self.addr
self.close()


class EchoServer(asyncore.dispatcher):
def __init__(self):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind(("", 2000))
self.listen(5)

def handle_accept(self):
conn, addr = self.accept()
EchoUser(conn, addr)

s = EchoServer()
asyncore.loop()


지금까지 파이썬 네트워크 코드는 전부 socket 을 사용해서 직접 구현했는데-_-;
asyncore 라는 간단한 구현이 있었네요 orz;;
이올린에 북마크하기(0) 이올린에 추천하기(0)
2009/02/01 18:39 2009/02/01 18:39