【发布时间】:2017-06-01 08:20:10
【问题描述】:
我尝试使用 requests python 模块登录到托管交换机(使用静态 IPv4),以检索连接到它的设备的 mac 地址表(然后注销,显然)。使用python 2.7.10时,每次登录都有效,使用python3.4.4时,登录失败,但不是每次。我会说我有1/3的成功机会。
这是一个示例代码,显示了我的机器(Mac OS X 10.10.5)上的问题。
import sys
import requests
def login():
return requests.post(LOGIN_URL,
headers=HEADERS,
data=LOGIN_DATA,
verify=False,
timeout=10)
def load_table(cookie):
return requests.get(TABLE_URL,
headers=HEADERS,
cookies=cookie)
def logout(cookie):
return requests.post(LOGOUT_URL,
headers=HEADERS,
cookies=cookie,
data=LOGOUT_DATA)
if __name__ == "__main__":
print(sys.version_info)
print("requests version :", requests.__version__)
login_resp = login()
cookies = list()
for req in login_resp.history:
cookies.append(req.cookies)
if len(cookies) > 0:
my_cookie = cookies[0]
print("errormsg position:", login_resp.text.find("errormsg"))
# This line will raise an exception in case of login failure
print("cookie :\n", my_cookie)
my_table = load_table(my_cookie).text
print("mac table :\n", my_table)
logout(my_cookie)
备注:
为了清楚起见,我没有复制 http 变量(LOGIN_URL 等)。都是正确的,我用chrome开发者工具和wireshark检查过了。
该代码应该在 Python3 中使用,因此在 Python2 中的输出会非常难看(请记住,这只是一个演示代码)。
请求版本相同:2.12.4(最新)。
在 Python3 中登录失败时会引发
NameError异常,导致响应历史记录为 cookie 为空。在这种情况下,errormsg="login failed. Authentication failed"将出现在response.text属性中。我已经尝试过Python3.6.0(最新),仍然出现失败。
你们有任何线索吗?该错误是来自python3、请求还是底层操作系统?我是否错过了身份验证过程中的某些内容(我对 http 很陌生)。
编辑
我在 Windows 7 下尝试使用 Python3.4.3 并请求 2.12.4,但它每次都失败。
【问题讨论】:
-
如何关注
login path?如果您键入please can you eleborate this消息,此评论对您没有帮助。 -
@dsgdfg 很抱歉,我不明白这个问题,也不明白你想让我添加什么信息:/
标签: python macos python-2.7 python-3.x python-requests