2

我正在尝试编写一个 websocket 客户端,它从 websocket 服务器侦听某些事件并将它们转储到屏幕上。如果消息长度超过 250 个字符,我希望关闭连接。我为此编写了以下代码:

import time
from ws4py.client.threadedclient import WebSocketClient

WS_HOST='172.17.0.2:8081'
EVENTS_URL = 'ws://' + WS_HOST + '/events'
FAULTS_URL = 'ws://' + WS_HOST + '/faults'
ALARMS_URL = 'ws://' + WS_HOST + '/alarms'
ECHO_URL = 'ws://echo.websocket.org/'

class SNMPWebSocketClient(WebSocketClient):
    def opened(self):
        print 'Opened client at {0}'.format(self.url)

    def closed(self, code, reason=None):
        print "Closed down", code, reason

    def received_message(self, m):
        print m
        print 'Client={0}, server={0}'.format(self.client_terminated, self.server_terminated)
        print len(m)
        if len(m) >= 272:
            print 'Send close signal'
            self.close()
        print '\n\n'

if __name__ == '__main__':
    ws = SNMPWebSocketClient(EVENTS_URL)
    try:
        ws.connect()
        ws.run_forever()
    except KeyboardInterrupt:
        ws.close(code=1002, reason='kb yay Interrupt')
        time.sleep(1)
        print 'Kb Interrupt'

但是在聆听时,关闭从未通过。已终止的客户端和服务器标志显示为“真”,但客户端仍继续接收消息。我使用 close() 有什么问题吗?或者这是 websocket 客户端应该如何工作,直到它被明确杀死?

这是输出:

 $ python threaded_ws.py 
Opened client at ws://172.17.0.2:8081/events
{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"PortOperStateDown","Description":"Port Operational State DOWN Event","SrcObjName":"Port","EvtId":2,"TimeStamp":"2017-03-28T14:38:32.881456505Z","SrcObjKey":{"IntfRef":"eth25"},"AdditionalData":null}
Client=False, server=False
258



{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"VlanOperStateDown","Description":"Vlan Operational State DOWN Event","SrcObjName":"Vlan","EvtId":4,"TimeStamp":"2017-03-28T14:38:32.882046191Z","SrcObjKey":{"VlanId":3050},"AdditionalData":null}
Client=False, server=False
254



{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"IPv4IntfOperStateDown","Description":"IPv4 Interface Operational State DOWN Event","SrcObjName":"IPv4Intf","EvtId":6,"TimeStamp":"2017-03-28T14:38:32.883912357Z","SrcObjKey":{"IntfRef":"eth25"},"AdditionalData":null}
Client=False, server=False
276
Send close signal



{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"PortOperStateUp","Description":"Port Operational State UP Event","SrcObjName":"Port","EvtId":1,"TimeStamp":"2017-03-28T14:38:33.38139292Z","SrcObjKey":{"IntfRef":"eth25"},"AdditionalData":null}
Client=True, server=True
253



{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"VlanOperStateUp","Description":"Vlan Operational State UP Event","SrcObjName":"Vlan","EvtId":3,"TimeStamp":"2017-03-28T14:38:33.382071856Z","SrcObjKey":{"VlanId":3050},"AdditionalData":null}
Client=True, server=True
250



{"Enable":true,"OwnerId":1,"OwnerName":"ASICD","EventName":"IPv4IntfOperStateUp","Description":"IPv4 Interface Operational State UP Event","SrcObjName":"IPv4Intf","EvtId":5,"TimeStamp":"2017-03-28T14:38:33.382852609Z","SrcObjKey":{"IntfRef":"eth25"},"AdditionalData":null}
Client=True, server=True
272
Send close signal
4

0 回答 0