python - 尝试从华为调制解调器读取消息时出现错误 125002

标签 python python-3.x bash

所以我想阅读我的华为调制解调器中收到的一些短信。 为此,我必须从一个页面中获取一个 token 值,然后在我的调制解调器的另一页(短信列表)中重复使用它

但我得到了这个错误 125002,这意味着我的 token 值不准确

这是我的代码

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)

从这个 bash 脚本中,我得到了我所有的消息列表,这几乎是相同的原理

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"

请问我在 python 中错过了什么?

最佳答案

我终于解决了我的问题,显然我没有得到正确的 token 和 session ID 值。

这是我的最终代码

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)

关于python - 尝试从华为调制解调器读取消息时出现错误 125002,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58195192/

相关文章:

python - 用正则表达式替换单词列表

python - 解释 res = cv2.bitwise_and(img,img,mask = mask) 中的参数含义

python - 我可以在 y 系列中输入一个值以防止 matplotlib 绘制该 x 值的值吗?

python - 基于python的PostgreSQL文档

python - 在 Python 中使用数组更快的 for 循环

python - 将 dict 写入文件,每个键/值对位于单独的行上

linux - bash - 在系统初始化时执行 jar 并正常关闭

regex - Bash - 如何计算指令数?

python - 进程内存使用输出

linux - 如何在 Windows 10 开发者预览版中启用 Bash?