GET/POST 请求与额外的非命名参数使用 python 请求

问题描述 投票:0回答:0

我目前正在处理 Anel 电源插座管理器的实施。 Anel 电源插座支持以下请求: https://forum.anel.eu/viewtopic.php?f=52&t=888&sid=39081b8e472aaae7ffcec4cd3fb41e83

但是,指定在URL中分隔的串联用户密码逗号的http请求的特殊形式让我头疼,因为它似乎不受python请求包的支持。

因此我的问题是:
如何以其中一种形式发送 http 请求

  1. http://IP?param=value,userpassword
  2. http://IP?param=value&userpassword
  3. http://IP?param&userpassword

例如

  1. http://192.168.0.244?Stat&userpassword
  2. http://192.168.0.244?Sw=0xc0&userpassword

使用 python 请求包?我可以使用浏览器手动发送请求并获得适当的响应。但是,我无法以编程方式发送它。

我在语法上苦苦挣扎,因为请求文档对未命名参数一无所知(https://requests.readthedocs.io/en/latest/user/quickstart/#make-a-request)。

它告诉我如何使用字典或元组将参数作为键值对传递,或者如何在请求正文中传递参数或处理响应对象。但这一切都不是我的问题。 它只是关于发送“userpassword”字符串,它可以作为连接的明文传递,也可以在命名参数后用逗号分隔的相同 base64 编码传递。

req = requests.get("IP", {"Param": "Value"}, "userpassword")
Traceback (most recent call last):
  File "/home/user/pycharm-231.7864.77/plugins/python/helpers/pydev/pydevconsole.py", line 364, in runcode
    coro = func()
  File "<input>", line 1, in <module>
TypeError: get() takes from 1 to 2 positional arguments but 3 were given
req = requests.post("http://IP", {"Param": "Value"}, "userpassword")
req
<Response [401]>
req = requests.post("http://IP", {"Param": "Value", None: "userpassword"})
req
<Response [401]>

但是,手动组装 URL 是可行的。

req = requests.get("http://IP?Param=Value,userpassword", allow_redirects=False)
req.content
b'304 Redirect: /u_res.htm\r\n'

req2 = requests.get("http://IP/u_res.htm")
req2.text
"blablablabla"

不幸的是,如果我使用抽象层,我不希望我的代码依赖于请求格式的确切细节。

python python-3.x python-requests httprequest positional-parameter
© www.soinside.com 2019 - 2024. All rights reserved.