我找到了一种方法,通过打开Stream=True请求(我使用请求插件(,通过ISAPI监听来自家庭访问控制器的事件。这是代码:
response = requests.get(url, stream=True, headers=headers, auth=HTTPDigestAuth(user, pwd))
for chunk in response:
logger.info(str(chunk))
问题:我无法解析json变量,因为块的长度不同(请参阅日志(。有一个";心跳块";我能够解码谁(使用json.loads(((,在将3个以上的块转换为连续字符串后,从字符中清除它,并根据start{}和stop对其进行修剪。它对心跳有效,但如果我有更多的裂缝,它就会失败。如何读取初始chunksize(写在chunk中(,然后在下载完成后用json解析它?(请注意,这是一个无限的下载(。
这是";心跳":
2022-07-26 18:09:11,505 | INFO | -
2022-07-26 18:09:11,505 | INFO | - Content-Length: 185
2022-07-26 18:09:11,506 | INFO | -
2022-07-26 18:09:11,506 | INFO | -
2022-07-26 18:09:11,506 | INFO | -
2022-07-26 18:09:11,507 | INFO | - {
2022-07-26 18:09:11,507 | INFO | - "ipAddress": "192.168.4.5",
2022-07-26 18:09:11,508 | INFO | - "dateTime": "2022-07-26T18:09:09+1:00",
2022-07-26 18:09:11,508 | INFO | - "activePostCount": 1,
2022-07-26 18:09:11,509 | INFO | - "eventType": "videoloss",
2022-07-26 18:09:11,509 | INFO | - "eventState": "inactive",
2022-07-26 18:09:11,510 | INFO | - "eventDescription": "videoloss alarm"
2022-07-26 18:09:11,510 | INFO | - }
2022-07-26 18:09:11,511 | INFO |
以及一些事件(有+150个编码事件(
2022-07-26 18:09:11,523 | INFO | 2022-07-26 18:09:11.523463 - --boundary
2022-07-26 18:09:11,524 | INFO | 2022-07-26 18:09:11.524463 -
2022-07-26 18:09:11,550 | INFO | 2022-07-26 18:09:11.550174 - Content-Type: application/json; charset="UTF-8"
2022-07-26 18:09:11,550 | INFO | 2022-07-26 18:09:11.550174 -
2022-07-26 18:09:11,551 | INFO | 2022-07-26 18:09:11.551179 - Content-Length: 349
2022-07-26 18:09:11,552 | INFO | 2022-07-26 18:09:11.552172 -
2022-07-26 18:09:11,552 | INFO | 2022-07-26 18:09:11.552172 -
2022-07-26 18:09:11,552 | INFO | 2022-07-26 18:09:11.552172 -
2022-07-26 18:09:11,553 | INFO | 2022-07-26 18:09:11.553171 - {"ipAddress": "192.168.4.5",
2022-07-26 18:09:11,554 | INFO | 2022-07-26 18:09:11.554185 - "dateTime": "2022-07-26T11:01:01+1:00",
2022-07-26 18:09:11,554 | INFO | 2022-07-26 18:09:11.554185 - "activePostCount": 1,
2022-07-26 18:09:11,554 | INFO | 2022-07-26 18:09:11.554185 - "eventType": "AccessControllerEvent",
2022-07-26 18:09:11,555 | INFO | 2022-07-26 18:09:11.555171 - "eventState": "active",
2022-07-26 18:09:11,556 | INFO | 2022-07-26 18:09:11.556171 - "eventDescription": "Access Controller Event",
2022-07-26 18:09:11,556 | INFO | 2022-07-26 18:09:11.556171 - "AccessControllerEvent" : {
2022-07-26 18:09:11,557 | INFO | 2022-07-26 18:09:11.557171 - "majorEventType": 5,
2022-07-26 18:09:11,557 | INFO | 2022-07-26 18:09:11.557171 - "subEventType": 22,
2022-07-26 18:09:11,558 | INFO | 2022-07-26 18:09:11.558171 - "doorNo": 2,
2022-07-26 18:09:11,558 | INFO | 2022-07-26 18:09:11.558171 - "serialNo": 1446,
2022-07-26 18:09:11,558 | INFO | 2022-07-26 18:09:11.558171 - "currentEvent": true,
2022-07-26 18:09:11,559 | INFO | 2022-07-26 18:09:11.559171 - "frontSerialNo": 1445
2022-07-26 18:09:11,559 | INFO | 2022-07-26 18:09:11.559171 - }
2022-07-26 18:09:11,559 | INFO | 2022-07-26 18:09:11.559171 - }
2022-07-26 18:09:11,559 | INFO | 2022-07-26 18:09:11.559171 -
2022-07-26 18:09:11,560 | INFO | 2022-07-26 18:09:11.560172 -
2022-07-26 18:09:11,560 | INFO | 2022-07-26 18:09:11.560172 -
2022-07-26 18:09:11,560 | INFO | 2022-07-26 18:09:11.560172 - --boundary
2022-07-26 18:09:11,560 | INFO | 2022-07-26 18:09:11.560172 -
2022-07-26 18:09:11,597 | INFO | 2022-07-26 18:09:11.597970 - Content-Type: application/json; charset="UTF-8"
2022-07-26 18:09:11,597 | INFO | 2022-07-26 18:09:11.597970 -
2022-07-26 18:09:11,598 | INFO | 2022-07-26 18:09:11.598970 - Content-Length: 494
2022-07-26 18:09:11,599 | INFO | 2022-07-26 18:09:11.599971 -
2022-07-26 18:09:11,599 | INFO | 2022-07-26 18:09:11.599971 -
2022-07-26 18:09:11,600 | INFO | 2022-07-26 18:09:11.600971 -
2022-07-26 18:09:11,600 | INFO | 2022-07-26 18:09:11.600971 - {"ipAddress": "192.168.4.5",
2022-07-26 18:09:11,601 | INFO | 2022-07-26 18:09:11.601971 - "dateTime": "2022-07-26T11:01:12+1:00",
2022-07-26 18:09:11,602 | INFO | 2022-07-26 18:09:11.602971 - "activePostCount": 1,
2022-07-26 18:09:11,602 | INFO | 2022-07-26 18:09:11.602971 - "eventType": "AccessControllerEvent",
2022-07-26 18:09:11,603 | INFO | 2022-07-26 18:09:11.603971 - "eventState": "active",
2022-07-26 18:09:11,604 | INFO | 2022-07-26 18:09:11.604971 - "eventDescription": "Access Controller Event",
2022-07-26 18:09:11,604 | INFO | 2022-07-26 18:09:11.604971 - "AccessControllerEvent" : {
2022-07-26 18:09:11,605 | INFO | 2022-07-26 18:09:11.605972 - "majorEventType": 5,
2022-07-26 18:09:11,606 | INFO | 2022-07-26 18:09:11.606971 - "subEventType": 1,
2022-07-26 18:09:11,607 | INFO | 2022-07-26 18:09:11.607975 - "cardNo": "17477",
2022-07-26 18:09:11,609 | INFO | 2022-07-26 18:09:11.609973 - "cardType": 1,
2022-07-26 18:09:11,610 | INFO | 2022-07-26 18:09:11.610972 - "cardReaderNo": 1,
2022-07-26 18:09:11,610 | INFO | 2022-07-26 18:09:11.610972 - "doorNo": 1,
2022-07-26 18:09:11,612 | INFO | 2022-07-26 18:09:11.611971 - "employeeNo": 00000001,
2022-07-26 18:09:11,612 | INFO | 2022-07-26 18:09:11.612972 - "employeeNoString": 00000001,
2022-07-26 18:09:11,613 | INFO | 2022-07-26 18:09:11.613971 - "serialNo": 1447,
2022-07-26 18:09:11,615 | INFO | 2022-07-26 18:09:11.615032 - "userType": 1,
2022-07-26 18:09:11,615 | INFO | 2022-07-26 18:09:11.615531 - "currentVerifyMode": 4,
2022-07-26 18:09:11,615 | INFO | 2022-07-26 18:09:11.615531 - "currentEvent": true,
2022-07-26 18:09:11,616 | INFO | 2022-07-26 18:09:11.616540 - "frontSerialNo": 1446
2022-07-26 18:09:11,616 | INFO | 2022-07-26 18:09:11.616540 - }
2022-07-26 18:09:11,617 | INFO | 2022-07-26 18:09:11.617540 - }
2022-07-26 18:09:11,617 | INFO | 2022-07-26 18:09:11.617540 -
2022-07-26 18:09:11,618 | INFO | 2022-07-26 18:09:11.618541 -
2022-07-26 18:09:11,618 | INFO | 2022-07-26 18:09:11.618541 -
2022-07-26 18:09:11,618 | INFO | 2022-07-26 18:09:11.618541 - --boundary
2022-07-26 18:09:11,619 | INFO | 2022-07-26 18:09:11.619540 -
2022-07-26 18:09:11,642 | INFO | 2022-07-26 18:09:11.642569 - Content-Type: application/json; charset="UTF-8"
2022-07-26 18:09:11,643 | INFO | 2022-07-26 18:09:11.643429 -
2022-07-26 18:09:11,643 | INFO | 2022-07-26 18:09:11.643928 - Content-Length: 349
2022-07-26 18:09:11,644 | INFO | 2022-07-26 18:09:11.644428 -
2022-07-26 18:09:11,644 | INFO | 2022-07-26 18:09:11.644928 -
2022-07-26 18:09:11,644 | INFO | 2022-07-26 18:09:11.644928 -
2022-07-26 18:09:11,646 | INFO | 2022-07-26 18:09:11.646067 - {"ipAddress": "192.168.4.5",
2022-07-26 18:09:11,648 | INFO | 2022-07-26 18:09:11.648031 - "dateTime": "2022-07-26T11:01:12+1:00",
2022-07-26 18:09:11,649 | INFO | 2022-07-26 18:09:11.649624 - "activePostCount": 1,
2022-07-26 18:09:11,650 | INFO | 2022-07-26 18:09:11.650457 - "eventType": "AccessControllerEvent",
2022-07-26 18:09:11,651 | INFO | 2022-07-26 18:09:11.650457 - "eventState": "active",
2022-07-26 18:09:11,652 | INFO | 2022-07-26 18:09:11.652130 - "eventDescription": "Access Controller Event",
2022-07-26 18:09:11,652 | INFO | 2022-07-26 18:09:11.652130 - "AccessControllerEvent" : {
2022-07-26 18:09:11,653 | INFO | 2022-07-26 18:09:11.653243 - "majorEventType": 5,
2022-07-26 18:09:11,653 | INFO | 2022-07-26 18:09:11.653586 - "subEventType": 21,
2022-07-26 18:09:11,653 | INFO | 2022-07-26 18:09:11.653985 - "doorNo": 1,
2022-07-26 18:09:11,654 | INFO | 2022-07-26 18:09:11.654182 - "serialNo": 1448,
2022-07-26 18:09:11,654 | INFO | 2022-07-26 18:09:11.654682 - "currentEvent": true,
2022-07-26 18:09:11,654 | INFO | 2022-07-26 18:09:11.654682 - "frontSerialNo": 1447
2022-07-26 18:09:11,655 | INFO | 2022-07-26 18:09:11.655686 - }
2022-07-26 18:09:11,655 | INFO | 2022-07-26 18:09:11.655686 - }
2022-07-26 18:09:11,655 | INFO | 2022-07-26 18:09:11.655686 -
2022-07-26 18:09:11,655 | INFO | 2022-07-26 18:09:11.655686 -
2022-07-26 18:09:11,656 | INFO | 2022-07-26 18:09:11.656823 -
2022-07-26 18:09:11,656 | INFO | 2022-07-26 18:09:11.656823 - --boundary
2022-07-26 18:09:11,657 | INFO | 2022-07-26 18:09:11.657543 -
2022-07-26 18:09:11,689 | INFO | 2022-07-26 18:09:11.689480 - Content-Type: application/json; charset="UTF-8"
2022-07-26 18:09:11,689 | INFO | 2022-07-26 18:09:11.689480 -
2022-07-26 18:09:11,690 | INFO | 2022-07-26 18:09:11.690481 - Content-Length: 349
2022-07-26 18:09:11,690 | INFO | 2022-07-26 18:09:11.690481 -
2022-07-26 18:09:11,690 | INFO | 2022-07-26 18:09:11.690481 -
2022-07-26 18:09:11,691 | INFO | 2022-07-26 18:09:11.691481 -
2022-07-26 18:09:11,691 | INFO | 2022-07-26 18:09:11.691481 - {"ipAddress": "192.168.4.5",
2022-07-26 18:09:11,691 | INFO | 2022-07-26 18:09:11.691481 - "dateTime": "2022-07-26T11:01:13+1:00",
2022-07-26 18:09:11,692 | INFO | 2022-07-26 18:09:11.692481 - "activePostCount": 1,
2022-07-26 18:09:11,692 | INFO | 2022-07-26 18:09:11.692481 - "eventType": "AccessControllerEvent",
2022-07-26 18:09:11,693 | INFO | 2022-07-26 18:09:11.693480 - "eventState": "active",
2022-07-26 18:09:11,693 | INFO | 2022-07-26 18:09:11.693480 - "eventDescription": "Access Controller Event",
2022-07-26 18:09:11,694 | INFO | 2022-07-26 18:09:11.694481 - "AccessControllerEvent" : {
2022-07-26 18:09:11,694 | INFO | 2022-07-26 18:09:11.694481 - "majorEventType": 5,
2022-07-26 18:09:11,695 | INFO | 2022-07-26 18:09:11.695481 - "subEventType": 22,
2022-07-26 18:09:11,696 | INFO | 2022-07-26 18:09:11.696499 - "doorNo": 1,
2022-07-26 18:09:11,697 | INFO | 2022-07-26 18:09:11.697488 - "serialNo": 1449,
2022-07-26 18:09:11,698 | INFO | 2022-07-26 18:09:11.698483 - "currentEvent": true,
2022-07-26 18:09:11,699 | INFO | 2022-07-26 18:09:11.698483 - "frontSerialNo": 1448
2022-07-26 18:09:11,699 | INFO | 2022-07-26 18:09:11.699482 - }
2022-07-26 18:09:11,699 | INFO | 2022-07-26 18:09:11.699482 - }
2022-07-26 18:09:11,700 | INFO | 2022-07-26 18:09:11.700483 -
2022-07-26 18:09:11,700 | INFO | 2022-07-26 18:09:11.700483 -
2022-07-26 18:09:11,701 | INFO | 2022-07-26 18:09:11.701481 -
2022-07-26 18:09:11,701 | INFO | 2022-07-26 18:09:11.701481 - --boundary
2022-07-26 18:09:11,701 | INFO | 2022-07-26 18:09:11.701481 -
2022-07-26 18:09:11,740 | INFO | 2022-07-26 18:09:11.740487 - Content-Type: application/json; charset="UTF-8"
2022-07-26 18:09:11,742 | INFO | 2022-07-26 18:09:11.742482 -
2022-07-26 18:09:11,743 | INFO | 2022-07-26 18:09:11.743482 - Content-Length: 390
2022-07-26 18:09:11,743 | INFO | 2022-07-26 18:09:11.743482 -
2022-07-26 18:09:11,744 | INFO | 2022-07-26 18:09:11.744482 -
2022-07-26 18:09:11,744 | INFO | 2022-07-26 18:09:11.744482 -
2022-07-26 18:09:11,745 | INFO | 2022-07-26 18:09:11.745482 - {"ipAddress": "192.168.4.5",
2022-07-26 18:09:11,746 | INFO | 2022-07-26 18:09:11.746481 - "dateTime": "2022-07-26T18:02:56+1:00",
2022-07-26 18:09:11,747 | INFO | 2022-07-26 18:09:11.747482 - "activePostCount": 1,
2022-07-26 18:09:11,747 | INFO | 2022-07-26 18:09:11.747482 - "eventType": "AccessControllerEvent",
2022-07-26 18:09:11,748 | INFO | 2022-07-26 18:09:11.748483 - "eventState": "active",
2022-07-26 18:09:11,749 | INFO | 2022-07-26 18:09:11.749489 - "eventDescription": "Access Controller Event",
2022-07-26 18:09:11,749 | INFO | 2022-07-26 18:09:11.749489 - "AccessControllerEvent" : {
2022-07-26 18:09:11,750 | INFO | 2022-07-26 18:09:11.750482 - "majorEventType": 3,
2022-07-26 18:09:11,751 | INFO | 2022-07-26 18:09:11.751486 - "subEventType": 112,
2022-07-26 18:09:11,752 | INFO | 2022-07-26 18:09:11.752482 - "netUser": "admin",
2022-07-26 18:09:11,753 | INFO | 2022-07-26 18:09:11.753481 - "remoteHostAddr": "192.168.4.3",
2022-07-26 18:09:11,753 | INFO | 2022-07-26 18:09:11.753481 - "serialNo": 1450,
2022-07-26 18:09:11,754 | INFO | 2022-07-26 18:09:11.754482 - "currentEvent": true,
2022-07-26 18:09:11,755 | INFO | 2022-07-26 18:09:11.754482 - "frontSerialNo": 1449
2022-07-26 18:09:11,755 | INFO | 2022-07-26 18:09:11.755482 - }
2022-07-26 18:09:11,757 | INFO | 2022-07-26 18:09:11.757488 - }
2022-07-26 18:09:11,758 | INFO | 2022-07-26 18:09:11.758482 -
2022-07-26 18:09:11,758 | INFO | 2022-07-26 18:09:11.758482 -
2022-07-26 18:09:11,759 | INFO | 2022-07-26 18:09:11.759483 -
我在寻找一种更有效的方法。。。。
jsChunkStart = '{'
jsChunkStop = '}'
chunkStart = 'Content-Type:'
chunkStop = chunkStart
tempchunk = ""
lastchunk = ""
headers = {
'Cache-Control': 'max-age=0',
'Connection': 'keep-alive',
}
response = requests.get(url, stream=True, headers=headers, auth=HTTPDigestAuth(user, pwd))
for chunk in response:
tempchunk += str(chunk)
tempchunk = tempchunk.replace("\r", "").replace("\n", "").replace("'b'", "")
#print ("print tempchunk:" + tempchunk)
if chunkStart in tempchunk :
print("found1")
tempstringright = tempchunk.split(chunkStart,1)[1]
#print ("nTEMPSTRINGRIGHT: " + tempstringright)
if chunkStop in tempstringright:
print("found2, processing")
#set 4 next cycle
tempchunk = chunkStop + tempstringright.split(chunkStop)[1]
#cut 4 js fetch
if jsChunkStop in tempstringright :
lastchunk = tempstringright.rsplit(jsChunkStop,1)[0] + jsChunkStop
lastchunk = jsChunkStart + lastchunk.split(jsChunkStart,1)[1]
print ("-----------PARSED----------------n"+ lastchunk + "nn")
日志:
found1
found1
found2, processing
-----------PARSED----------------
{"ipAddress": "192.168.4.5","dateTime": "2022-07-26T23:10:39+1:00","activePostCount": 1,"eventType": "videoloss","eventState": "inactive","eventDescription": "videoloss alarm"}
found1
found1
found2, processing
-----------PARSED----------------
{"ipAddress": "192.168.4.5","dateTime": "2022-07-26T23:10:43+1:00","activePostCount": 1,"eventType": "videoloss","eventState": "inactive","eventDescription": "videoloss alarm"}
found1
found1
found2, processing
-----------PARSED----------------
{"ipAddress": "192.168.4.5","dateTime": "2022-07-26T23:10:47+1:00","activePostCount": 1,"eventType": "videoloss","eventState": "inactive","eventDescription": "videoloss alarm"}
found1
found1
found2, processing
-----------PARSED----------------
{"ipAddress": "192.168.4.5","dateTime": "2022-07-26T23:10:51+1:00","activePostCount": 1,"eventType": "videoloss","eventState": "inactive","eventDescription": "videoloss alarm"}