简体   繁体   中英

how to execute shell command files in python

I am having trouble executing the following shell command. I'm in windows 10. I had to delete the user authorization here and left it as 'tktk' as i dind't want my authorization out here like that.

curl 'https://pegasus-test.etflogic.io/portfolio/analyze' -H 'authority: pegasus-test.etflogic.io' -H 'pragma: no-cache' -H 'cache-control: no-cache' -H 'accept: application/json' -H 'authorization: tktk' -H 'user-agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36' -H 'content-type: application/json' -H 'origin: https://akita.etflogic.com' -H 'sec-fetch-site: cross-site' -H 'sec-fetch-mode: cors' -H 'referer: https://akita.etflogic.com/portfolio-analysis' -H 'accept-encoding: gzip, deflate, br' -H 'accept-language: en-US,en;q=0.9' --data-binary '{"portfolio":{"currency":"usd","id":"8576e266-c6f4-44fe-a6a6-9c64869b55dd","name":"test1","records":[{"locale":"US","id":"df92931e-3063-4889-8e4b-100c66f14d22","name":"SSGA SPDR S&P 500 - SSgA Active Trust","secid":549535,"size":100,"ticker":"SPY US"}],"size_type":"notional","type":"primary"}}' --compressed > /tmp/jsonpayload

I stored the above command in a test.sh file I'm having trouble executing the above shell command. It works on the linux terminal. But it doesn't work on my windows cmd. I need to read the above url, get the json object and do some analysis with it.

What i tried:

import subprocess 
ans = subprocess.Popen(["bash",os.path.join(os.getcwd(), 'test.sh')])

This fails. ok...

I tried then...

subprocess.call(['test.sh'])

I'm getting error '%1 is not a valid Win32 application'

I copied your test.sh code into my own test.sh file, and tested a few different things.

Try using os.system for calling your command instead of methods from subprocess .

This worked for me:

import os
os.system(os.getcwd() + '/test.sh')

If you prefer os.path.join(os.getcwd(), 'test.sh') instead of what I have here, you can use that too, there's no major difference.

I hope this solves your problem :)

try:

# Importing the necessary packages
$ from subprocess import call  

# Executing the test.sh script
$ call('sh test.sh 2>/dev/null', shell=True)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM