我想运行一个服务器,该服务器接受来自少数客户端的连接,并且还希望它发送一条消息到特定的另一个服务器 - 它会侦听1234端口初始时间。
self.connect(('localhost', 1234))
self.buffer = 'connect'
我把它放在init方法中。和 编写了下面那种代码。
class EchoHandler(asyncore.dispatcher_with_send):
def handle_read(self):
data = self.recv(8192)
if data:
print data
self.send(data + " echo")
class EchoServer(asyncore.dispatcher):
def __init__(self, host, port):
asyncore.dispatcher.__init__(self)
self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
self.set_reuse_addr()
self.bind((host, port))
self.listen(5)
self.connect(('localhost', 1234))
self.buffer = 'connect'
def handle_accept(self):
pair = self.accept()
if pair is not None:
sock, addr = pair
print 'Incoming connection from %s' % repr(addr)
handler = EchoHandler(sock)
def handle_write(self):
sent = self.send(self.buffer)
self.buffer = self.buffer[sent:]
server = EchoServer('localhost', 0)
asyncore.loop()
我需要你的帮助..期待你的回复。谢谢..