gpt4 book ai didi

python - 扭曲套接字连接未接收到数据

转载 作者:行者123 更新时间:2023-11-30 23:14:20 26 4
gpt4 key购买 nike

我有一个扭曲的服务器脚本在unix套接字上监听,当客户端处于扭曲状态时它会接收数据,但如果我通过普通的python套接字代码发送它则不起作用。

class SendProtocol(LineReceiver):
"""
This works
"""
def connectionMade(self):
print 'sending log'
self.sendLine(self.factory.logMessage)

if __name__ == '__main__':

address = FilePath('/tmp/test.sock')
startLogging(sys.stdout)

clientFactory = ClientFactory()
clientFactory.logMessage = 'Dfgas35||This is a message from server'
clientFactory.protocol = SendProtocol

port = reactor.connectUNIX(address.path, clientFactory)
reactor.run()

但这不会(服务器没有获取任何数据)

sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

sock_addr = '/tmp/test.sock'
try:
sock.connect(sock_addr)
except socket.error, msg:
print >> sys.stderr, msg
sys.exit(1)

sock.setblocking(0) # not needed though tried both ways
print 'connected %s' % sock.getpeername()
print 'End END to abort'

while True:
try:
line = raw_input('Enter mesg: ')
if line.strip() == 'END':
break

line += '\n'
print 'sending'
sock.sendall(line)
finally:
sock.close()

最佳答案

您的两个客户端程序发送不同的数据。发送以 \r\n 结尾的行。另一个发送以 \n 结尾的行。也许您的服务器期望以 \r\n 结尾的行,这就是后一个示例似乎不起作用的原因。您的非 Twisted 示例也会在发送第一行后关闭套接字,但继续其读发送循环。

关于python - 扭曲套接字连接未接收到数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28797034/

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