python的PUT模式请求,xls文件作为请求中的数据

2024-04-18 17:34:05 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Python中使用带有PUT模式的请求,以便将带有白名单的xls文件导入到相机。我的文件在我的桌面上

我已经在postman中测试了API,并要求postman用python导出我的API。我在启用摘要授权的postman中的命令如下所示

http://admin:*******@192.168.88.243/ISAPI/Traffic/channels/1/licensePlateAuditData

我已经在我的Python请求测试中尝试过它,如下面所示,并使用Report200OK

PUT http://admin:*******@192.168.200.108/ISAPI/Traffic/channels/1/licensePlateAuditData
Content-Type: text/plain

我已经使用postman在Curl和Python请求中翻译了我的API,结果如下所示

Python请求

import requests

url = "http://192.168.88.243/ISAPI/Traffic/channels/1/licensePlateAuditData"

payload = "<file contents here>"
headers = {
  'Content-Type': 'text/plain'
}

卷曲

curl --location --request PUT 'http://192.168.88.243/ISAPI/Traffic/channels/1/licensePlateAuditData' \
--header 'Content-Type: text/plain' \
--data-binary '@/C:/Users/gerki/Desktop/me=teNolist_192.168.88.243_20200407142606.xls'

response = requests.request("PUT", url, headers=headers, data = payload)

print(response.text.encode('utf8'))

我已经开始使用Python编写代码,首先确保我的授权首先使用GET命令,您可以在下面的代码中以注释模式看到该命令。授权运作良好

Python代码

ht4 = '/ISAPI/Traffic/channels/1/licensePlateAuditData'
ht1 = 'http://192.168.200.108'
ht3 = 'http://192.168.200.109'
ht = ht1 + ht4
htalt = ht3 + ht4

import requests

from requests.exceptions import HTTPError

for url in [ht, htalt]:
    try:
        from requests.auth import HTTPDigestAuth

        payload = "C:/Users/gerki/Desktop/me=teNolist_192.168.88.243_20200407142606.xls"
        headers = {
            'Content-Type': 'text/plain'
        }

        response = requests.request("PUT", url, data=payload, headers=headers, auth=HTTPDigestAuth('admin', 'OTE_2019'))

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!')
        response.encoding = 'utf-8'
        print(response.text)

然后我得到一个400错误,我怀疑这与python打开xls文件的方式有关。 我的错误代码是

HTTP error occurred: 400 Client Error: Bad Request for url: http://192.168.200.108/ISAPI/Traffic/channels/1/licensePlateAuditData

有人能解释一下我是否可以用某种方式来做这个程序吗?我需要在许多IP地址中应用代码,才能在许多CAM中应用xls文件。所以我需要python中的代码来创建可执行文件,因此POSTMAN不起作用


Tags: 文件代码texthttpurlputresponsexls
1条回答
网友
1楼 · 发布于 2024-04-18 17:34:05

最后,我发现了问题所在。文档声明在请求中使用头。我刚把它取下来,开始工作。我附上下面的代码

By the way, this answer is relevant to a very unique problem which is based on how to create a small software to change the Whitelist/blacklist of HIkvision Traffic/ANPR cameras. Up to now only use of the camera GUI or PUT commands through POSTMAN or similar systems could be used to do so.

因此,这个答案对于希望将海康威视SDK用于ANPR摄像机的许多人来说确实很有帮助

ht1 = '/ISAPI/ITC/capability'
ht2 = '/ISAPI/Traffic/channels/1/licensePlateAuditData'
ht3 = 'http://192.168.200.108'
ht4 = 'http://192.168.200.109'
htc = ht3 + ht2
htc2 = ht4 + ht2

import requests

from requests.exceptions import HTTPError

for url in [htc, htc2]:
    try:
        from requests.auth import HTTPDigestAuth

        #response = requests.get(url, auth=HTTPDigestAuth('<username>', ',password>'))
        payload = open("<filename with full path>", 'rb')

        headers = {
            'Content-Type': 'text/plain'
        }

        response = requests.put(url, data=payload, auth=HTTPDigestAuth('<username>', ',password>'))

        # If the response was successful, no Exception will be raised
        response.raise_for_status()
    except HTTPError as http_err:
        print(f'HTTP error occurred: {http_err}')  # Python 3.6
    except Exception as err:
        print(f'Other error occurred: {err}')  # Python 3.6
    else:
        print('Success!')
        response.encoding = 'utf-8'
        print(response.text)

相关问题 更多 >