python - 无法使用 Asyncore Python 接收数据

标签 python asyncore

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

客户端.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()

服务器.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()

最佳答案

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

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"])

关于python - 无法使用 Asyncore Python 接收数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20701437/

相关文章:

python - 我可以用数据框一次替换一些值吗?

python - 在 asyncore 客户端中定义可写方法使发送数据非常慢

Python 聊天与异步获取 session 数据

python 异步连接

python - 使用 asyncore 读取套接字缓冲区

python - 在Elasticsearch中按汇总查询前N个类别

python - 我如何使用 py2app?

python - 根据定界符吐出一列

python - 如何在Python中计算非零信号之间的时间间隔

python - Python 中的异步 HTTP 调用