Python Forum
convert sh script to python script
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
convert sh script to python script
#1
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)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problems passing arguments containing spaces to bash script and then on to python kaustin 6 393 Apr-03-2024, 08:26 PM
Last Post: deanhystad
  Using a script to open multiple shells? SuchUmami 9 499 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  How to include one script into another? MorningWave 8 474 Mar-21-2024, 10:34 PM
Last Post: MorningWave
  ChromeDriver breaking Python script genericusername12414 1 313 Mar-14-2024, 09:39 AM
Last Post: snippsat
  using PowerShell from Python script for mounting shares tester_V 8 530 Mar-12-2024, 06:26 PM
Last Post: tester_V
  No Internet connection when running a Python script basil_555 8 602 Mar-11-2024, 11:02 AM
Last Post: snippsat
Question Running Python script through Task Scheduler? Winfried 8 490 Mar-10-2024, 07:24 PM
Last Post: Winfried
  Combine console script + GUI (tkinter) dejot 2 429 Feb-27-2024, 04:38 PM
Last Post: deanhystad
  How to receive two passed cmdline parameters and access them inside a Python script? pstein 2 349 Feb-17-2024, 12:29 PM
Last Post: deanhystad
  OBS Script Troubleshooting Jotatochips 0 297 Feb-10-2024, 06:18 PM
Last Post: Jotatochips

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020