2016-11-10 4 views
0

화웨이 모바일 라우터에 대한 흥미로운 기사를 찾았습니다. https://blog.hqcodeshop.fi/archives/259-Huawei-E5186-AJAX-API.html 두 번째 메모에서 rvl이라는 누군가가 필자의 스크립트를 제공하여 API로 자동 재부팅하도록했습니다.파이썬 - 화웨이 재부팅 스크립트

들여 쓰기를 직접 수정하려고했습니다. 결과는 다음과 같습니다 http://pastebin.com/KqF5RsS0 맞는지 확실하지 않습니다. 어떤 Python 버전을 사용해야하는지조차 모르겠습니다.

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

내가 사용해야 파이썬의 어떤 버전 및 매개 변수의 종류 (-m 같은)를 사용 하는가?

+3

체크 아웃이 보인다. 'pip install requests'를 시도한 다음 다시 실행 해 보셨습니까? –

+0

질문에 대한 답변과 함께 편집되었습니다. – sabbath

답변

1

두 가지 문제가 :

  1. 그것은 파이썬 2.x에서, 당신은 당신의 페이스트 빈에있는 코드에서 일부 서식 문제가 있었다 3.
  2. 파이썬에서 from __future__ import print_function 표시되지 않습니다. 파이썬은 공백을 사용하여 함수, 클래스 등으로 그룹화 된 코드 블록을 나타냅니다. 공백은 옳지 않습니다. 마지막으로

    ########################### 
    #!/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 개 라인이 필요 참고 : 나는

코드 (도 this pastebin 참조) 아래의 것을 수정했습니다 자신의 목적에 맞게 업데이트하십시오. 라우터에 올바른 WEB, USERNAMEPASS을 설정해야합니다. 그런 다음 위에서 수행 한대로 if __name__ == "__main__":으로 시작하는 3 줄의 주석 처리를 제거해야합니다. 당신이 요청 모듈을 놓치고처럼

당신이 요청 패키지 누락하고 있기 때문에 당신은 여전히 ​​오류를 받고있는 경우, 첫 번째 오류에서 the answer to this question.

관련 문제