2016-11-10 97 views
0

我发现华为移动路由器有趣的文章: https://blog.hqcodeshop.fi/archives/259-Huawei-E5186-AJAX-API.html 在第二条评论中,有人命名rvl提供了他的脚本,以便通过API自动重启,如果需要的话。Python - 华为重启脚本

我试图自己修复缩进。这是一个结果http://pastebin.com/KqF5RsS0 我不确定它是否正确。甚至不知道我应该使用哪个版本的Python来运行它。

[email protected] ~> /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 

[[email protected] ~]$ 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__') 

我没有Python的技能。有人能帮我弄清楚如何运行它吗?

编辑

[[email protected] ~]$ 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. 
[[email protected] ~]$ 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 
[[email protected] ~]$ sudo pip install requests 
Requirement already satisfied: requests in /usr/lib/python3.5/site-packages 
[[email protected] ~]$ 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__') 
[[email protected] ~]$ python router-reboot-script.py 

我应该使用哪个版本的Python我应该使用的,什么样的参数(如-m)?

+3

从第一个错误,好像你就错过了请求模块。你尝试过'pip安装请求'然后再运行一次吗? –

+0

用您的问题的答案编辑。 – sabbath

回答

1

两个问题:

  1. 这是蟒蛇2.x中,你看不到from __future__ import print_function在Python 3
  2. 有在你的引擎收录代码中的一些格式问题。 Python使用空格来指示哪些代码块被分组到一起,形成函数,类等等。白色空间不太正确。我已经修复了以下(见this pastebin

代码:

########################### 
#!/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) 
######################### 

最后,需要注意的是最后10行(从空线和#####分开)需要为了您自己的目的而更新。您需要为您的路由器设置正确的WEB,USERNAMEPASS。然后,您需要取消注释从if __name__ == "__main__":开始的3条线,如上所述。

如果你仍然得到错误,因为你错过了请求包,检查出the answer to this question.