双绞套接字连接未接收到数据

Data not received by twisted socket connection

我有一个扭曲的服务器脚本在 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 示例还在发送第一行之后关闭套接字,但继续其读取-发送循环。