2013-08-29 37 views
2

我想从路由器获取页面192.168.1.1 /basic/home_dhcplist.htm ,但它在开始时要求输入用户名和密码。如何从Python访问受密码保护的路由器页面?

我通过urllib2的

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() 
+0

路由器的细节将不胜感激 –

回答

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" 
+0

我解决了认证的问题。 但是,我可以使用请求自动点击一个按钮吗? 像一些网站我同意按钮。 – skarfa

0

获取Python中的页面基本上你需要set the cookie它保持该会话,最有可能。

通过浏览器(Firefox)访问该页面时提示输入登录通行证。

按下Ctrl键移-K,并重新加载页面,点击任何最近GET的请求,你会得到展示GET请求详细信息的窗口。请注意0​​并相应地设置cookie。

键值这将是最有用的是Authorization