在做视觉的时候,需要通过请求海康后台得到RTSP值
流程
1.从数据库获取相机编码(相机编码都为海康平台提供)
2.发送POST请求相机编码(这部分单位里JAVA使用海康的包进行转接请求)
3.对返回的数据正则化获取RTSP(相机编码正确+请求接口正确)
返回数据样例:
分别为含有RTSP(正确)、data:None 、未查到相机编码
{'code': 200, 'data': '{"code":"0","msg":"success","data":{"url":"rtsp://202.110.241.70:556/openUrl/z3dTw08"}}'} {'code': 200, 'data':'None'} {'code': 200, 'data': '{"code":"0x01b01301","msg":"Camera not found. cameraIndexCode not found, cameraIndexCode=24f5fd1866dd4367ac3d3146bf96629b","data":null}'}
逐步改进:
起初以为返回参数是肯定是含有rtsp的,结果后面出现了:'data':'None' 现象,自己就加了个判别条件:
RestInfo = self.UrlHighAltitudeIndexCode + CamId RestData = requests.post(RestInfo) RestDataDict = RestData.json() Datas = RestDataDict['data'] print(RestDataDict) RTSP = None if Datas is not None: Data_RestDataDict = ((eval(RestDataDict['data']))['data']) RTSP = Data_RestDataDict['url'] return RTSP
随着程序后面运行着报错,发现了:Camera not found. cameraIndexCode not found ,于是自己就又开始叠罗汉:
RestInfo = self.UrlHighAltitudeIndexCode + CamId RestData = requests.post(RestInfo) RestDataDict = RestData.json() Datas = RestDataDict['data'] print(RestDataDict) RTSP = None if Datas is not None: EoorCode = "0x01b01301" RightCode = EoorCode in Datas print(CamId, RightCode) if RightCode is False: Data_RestDataDict = ((eval(RestDataDict['data']))['data']) RTSP = Data_RestDataDict['url'] return RTSP
感觉自己写的是坨粑粑,深恶痛及,不过还好这会还好是可以用的。
良久感觉不能写坨屎出来,就算是坨屎,那也得是懒洋洋。
截止目前的一版,可算是通过是否含有rtsp来筛选RTSP了
data = {'cameraIndexCode': CamId} RestData = requests.post(self.UrlConstructionIndexCode, data=data) RestDataDict = RestData.json() # print(RestDataDict) RTSP = None RightInfo = "rtsp" in str(RestDataDict) if RightInfo is True: Data_RestDataDict = ((eval(RestDataDict['data']))['data']) RTSP = Data_RestDataDict['url'] return RTSP
不知道还能再优化下不