无法使用Asyncore Python接收数据

时间:2013-12-20 10:24:53

标签: python asyncore

你好我是python编程新手。我可以知道为什么我的服务器没有收到客户端发送的消息吗?我试图将数据从客户端发送到服务器并使服务器在控制台中显示...

Client.py

import asyncore
import socket


class Client(asyncore.dispatcher):
    def __init__(self,IPAddress_Server,intServerPort):
        asyncore.dispatcher.__init__(self)

        self.IPHostEntry_Server = (IPAddress_Server,intServerPort)
        self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.connect(self.IPHostEntry_Server)

    def handle_connect(self):
        print("Successfully connected to {0}".format(self.IPHostEntry_Server))

    def handle_write(self):
        bytesTx = b"DDDDDDD"
        self.send(bytesTx)
        self.close()


client = Client("127.0.0.1", 4444)

asyncore.loop()

Server.py

import asyncore
import socket

class Server(asyncore.dispatcher):

    def __init__(self,IPAddress_Server,intServerPort):
        asyncore.dispatcher.__init__(self)

        self.IPHostEntry_Server = (IPAddress_Server,intServerPort)
        self.create_socket(socket.AF_INET,socket.SOCK_STREAM)
        self.set_reuse_addr()
        self.bind(self.IPHostEntry_Server)
        self.listen(5)

    def handle_accept(self):
        pair = self.accept()
        if pair is not None:
            sock, addr = pair
            print ('Incoming connection from %s' % repr(addr))


    def handle_read(self):
        bytesRx = self.recv(4)
        print("%s" %bytesRx)

server = Server("127.0.0.1", 4444)
asyncore.loop()

1 个答案:

答案 0 :(得分:1)

您的服务器接受连接,然后将它们丢弃在地板上:

def handle_accept(self):
    pair = self.accept()
    if pair is not None:
        sock, addr = pair
        print ('Incoming connection from %s' % repr(addr))

服务器套接字与服务器套接字接受的每个连接是分开的。

如果你想从连接读取字节,那么你必须做更多的工作来处理上的读取事件。

我建议看看Twisted代替。它比asyncore更积极地维护,记录更好,功能更强大。这是一个最小的基于Twisted的示例,可以执行我认为您希望应用程序执行的操作:

tx_client.py

from twisted.internet.task import react
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Protocol
from twisted.internet.endpoints import clientFromString, connectProtocol

class SendBytes(Protocol):
    def __init__(self, data):
        self.data = data
        self.done = Deferred()

    def connectionMade(self):
        self.transport.write(self.data)
        self.transport.loseConnection()

    def connectionLost(self, reason):
        self.done.callback(None)

def main(reactor, description, data):
    endpoint = clientFromString(reactor, description)
    connecting = connectProtocol(endpoint, SendBytes(data))
    def connected(proto):
        print("Successfully connected to {0}".format(proto.transport.getPeer().host))
        return proto.done
    connecting.addCallback(connected)
    return connecting

if __name__ == '__main__':
    react(main, [b"tcp:host=127.0.0.1:port=4444", b"DDDDDD"])

tx_server.py

from twisted.internet.task import react
from twisted.internet.defer import Deferred
from twisted.internet.protocol import Factory, Protocol
from twisted.internet.endpoints import serverFromString

class ReadBytes(Protocol):
    def connectionMade(self):
        print("Incoming connection from {}".format(self.transport.getPeer()))

    def dataReceived(self, data):
        print("Received {}".format(data))

def main(reactor, description):
    endpoint = serverFromString(reactor, description)
    endpoint.listen(Factory.forProtocol(ReadBytes))
    return Deferred()

if __name__ == '__main__':
    react(main, [b"tcp:interface=127.0.0.1:port=4444"])