Reputation: 847
so i want to read some sms received in my huawei modem. for this i have to take a token value from one page and reuse it in another pages (sms-list) of my modem
but i got this error 125002 which means that my token value is not accurate
here is my code
import hashlib
import base64
import binascii
import xml.etree.ElementTree as ET
from datetime import datetime
import requests
from bs4 import BeautifulSoup
BASEURL = 'http://192.168.8.1'
session = requests.Session()
reqresponse = session.get(BASEURL + '/api/webserver/SesTokInfo')
if reqresponse.status_code == 200:
root = ET.fromstring(reqresponse.text)
for results in root.iter('SesInfo'):
sessionid = results.text
print("the sessionId is", sessionid)
for results in root.iter('TokInfo'):
token = results.text
print("The token is", token)
sessioncookies = reqresponse.cookies
post_data = '<?xml version = "1.0" encoding = "UTF-8"?>\n'
post_data += '<request><PageIndex>1</PageIndex><ReadCount>3</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>\n'
headers = {'Content-Type': 'text/xml; charset=UTF-8',
'__RequestVerificationToken': token,'X-Requested-With: XMLHttpRequest'}
api_url = BASEURL + '/api/sms/sms-list'
logonresponse = session.post( api_url, data=post_data, headers=headers, cookies=sessioncookies)
result = BeautifulSoup(logonresponse.text, 'html.parser')
for r in result:
print(r)
from this bash script, i m getting all my message list and it is almost the same principle
RESPONSE=`curl -s -X GET http://192.168.8.1/api/webserver/SesTokInfo`
COOKIE=`echo "$RESPONSE"| grep SessionID=| cut -b 10-147`
TOKEN=`echo "$RESPONSE"| grep TokInfo| cut -b 10-41`
DATA="<request><PageIndex>1</PageIndex><ReadCount>3</ReadCount>
<BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending>
<UnreadPreferred>1</UnreadPreferred></request>"
curl -b $COOKIE -c $COOKIE -H "X-Requested-With: XMLHttpRequest" --data
"$DATA" http://192.168.8.1/api/sms/sms-list --header
"__RequestVerificationToken: $TOKEN" --header "Content-Type:text/xml"
what did i missed in python please?
Upvotes: 2
Views: 4452
Reputation: 847
i finally solve my problem, obviously i was not getting the right token and session id value.
here is my final code
import hashlib
import base64
import binascii
import xml.etree.ElementTree as ET
from datetime import datetime
import requests
from bs4 import BeautifulSoup
import xmltodict
import os
BASEURL = 'http://192.168.8.1'
session = requests.Session()
reqresponse = session.get(BASEURL + '/api/webserver/SesTokInfo')
if reqresponse.status_code == 200:
_dict = xmltodict.parse(reqresponse.text).get('response', None) #here is the correct method to get sessionid and token values
post_data = '<?xml version = "1.0" encoding = "UTF-8"?>\n'
post_data += '<request><PageIndex>1</PageIndex><ReadCount>'+nb+'</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>\n'
headers = {'Content-Type': 'text/xml; charset=UTF-8','Cookie': _dict['SesInfo'],
'__RequestVerificationToken': _dict['TokInfo']
} ' in the header i m using the correct values of sessionId and Token
api_url = BASEURL + '/api/sms/sms-list'
logonresponse = session.post( api_url, data=post_data, headers=headers)
result = BeautifulSoup(logonresponse.text, 'html.parser')
for r in result:
print(r)
Upvotes: 6