【问题标题】:How to access the password protected Router Page from Python?如何从 Python 访问受密码保护的路由器页面?
【发布时间】:2013-08-29 17:23:18
【问题描述】:

我要获取页面192.168.1.1 /basic/home_dhcplist.htm 来自路由器,但它在开始时要求输入用户名和密码。

我正在通过 urllib2 在 Python 中获取页面

import urllib2
response = urllib2.urlopen('http://192.168.1.1/basic/home_dhcplist.htm')
html = response.read()
str="Prasads"
value= html.find(str)
print value
if value!=-1 :
    print "found"
else:
print "not found"

response.close()

【问题讨论】:

  • 路由器的详细信息将不胜感激

标签: python passwords authorization


【解决方案1】:

我见过的每个家庭路由器都使用基本身份验证进行身份验证。这只是您与请求一起发送的另一个标头。每次您请求页面时,用户名和密码都会作为标头发送到服务器,并在每个请求中对其进行验证。

我会建议 requests 库而不是 urllib2

import requests

r = requests.get('http://192.168.1.1/basic/home_dhcplist.htm', auth=('username', 'password'))

if 'Prasads' in r.text():
    print "found"
else:
    print "not found"

【讨论】:

  • 我解决了身份验证问题。但是我可以使用请求自动点击按钮吗?像某些网站一样有我同意按钮。
【解决方案2】:

基本上你需要set the cookie 来维护会话,很可能。

通过浏览器 (Firefox) 访问页面,在提示时输入登录密码。

Ctrl-Shift-k,然后重新加载页面并单击任何最近的GET 请求,您将看到一个显示GET 请求详细信息的窗口。注意Request Headers 并相应地设置cookie。

最有用的键值Authorization

【讨论】:

    猜你喜欢
    • 2011-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 2011-11-29
    • 2019-01-25
    • 1970-01-01
    相关资源
    最近更新 更多