从扭曲的客户端向服务器发送多条消息

从扭曲的客户端向服务器发送多条消息

本文介绍了从扭曲的客户端向服务器发送多条消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个客户端/服务器系统,客户端将消息发送到服务器.服务器只是打印出客户端发送的内容.

I'm trying to build a client/server system that clients send messages to server. Server does nothing more than printing out what clients send.

from twisted.internet import protocol, reactor

class Echo(protocol.Protocol):
    def dataReceived(self, data):
        print data
        self.transport.write(data)

class EchoFactory(protocol.Factory):
    def buildProtocol(self, addr):
        return Echo()

reactor.listenTCP(8000, EchoFactory())
reactor.run()

问题是,当我尝试使用此代码从客户端发送多条消息时,客户端在第一次连接并发送后引发错误.

The issue is that when I tried to send multiple message from client with this code, the client raises an error after the first connection and send.

from twisted.internet import reactor, protocol
import time

class EchoClient(protocol.Protocol):
   def __init__(self, message):
       self.message = message

   def connectionMade(self):
       self.transport.write(self.message)

   def dataReceived(self, data):
       print "Server said:", data
       self.transport.loseConnection()

class EchoFactory(protocol.ClientFactory):
   def __init__(self, message):
       self.message = message

   def buildProtocol(self, addr):
       return EchoClient(self.message)

   def clientConnectionFailed(self, connector, reason):
       print "Connection failed."
       reactor.stop()

   def clientConnectionLost(self, connector, reason):
       print "Connection lost."
       reactor.stop()

def sendMessage(message):
    reactor.connectTCP("localhost", 8000, EchoFactory(message))
    reactor.run()

if __name__ == "__main__":
    while True:
        r = raw_input(">")
        if r == 'q' or len(r) == 0: break
        sendMessage(r)

可能有什么问题?这是错误信息.

What might be wrong? this is the error message.

>a
Server said: a
Connection lost.
>b
Traceback (most recent call last):
  File "echoclient.py", line 38, in <module>
    sendMessage(r)
  File "echoclient.py", line 32, in sendMessage
    reactor.run()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 1168, in run
    self.startRunning(installSignalHandlers=installSignalHandlers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 1148, in startRunning
    ReactorBase.startRunning(self)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/Extras/lib/python/twisted/internet/base.py", line 680, in startRunning
    raise error.ReactorNotRestartable()
twisted.internet.error.ReactorNotRestartable

推荐答案

出于我自己的目的,使用套接字向服务器发送消息工作正常.

For my own purposes, using socket to send messages to server works fine.

import socket
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(('localhost', 8000))
while True:
    a = raw_input("> ")
    if a == 'q' or len(a) == 0:
        client_socket.close()
        break
    else:
        client_socket.send(a)

这篇关于从扭曲的客户端向服务器发送多条消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 07:26