Python Forum

Full Version: convert sh script to python script
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
hi, i have a sh script code and need to convert it into python script.
i m struggling finding the correspondant command.


so here is my sh script

    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"
so i m returning the first 3 most recent results of my html page with the different specific tag


i want to write a script in python which can do the same

thanks

I tried Requests from python library to access the session ID and the Token, so that i can access the next page but i m facing troubles while trying to pass this parameters to requests.get


#!/usr/bin/env python
from urllib.request import urlopen
import urllib.request
import urllib.parse
import requests
from bs4 import BeautifulSoup

#response = Connection('http://192.168.8.1/api/webserver/SesTokInfo')

session = requests.Session()

response1= 'http://192.168.8.1/api/webserver/SesTokInfo'
page1= requests.get(response1)
result1 = BeautifulSoup(page1.text, 'html.parser')

for p in result1.find_all('sesinfo'):
    cookie=p.text
for q in result1.find_all('tokinfo'):
    token=q.text


headers = {
    'X-Requested-With': 'XMLHttpRequest',
    '__RequestVerificationToken':token,
    'Content-Type': 'text/xml'
}

data="<request><PageIndex>1</PageIndex><ReadCount>3</ReadCount><BoxType>1</BoxType><SortType>0</SortType><Ascending>0</Ascending><UnreadPreferred>1</UnreadPreferred></request>"

#response = requests.get('http://192.168.8.1/api/sms/sms-list', headers=headers, cookies=cookie, data=data)
result = BeautifulSoup(response.text, 'html.parser')

for r in result:
    print(r)