gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 18:40:11 37 4
gpt4 key购买 nike

嗨,我是 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/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com