Python-华为重新启动脚本

Python-华为重新启动脚本,python,linux,huawei-mobile-services,Python,Linux,Huawei Mobile Services,我发现了一篇关于华为移动路由器的有趣文章: 在第二条评论中,一个名为rvl的人提供了脚本,以便在需要时通过API自动重启 我试着自己修复压痕。这是一个结果 我不确定这是否正确。我甚至不知道应该使用哪个版本的Python来运行它 sabbath@dell ~> /usr/bin/python2 router-reboot-script.py Traceback (most recent call last): File "router-reboot-script.py"

我发现了一篇关于华为移动路由器的有趣文章: 在第二条评论中,一个名为rvl的人提供了脚本,以便在需要时通过API自动重启

我试着自己修复压痕。这是一个结果 我不确定这是否正确。我甚至不知道应该使用哪个版本的Python来运行它

sabbath@dell ~> /usr/bin/python2 router-reboot-script.py
Traceback (most recent call last):
  File "router-reboot-script.py", line 6, in <module>
    import requests
ImportError: No module named requests
我没有Python技能。有人能帮我弄清楚怎么操作吗

编辑 我应该使用哪种版本的python,以及应该使用哪种类型的参数(如-m)?

两个问题:

  • 它是Python2.x,您在Python3中看不到来自_ufuture _; import print_函数的
  • 粘贴箱中的代码存在一些格式问题。Python使用空格来指示哪些代码块被分组到函数、类等中。空格不太正确。我已经修复了下面的问题(另请参见)
  • 代码:

    最后,请注意,最后10行(除了空行和######之外)需要出于您自己的目的进行更新。您需要为路由器设置正确的
    WEB
    USERNAME
    PASS
    。然后,您需要取消注释以
    开头的3行,如果[uuuuu name\uuuuuu==“\uuuuuuu main\uuuuuu”:
    正如我上面所做的那样

    如果由于缺少请求包而仍然出现错误,请查看两个问题:

  • 它是Python2.x,您在Python3中看不到来自_ufuture _; import print_函数的
  • 粘贴箱中的代码存在一些格式问题。Python使用空格来指示哪些代码块被分组到函数、类等中。空格不太正确。我已经修复了下面的问题(另请参见)
  • 代码:

    最后,请注意,最后10行(除了空行和######之外)需要出于您自己的目的进行更新。您需要为路由器设置正确的
    WEB
    USERNAME
    PASS
    。然后,您需要取消注释以
    开头的3行,如果[uuuuu name\uuuuuu==“\uuuuuuu main\uuuuuu”:
    正如我上面所做的那样


    如果由于缺少请求包而仍然出现错误,请检查第一个错误中的

    ,看起来您缺少请求模块。您是否尝试过
    pip install requests
    ,然后再次运行它?根据问题的答案进行编辑。从第一个错误开始,您似乎缺少requests模块。您是否尝试过
    pip安装请求
    ,然后再次运行它?编辑了问题的答案。
    [sabbath@dell ~]$ python -m router-reboot-script.py
    /usr/bin/python: Error while finding spec for 'router-reboot-script.py' (AttributeError: module 'router-reboot-script' has no attribute '__path__')
    
    [sabbath@dell ~]$ sudo pip install requests
    Requirement already satisfied (use --upgrade to upgrade): requests in /usr/lib/python3.5/site-packages  
    You are using pip version 8.1.2, however version 9.0.1 is available.  
    You should consider upgrading via the 'pip install --upgrade pip' command.  
    [sabbath@dell ~]$ sudo pip install --upgrade pip
    Collecting pip  
    Downloading pip-9.0.1-py2.py3-none-any.whl (1.3MB)  
    100% |████████████████████████████████| 1.3MB 686kB/s 
    Installing collected packages: pip
    Found existing installation: pip 8.1.2
    Uninstalling pip-8.1.2:
    Successfully uninstalled pip-8.1.2
    Successfully installed pip-9.0.1
    [sabbath@dell ~]$ sudo pip install requests
    Requirement already satisfied: requests in /usr/lib/python3.5/site-packages
    [sabbath@dell ~]$ python -m router-reboot-script.py
    /usr/bin/python: Error while finding spec for 'router-reboot-script.py' (AttributeError: module 'router-reboot-script' has no attribute '__path__')
    [sabbath@dell ~]$ python router-reboot-script.py
    
    ###########################
    #!/usr/bin/python
    
    from __future__ import print_function
    
    import requests
    import re
    import hashlib
    import base64
    
    
    def login(baseurl, username, password):
        s = requests.Session()
        r = s.get(baseurl + "html/index.html")
        csrf_tokens = grep_csrf(r.text)
        s.headers.update({'__RequestVerificationToken': csrf_tokens[0]})
    
        # test token on statistics api
        # r = s.get(baseurl + "api/monitoring/statistic-server")
    
        data = login_data(username, password, csrf_tokens[0])
        r = s.post(baseurl + "api/user/login", data=data)
    
        s.headers.update({'__RequestVerificationToken': r.headers["__RequestVerificationTokenone"]})
        return s
    
    
    def reboot(baseurl, session):
        s.post(baseurl + "api/device/control", data='1')
    
    
    def grep_csrf(html):
        pat = re.compile(r".*meta name=\"csrf_token\" content=\"(.*)\"", re.I)
        matches = (pat.match(line) for line in html.splitlines())
        return [m.group(1) for m in matches if m]
    
    
    def login_data(username, password, csrf_token):
        def encrypt(text):
            m = hashlib.sha256()
            m.update(text)
            return base64.b64encode(m.hexdigest())
    
        password_hash = encrypt(username + encrypt(password) + csrf_token)
        return '%s%s4' % (username, password_hash)
    
    
    WEB = "http://192.168.1.1/"
    USERNAME = "admin"
    PASSWORD = "admin"
    
    if __name__ == "__main__":
        s = login(WEB, USERNAME, PASSWORD)
    reboot(WEB, s)
    #########################