Skip to main content
 首页 » 编程设计

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

2024年11月01日11oomusou

我想获取页面 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() 

请您参考如下方法:

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

我建议使用 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"