Python对requests.get使用auth=(user, pass)不起作用

2024-09-28 13:23:17 发布

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

当我将凭据作为URL的一部分发送时,我会得到正确的响应:

requests.get('<URL>?user=admin+pass=admin', params={"cmd": "getpower"})

response.code = 200
response.text ==> P61=0,P62=0,P63=1,P64=0,P65=1,P66=1,P67=1,P68=1

但是,当尝试使用auth=(user,pass)执行此操作时,它不起作用:

^{pr2}$

Tags: textcmdurlgetadminresponsecodepass
1条回答
网友
1楼 · 发布于 2024-09-28 13:23:17

Comment: the order of parameters in URL is critical

还有一个区别,paramencoded,因此=变成{}:

# '<URL>?user=admin+pass=admin'
url:http://httpbin.org/anything?user=admin+pass=admin&cmd=getpower

# requests.get(url, params=params
url:http://httpbin.org/anything?user=admin+pass%3Dadmin&cmd=getpower

# `dict` is **not** Ordered, it's possible to become reversed
url:http://httpbin.org/anything?cmd=getpower&user=admin+pass%3Dadmin

要克服这个问题,请使用:

^{pr2}$

结论:我认为没有办法克服作为URL一部分的传递。
假设服务器坏了? 阅读相关的SO post-request-failing-errno-10054


Comment;: ... trying to add User+Pass to params, did not work

我忽略了您使用的是Plus而不是&;,因此auth必须是:

^{3}$

同时尝试:

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

Question: requests.get using auth=(user, pass) not working

服务器似乎不支持postauth=。在

作为URL的一部分发送:

Note: Using get and as Part of the URL is unsecure.

url = 'http://httpbin.org/anything'
auth = {'user': 'admin pass=admin'}
params = {"cmd": "getpower"}
params.update(auth)
r = requests.get(url, params=params)

用Python:3.4.2测试

相关问题 更多 >

    热门问题