Python 解析来自路由器的数据时发生授权错误

Python 解析来自路由器的数据时发生授权错误,python,mechanize,router,Python,Mechanize,Router,我想从我的路由器上删除一些数据用于家庭自动化,但我面临一些我无法解决/破解的问题 我已成功登录到路由器,但在使用python脚本访问数据(在路由器web界面中打开链接)时,我收到一个错误消息:您无权访问此路由器 如果我手动将python脚本正在访问的url复制并粘贴到浏览器中(设置了cookies),则响应是相同的。但如果我点击路由器网络界面内的按钮,我不会收到“权威”投诉。有没有办法解决这个问题 以下是脚本: import re import mechanize import cookieli

我想从我的路由器上删除一些数据用于家庭自动化,但我面临一些我无法解决/破解的问题

我已成功登录到路由器,但在使用python脚本访问数据(在路由器web界面中打开链接)时,我收到一个错误消息:您无权访问此路由器

如果我手动将python脚本正在访问的url复制并粘贴到浏览器中(设置了cookies),则响应是相同的。但如果我点击路由器网络界面内的按钮,我不会收到“权威”投诉。有没有办法解决这个问题

以下是脚本:

import re
import mechanize
import cookielib

br = mechanize.Browser()

cookies = cookielib.LWPCookieJar()
br.set_cookiejar(cookies)
#they "encrypt" the username and password and store it into the cookie. I stole this value from javascript in runtime.
br.addheaders = [('Cookie','Authorization=Basic YWRtaW46MjEyMzJmMjk3YTU3YTVhNzQzODk0YTBlNGE4MDFmYzM=;')] 
#open connection to the router address
br.open('http://192.168.1.1/')
#the only form is "login" form (which we dont have to fill up, because we already have the cookie)
br.select_form(nr=0) 
br.form.enctype = "application/x-www-form-urlencoded"
br.submit() 

#then the router returns redirect script, so we have to parse it (get the url).
redirect_url = re.search('(http:\/\/[^"]+)',br.response().read()).group(1)
token = re.search("1\/([A-Z]+)\/",redirect_url).group(1) #url always has a random token inside (some kind of security?)

#So with this url I should be able to navigate to page containing list of DHCP clients
br.open("http://192.168.1.1/"+token+"/userRpm/AssignedIpAddrListRpm.htm")

print(br.response().read()) #But response contains html saying "You have no authority to access this router!".

我通过添加以下内容解决了此问题:

br.addheaders.append(
    ('Referer', "http://192.168.1.1/userRpm/LoginRpm.htm?Save=Save")
)
原因:

在网上搜索这条消息时,我浏览到一个论坛,在那里,使用firefox版本(旧版)的用户抱怨同样的警告。修复方法是让推荐人能够被发送,所以我在脚本中也做了同样的操作,结果成功了