Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重新连接Netgear DGN2200v4的Python脚本_Python_Python 2.7 - Fatal编程技术网

重新连接Netgear DGN2200v4的Python脚本

重新连接Netgear DGN2200v4的Python脚本,python,python-2.7,Python,Python 2.7,我需要一个用于Netgear DGN2200v4的脚本,该脚本允许我每x秒更改一次ip。在线搜索我发现以下代码: #!/usr/bin/env python2.7 import re import urllib2 from base64 import b64encode from time import sleep BASE_URL = 'http://192.168.0.1/' USERNAME = 'admin' PASSWORD = 'admin' id_regex = re

我需要一个用于Netgear DGN2200v4的脚本,该脚本允许我每x秒更改一次ip。在线搜索我发现以下代码:

    #!/usr/bin/env python2.7

import re
import urllib2
from base64 import b64encode
from time import sleep

BASE_URL = 'http://192.168.0.1/'
USERNAME = 'admin'
PASSWORD = 'admin'

id_regex = re.compile('setup.cgi\?id=(\w+)"')

auth = "Basic %s" % b64encode("%s:%s" % (USERNAME, PASSWORD))

def load_html(url, data = None):
    request = urllib2.Request(BASE_URL + url, data,
                              {"Authorization": auth})
    result = urllib2.urlopen(request)
    return result.read()

# Evito eccezione Unauthorized
try:
    load_html('')
except urllib2.HTTPError:
    pass

# Chiamo la schermata di riepilogo
html = load_html('setup.cgi?next_file=RST_st_poe.htm')
ID = id_regex.search(html).group(1)

# Lancio la disconnessione
load_html('setup.cgi?id=' + ID,
          'todo=disconnect&this_file=RST_st_poe.htm&next_file=RST_st_poe.htm&SID=')

# Attendo 3 secondi
sleep(3)

# Chiamo la schermata di riepilogo
html = load_html('setup.cgi?next_file=RST_st_poe.htm')
ID = id_regex.search(html).group(1)

# Lancio la riconnessione
load_html('setup.cgi?id=' + ID,
          'todo=connect&this_file=RST_st_poe.htm&next_file=RST_st_poe.htm&SID=')

# Attendo 7 secondi
sleep(7)

# Logout
load_html('setup.cgi?todo=logout')

但它在我的电脑上不工作。 我已经用我的凭证更改了“用户名”和“密码”,并设置了PPPOE协议,这是日志

Traceback (most recent call last):
  File "C:\Users\x\Documents\dgnd3700.py", line 31, in <module>
    html = load_html('setup.cgi?next_file=RST_st_poe.htm')
  File "C:\Users\x\Documents\dgnd3700.py", line 19, in load_html
    result = urllib2.urlopen(request)
  File "C:\Python27\lib\urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "C:\Python27\lib\urllib2.py", line 397, in open
    response = meth(req, response)
  File "C:\Python27\lib\urllib2.py", line 510, in http_response
    'http', request, response, code, msg, hdrs)
  File "C:\Python27\lib\urllib2.py", line 435, in error
    return self._call_chain(*args)
  File "C:\Python27\lib\urllib2.py", line 369, in _call_chain
    result = func(*args)
  File "C:\Python27\lib\urllib2.py", line 518, in http_error_default
    raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 404: Not Found
回溯(最近一次呼叫最后一次):
文件“C:\Users\x\Documents\dgnd3700.py”,第31行,在
html=load\u html('setup.cgi?next\u file=RST\u st\u poe.htm'))
文件“C:\Users\x\Documents\dgnd3700.py”,第19行,以加载html格式
结果=urlib2.urlopen(请求)
文件“C:\Python27\lib\urllib2.py”,第126行,在urlopen中
return\u opener.open(url、数据、超时)
文件“C:\Python27\lib\urllib2.py”,第397行,打开
响应=方法(请求,响应)
文件“C:\Python27\lib\urllib2.py”,第510行,在http\u响应中
“http”、请求、响应、代码、消息、hdrs)
文件“C:\Python27\lib\urllib2.py”第435行出错
返回自我。调用链(*args)
文件“C:\Python27\lib\urllib2.py”,第369行,在调用链中
结果=func(*args)
文件“C:\Python27\lib\urllib2.py”,第518行,默认为http\u error\u
raise HTTPError(请求获取完整url(),代码,消息,hdrs,fp)
HTTPError:HTTP错误404:未找到

我怎样才能解决这个问题?感谢您的建议

从回溯来看,连接和授权似乎是成功的,但是

html = load_html('setup.cgi?next_file=RST_st_poe.htm')
不正确,因此出现404错误

可能安装wireshark或fiddler,启动跟踪,然后手动运行步骤(使用web浏览器),并查看请求的路径。然后可以修改load_html函数的条目,以反映找到的正确路径