关于python:扭曲的套接字连接未接收到数据

Data not received by twisted socket connection

我有一个扭曲的服务器脚本,在unix套接字上监听,当客户端处于扭曲状态时,它将接收数据,但是如果我通过香草python套接字代码发送数据,它将无法正常工作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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()

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
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 += '\
'

    print 'sending'
    sock.sendall(line)
  finally:
    sock.close()

您的两个客户端程序发送不同的数据。一个发送\
\
终止的行。另一个发送\
终止的行。也许您的服务器期望使用\
\
终止的行,这就是为什么后面的示例似乎不起作用的原因。您的非Twisted示例还会在发送第一行后关闭套接字,但会继续执行其读取发送循环。