How to execute curl command using python 3.6 windows - python

I have a curl command, Which works on my UNIX server.I want to execute it using python script.
curl comma:
curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name":"Suppression_Testing","timeRange": {"startTimeMillis": "2017-12-25T04:16:30+0000","endTimeMillis": "2017-12-26T06:16:30+0000"},"affects": {"type": "APP"}}' --user api#adgm-nonprod:ac#123
https://apcn.adm.com/controller/api/accounts/3/applications/61/actionsuppressions
I tried import os and executed curl butI am getting error as POST invalid syntax.
Also I tried using subprocess
subprocess.Popen(curl 0) but no luck. Can anyone help me?
Many Thanks,
Apurva Acharya

you can use python , subprocess module to do this
from subprocess import Popen
command='''curl -X POST -H "Content-type: application/vnd.appd.cntrl+json;v=1" -d '{"name":"Suppression_Testing","timeRange": {"startTimeMillis": "2017-12-25T04:16:30+0000","endTimeMillis": "2017-12-26T06:16:30+0000"},"affects": {"type": "APP"}}' --user api#adgm-nonprod:ac#123'''
#use shell=True , this will allow you to run the command in a single string on a shell like environment
proc=Popen(command,shell=True)

Related

Problem with sending JSON POSTs using paramiko

I'm trying to execute some curl POST commands to a SSH server using Paramiko.
The problem is that the curl command includes quotes and simple quotes and some extra characters are added when sending the command to the server.
What I try to send is the following:
ssh.exec_command("curl -X POST -H 'Content-Type: application/json' -d '{\"backlight\":true}' [127.0.0.1]")
Server answers with invalid JSON. I checked it and what reaches the server is:
curl -X POST -H \'Content-Type: application/json\' -d \'{"backlight":true}\' [127.0.0.1]
There are some backslashes added to my string, so that the curl command is invalid.
I tried a lot of things like escaping, replace(), change the command etc. nothing worked.
Do you have any tips or ideas how to solve this issue? It's really frustrating.
Server command should look like this:
curl -X POST -H 'Content-Type: application/json' -d '{"backlight":true}' [127.0.0.1]

How to send '-X POST' using 'requests' library in Python?

I am trying to replicate shell command:
curl -u [staff_email]:[api_key] -F "case[attachments][0]=#/path/to/file1.ext" -F "case[attachments][1]=#/path/to/file2.ext" -F "case[content]=I need help" -F "case[subject]=I need help" -F "case[user_email]=user#domain.ru" -F "case[user_full_name]=FullName" -F "case[language_id]=2" -F "case[custom_fields][cf_44]=3" -X POST https://[domain].omnidesk.ru/api/cases.json
into Python code using Requests library:
import requests
What is a proper syntax for this example (curl -X POST)?
You can use requests.post, as explained here
for the attachments part (your first -F), read here
for the auth part (-u): here

python - convert curl command

I have this curl command.
This works
curl -O -H "X-Api:3433432" -X GET "https://website.com/file.zip"
Trying to figure it out how to convert it to something python understands.
curl -O -H "X-Api:3433432" -X GET "https://website/file.zip"
Perhaps you're looking for the "Requests" module?
It allows you to create a GET request in a similar way you would use a curl command.
You can find some documentation at:
https://www.w3schools.com/PYTHON/ref_requests_get.asp
Edit: There's also https://curlconverter.com/ which should convert it automatically for you.
try using module requests:
import requests
headers = {
'X-Api': '3433432',
}
response = requests.get('https://website.com/file.zip', headers=headers)

Transformaing CURL post request to python request

I am running a CURL command and want to update it to python request command.
CURL Command:
curl -H "Content-Type: application/json" -X POST -k -s --date '{"command": "/semate:severitytoAll', "target": "/gen1/gateway[(#name=\"#gname\")]/directory/probe[(#name=\"#probn\")]/managedE[(#name=\"#me\")]/sam[#name=\"PRO")] [(#type=\"\')]/dav[(#name=\"PRO\")]/row/rows[(#name=\""PRO_D\')]", "args': {"1": "testing sn via Rest aPI", '2": 1, "4":3}}' -u #gatName
Can someone guide me with the equivalent python request commands please
Regards
Bicky
check https://www.postman.com/product/rest-client/
Write it in Postman, select code and select Python.

How to use curl command via python

I want to send a json file to keen.io in their documentation they use the following command
curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json
I am thinking how can I use this to work with python
you can use subprocess module to run the curl command using subprocess.call() method.
eg :
subprocess.call('curl "https://api.keen.io/3.0/projects/PROJECT_ID/events/EVENT_COLLECTION?api_key=WRITE_KEY" -H "Content-Type: application/json" -d #purchase1.json', shell=true)
You could use the requests library to achieve this.
import requests
uri = "https://api.keen.io/3.0/projects/{}/events/{}?api_key={}".format(PROJECT_ID, EVENT_COLLECTION, API_KEY)
json_payload = open('purchase1.json', 'rb').read()
requests.post(uri, json=json_payload)
You can read through their documentation for more information.
You can also use subprocess to execute the command and then catch the output https://docs.python.org/2/library/subprocess.html

Categories