Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Huawei API
#!/usr/bin/env python
from __future__ import unicode_literals
#from PyCRC.CRC16 import CRC16
import requests
import re
import hashlib
import base64
import os, sys
def changemode(baseurl, session):
print "Trying to change 4G mode..."
r= + "/api/net/net-mode", data=data, allow_redirects=False)
#r= session.get(baseurl + "/api/net/net-mode")
print r
def login(baseurl, username, password):
s = requests.Session()
r = s.get(baseurl + "html/home.html")
csrf_tokens = grep_csrf(r.text)
headers_update(s.headers, csrf_tokens[1])
data = login_data(username, password, str(csrf_tokens[1]))
r = s.request('POST', baseurl + "api/user/login", data=data, allow_redirects=False)
s.headers.update({'__RequestVerificationToken': r.headers["__requestverificationtokenone"]})
return s
def headers_update(dictbase, token):
dictbase['Accept-Language'] = 'en-US'
dictbase['Content-Type'] = 'application/x-www-form-urlencoded'
dictbase['X-Requested-With'] = 'XMLHttpRequest'
dictbase['__RequestVerificationToken'] = token
dictbase['Cache-Control'] = 'no-cache'
dictbase['User-Agent'] = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/600.8.9 (KHTML, like Gecko) Version/8.0.8 Safari/600.8.9'
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 [ for m in matches if m]
def login_data(username, password, csrf_token):
def encrypt(text):
m = hashlib.sha256()
return base64.b64encode(m.hexdigest())
password_hash = encrypt(username + encrypt(password) + csrf_token)
return '<?xml version "1.0" encoding="UTF-8"?><request><Username>%s</Username><Password>%s</Password><password_type>4</password_type></request>' % (username, password_hash)
baseurl = ""
username = "admin"
password = "admin"
if __name__ == "__main__":
s = login(baseurl, username, password)
changemode(baseurl, s)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.