我正在通过python socketIO_client运行serval https web测试(然后将代码导入到robotframework中)以获取特定的socketIO通知。但有时候,连接因等待连接而死亡。
可以看到错误,例如:
192.168.0.1:7777/socket.io [连接错误] recv由SSL断开连接([SSL:SSLV3_ALERT_BAD_RECORD_MAC] sslv3警告错误记录mac(_ssl.c:1754))
192.168.0.1:7777/socket.io / restNotificationsPath [socket.io等待连接]
我的问题是如何让这些例外成功入睡一段时间然后重新连接?
我试图寻找解决方案,并找到两种可能的方法来解决这个问题。 1.使用命名空间,并覆盖on_connect()以检查连接状态。但我的检查方法不起作用。我检查命名空间is_connected和一些属性,但似乎它们不代表conecntion状态。 2.设置套接字连接时,使用池来保存会话,然后有一个问题,如何从机器人框架中重新连接?
附上我的代码:
import json
import sys
import logging
import time
import requests
from socketIO_client import SocketIO, BaseNamespace, ConnectionError
from robot.libraries.BuiltIn import BuiltIn
from robot.utils.asserts import fail
requests.packages.urllib3.disable_warnings()
logger = logging.getLogger(__name__)
class MyNotificationNamespace(BaseNamespace):
@classmethod
def setConnectedCallBackAndkwargs(cls, connectCallback):
if connectCallback :
cls.connectCallback = connectCallback
@classmethod
def setHitCallbackAndKwargs(cls, hitCallback):
if hitCallback:
cls.hitCallback = hitCallback
@classmethod
def setTargetEventType(cls, targetEventType):
cls.targetEventType = targetEventType
@classmethod
def setTargetEventContent(cls, targetEventContent):
cls.targetEventContent = targetEventContent
@classmethod
def setPath(cls, targetNotificationPath):
cls.path = targetNotificationPath
def on_connect(self):
self._io.hit = False
print('Connected to socket io, and set hit flag to False')
if self.connectCallback:
print "when connected, execute connect callback function without kwargs"
#this will let robotframework execute keywords as callback
BuiltIn().run_keyword(self.connectCallback)
def on_event(self, name, data):
if name == self.targetEventType and data === self.targetEventContent:
print "Hit, receive target notification, set hit flag to True"
self._io.hit = True
if self.hitCallback is not None:
print "execute hit callback, when capture the target notification"
#this will let robotframework execute keywords as callback
BuiltIn().run_keyword(self.hitCallback)
else:
logger.debug("This is not target notification: %r,data:%r" % (name, json.dumps(data)))
def setupSocketIO(serverIp, headers, targetEventType, targetEventContent, waitTimeout, connectCallback=None, hitCallback=None):
serverURL = 'https://' + serverIp
nameSpace = MyNotificationNamespace
nameSpace.setPath("/restNotificationsPath")
nameSpace.setTargetEventContent(targetEventContent)
nameSpace.setTargetEventType(targetEventType)
nameSpace.setConnectedCallBackAndkwargs(connectCallback)
nameSpace.setHitCallbackAndKwargs(hitCallback)
try:
with SocketIO(serverURL, 7777, verify=False, headers=headers) as socketIOInstance:
socketIOInstance.define(nameSpace, '/restNotificationsPath')
socketIOInstance.wait(int(waitTimeout))
if socketIOInstance.hit:
return socketIOInstance.hit
else:
return False
except ConnectionError as err:
print "Connection has error:" + str(err.message)
print "continue without block whole test"
pass