Python身份验证返回401

时间:2020-08-19 17:23:36

标签: python python-3.x authentication

我有一个python脚本,可以从相机下载快照。我使用身份验证登录到相机。对于老式相机,它没有问题,而新相机则没有问题。我已经通过从python脚本中复制链接和凭据来测试链接和凭据,以确保它们可以正常工作,但是我仍然无法登录,并且不确定为什么。注释的URL是有效的URL。 uniview没有。我已经用正确的密码替换了密码,并且还在Chromium中测试了该链接,该链接仍然有效。

import requests

#hikvision old cameras
#url = 'http://192.168.100.110/ISAPI/Streaming/channels/101/picture'

#uniview
url = 'http://192.168.100.108:85/images/snapshot.jpg'

r = requests.get(url, auth=('admin','password'))

if r.status_code == 200:
    with open('/home/pi/Desktop/image.jpg', 'wb') as out:
        for bits in r.iter_content():
            out.write(bits)
else:
    print(r.status_code)
    print(r.content)

以下是我得到的回复

b'{\r\n"Response": {\r\n\t"ResponseURL": "/images/snapshot.jpg",\r\n\t"ResponseCode": 3,\r\n \t"SubResponseCode": 0,\r\n \t"ResponseString": "Not Authorized",\r\n\t"StatusCode": 401,\r\n\t"StatusString": "Unauthorized",\r\n\t"Data": "null"\r\n}\r\n}\r\n'

1 个答案:

答案 0 :(得分:1)

因此,hikvisio正在使用Basic_access_authentication,而uniview正在使用Digest_access_authentication,因此根据docs,您需要将请求更改为:< / p>

from requests.auth import HTTPDigestAuth
r = requests.get(url, auth=HTTPDigestAuth('admin','password'))