简体   繁体   English

在Twisted上实现每隔x秒调用一次服务器的客户端

[英]Implement Client which call to server every x secs on Twisted

I'm trying to create client app(using twisted) which will send some data(always the same) to the server. 我正在尝试创建客户端应用程序(使用双绞线),它将向服务器发送一些数据(始终相同)。 I need this for checking status of operations on server. 我需要这个来检查服务器上的操作状态。 But I can't figure out how to do this using Twisted. 但是我不知道如何使用Twisted做到这一点。

from twisted.internet.protocol import Protocol, ClientFactory
from twisted.internet import task
from sys import stdout
from twisted.internet import reactor

host = 'localhost'
port = 8007

class InstProtocol(Protocol):
    def __init__(self):
        with open('install_qeue.json','r') as jsonfile:
            self.json_data = jsonfile.read()
        jsonfile.close()
        self.json_data_status = self.json_data.replace('"end": 1', '"end": 2')

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

    def dataReceived(self, data):
        stdout.write(data)

    def sendMsg(self):
        self.transport.write(self.json_data_status)

class InstFactory(ClientFactory):
    protocol = InstProtocol

    def __init__(self):
        self.lc = task.LoopingCall(self.protocol.sendMsg)
        self.lc.start(10)

    def startedConnecting(self, connector):
        print 'Started to connect.'

    def buildProtocol(self, addr):
        print 'Connected.'
        return InstProtocol()

    def clientConnectionLost(self, connector, reason):
        print 'Lost connection.  Reason:', reason

    def clientConnectionFailed(self, connector, reason):
        print 'Connection failed. Reason:', reason

reactor.connectTCP(host, port, InstFactory())
reactor.run()

Error: 错误:

Unhandled error in Deferred:


Traceback (most recent call last):
  File ".\client.py", line 97, in <module>
    reactor.connectTCP(host, port, InstFactory())
  File ".\client.py", line 82, in __init__
    self.lc.start(10)
  File "C:\Python27\lib\site-packages\twisted\internet\task.py", line 168, in start
    self()
  File "C:\Python27\lib\site-packages\twisted\internet\task.py", line 213, in __call__
    d = defer.maybeDeferred(self.f, *self.a, **self.kw)
--- <exception caught here> ---
  File "C:\Python27\lib\site-packages\twisted\internet\defer.py", line 150, in maybeDeferred
    result = f(*args, **kw)
exceptions.TypeError: unbound method sendMsg() must be called with InstProtocol instance as first argument (got nothing
instead)
Started to connect.

how can I fix it, please advice. 我该如何解决,请指教。

Well, 好,

  1. put reactor into init of Factory 将反应堆放入工厂的初始化
  2. in dataReceived method of protocol write: self.factory.reactor.callLater(30, self.build_log, data) 在data接收的协议写入方法中:self.factory.reactor.callLater(30,self.build_log,data)
  3. callback function is a method of protocol where you need to specify: output message received + send another message. 回调函数是一种协议方法,您需要在其中指定:接收到的输出消息+发送另一条消息。

in this case every time you receive message from server - you will react in 30 seconds and send another one. 在这种情况下,每次您收到来自服务器的消息时,您都会在30秒内做出反应并发送另一个消息。 each tie server got you request - it will send response immediately. 每个联系服务器都收到了您的请求-它将立即发送响应。 that's how to handle this w\\o time.sleep() and while() loop. 这就是如何处理time.sleep()和while()循环的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM