Python script - API call fails due to JSON arguments - python

I am trying to pull reports through a (shadowserver) API - doc here:
The python script is written by ShadowServer and I have tested it with the simple usage example they are providing:
$ ./call-api.py test/ping '{}'
{"pong":"2020-10-26 23:06:37"}
Thus far, everything works fine. I can submit additional calls and I get a response back.
However, when i try to pass a JSON parameter to the script (that is executed in command prompt), such as the example below:
os.system('''[some_directory]\\call-api.py reports/query '{ "help":true }' pretty''')
I get the following result:
JSON Exception: Expecting value: line 1 column 1 (char 0)
I believe that it is an issue with the quotes - single/double/triple.. but I've tried everything and it keeps returning the same result.
Any ideas?
Many thanks!

You might want to try subprocess to see if you get the same result, e.g. like this
import subprocess
subprocess.Popen([
"[some_directory]\\call-api.py",
"reports/query",
'{"help": true}',
"pretty",
])
or use json.dumps to not worry about any quoting
import subprocess
import json
subprocess.Popen([
"[some_directory]\\call-api.py",
"reports/query",
json.dumps({"help": True}),
"pretty",
])
SitiSchu came up with the json.dumps idea.

Related

Same command giving different results when run in CLI and when as an argument for os.system()

I'm using python to try and scrape twitter data using SNSCRAPE, and for that I need to run the following example command to get the output I want:
snscrape --max-results 100 twitter-search '#Tesla #Elonmusk' > scrapedtweets.txt
When run on the terminal, it sends the output directly to a text file named 'scrapedtweets'. But when I plug this string into os.system like so:
os.system("snscrape --max-results 100 twitter-search '#Tesla #Elonmusk' > scrapedtweets.txt")
I get an error code '2', and scrapedtweets.txt remains blank.
I should mention that the command works perfectly in os.system() when there are no # symbols involved in the command, which leads me to think that it is being parsed funny by the function, but I have no idea what the exact problem is or how to solve it.
Or alternatively is there something wrong with how I am using os.system()?
Any and all help is appreciated.

how to use the output of a curl command like a dictionary in python?

when I execute the command:
curl http://cs-service:5000/swdpconfig/swdp_templateConfigData/robot_framework
the output is:
{
"adm_ts_path": "/data/ngxp_test_automation/bin/admin",
"be_ts_path": "/data/ngxp_test_automation/bin/backend",
"fe_ts_path": "/data/ngxp_test_automation/bin/frontend",
"ip": "40.124.25.232",
"password": "Er1c550n#123",
"port": "22",
"user": "ngxpcdd"
}
Now I want to use the key-value pairs as parameters in my python script. How can I do that
You could pipe it directly to python if you want it executed in cli, and if not as someone mentioned you can save it as a file and load it in python with json.loads
Here is already answered stackoverflow question that might peak your interest
Pipe output from shell command to a python script
why would you mix python and curl ... that basically doesnt make much sense ... but you easily can
import subprocess,json
data = json.loads(subprocess.check_output('curl ...'))
it would be a much better idea to just use python (either requests or urllib)
import requests
url = "http://cs-service:5000/swdpconfig/swdp_templateConfigData/robot_framework"
data = requests.get(url).json()
if you wanted to use urllib instead i think its just
import urllib.request, json
data = json.loads(urllib.request.urlopen(urlData).read())

How to import and run a python file in an rmarkdown chunk?

I have an r markdown document and a python script named sim1.py. I would like to import the python code into a chunk. In some cases I want it to run, and in other cases I just want the code pasted.
So far I've tried using the child chunk option in the following way:
```{python, child=here::here("simulations", "sim1.py"), eval=FALSE}
```
I receive no errors when I run the above code, but no output is produced.
When I run the following code
```{python, child=here::here("simulations", "sim1.py"), eval=TRUE}
```
I get an error:
You can't use `macro parameter character #' in horizontal mode.
l.1938 #
plt.savefig("test.pdf")
where the error is referring to a comment line.
Any ideas on how I can do both:
Showing just the code
Running the code and displaying both the code and the output
EDIT: Another option I'm trying is the following:
```{python sim-1, code=cat(readLines(sim_folder), sep = '\n'), eval=FALSE}
```
with the intention that this reads the contents of the file, pastes that content into the body of the chunk, and evaluates it using the python engine. Though in this case with eval=FALSE it would just display the file contents as valid python code. This doesn't generate an error, but it doesn't display anything either.
I almost had it! The correct solution is in the documentation of all places: https://yihui.org/knitr/options/#code-chunk
```{python sim-1, code=readLines(sim_folder), eval=FALSE}
```

Converting cURL in python request

I would like to know how to convert a cURL command into a python request.
Indeed, I am using this cURL command :
curl -i -XPOST 'http://localhost:8086/write?db=mydb' --data-binary
'air_quality,host=raspberrypi value=200'
So, it allows to write the value 200 in the database mydb. But I would like to put this command in a python script. Then, it's not possible to do it, I got a format error.
I think it is possible to do it with python but I don't know how exactly. First, I have to import that :
import requests
Then the command should be like that :
requests.post("htp://localhost:8086/write?db=mydb
air_quality,host=raspberrypi value="+str(sensor_value))
My question is : how to write correctly the previous line for the python request ?
This a screenshot of my error :
Troubleshooting
#Jack I found the answer, the right command is :
payload='air_quality,host=raspberrypi value=100'
requests.post(url="http://localhost:8086/write?db=mydb", data=payload)
I checked in the influxdb database mydb and this is working, meanwhile I would like to get back the values from a sensor, the value is written in the variable sensor_value. How to get it ? I tried this :
payload='air_quality,host=raspberrypi value=sensor_value'
And I got this error : {"error":"unable to parse 'air_quality,host=raspberrypi value=sensor_value': invalid boolean"}

Trying to convert php to python and getting a syntax error

I'm trying to convert some php code into python and am using curl. I've gotten most of it to be accepted, but when it gets to the result = pycurl.exec(Moe) it keeps throwing a syntax error. I guess that I'm not filling out the exec field correctly, but I can't seem to figure out where it is going wrong.
from urllib2 import urlopen
from ClientForm import ParseResponse
import cgi, cgitb
import webbrowser
import curl, pycurl
Moe = pycurl.Curl(wsdl)
Moe.setopt(pycurl.POST, 1)
Moe.setopt(pycurl.HTTPHEADER, ["Content-Type: text/xml"])
Moe.setopt(pycurl.HTTPAUTH, pycurl.BASIC)
Moe.setopt(pycurl.USERPWD, "userid:password")
Moe.setopt(pycurl.POSTFIELDS, Larry)
Moe.setopt(pycurl.SSL_VERIFYPEER, 0)
Moe.setopt(pycurl.SSLCERT, pemlocation)
Moe.setopt(pycurl.SSLKEY, keylocation)
Moe.setopt(pycurl.SSLKEYPASSWD, keypassword)
Moe.setopt(pycurl.RETURNTRANSFER, 1)
result = pycurl.exec(Moe)
pycurl.close(Moe)
Use result = Moe.perform() to execute your request.
PS:
Moe.setopt(pycurl.POSTFIELDS, Larry)
Is Larry actually a variable? If it's a string, quote it.
exec is a reserved word in Python, you cannot have a function of that name. Try reading the pycurl documentation to see what function you should be calling.
I haven't used pycurl myself, but maybe you want to call Moe.perform()?

Categories

Resources