如何从Python访问受密码保护的路由器页面?

如何从Python访问受密码保护的路由器页面?,python,passwords,authorization,Python,Passwords,Authorization,我想获取页面192.168.1.1/basic/home\u 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

我想获取页面
192.168.1.1/basic/home\u 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()
基本上,您需要知道维护会话的是哪一个

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

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


最有用的键值是
授权

我所见过的每一个家庭路由器都使用basic auth进行身份验证。这只是随请求一起发送的另一个标头。每次请求页面时,用户名和密码都会作为标题发送到服务器,并在服务器上验证每个请求

我建议将
请求
库置于
urlib2
之上

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"

路由器的详细信息会很受欢迎。我用身份验证解决了这个问题。但是我可以通过请求自动点击按钮吗?就像一些网站有我同意按钮。