How can I use python to execute a curl command

+1 vote

I want to execute a curl command in python.

Usually, I just need to enter the command in terminal and press return key. However, I don't know how it works in python.

The command shows below:

curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere

There is a request.json file to be sent to get response.

I searched a lot and got confused. I tried to write a piece of code, although I could not fully understand. It didn't work.

import pycurl
import StringIO

response = StringIO.StringIO()
c = pycurl.Curl()
c.setopt(c.URL, 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere')
c.setopt(c.WRITEFUNCTION, response.write)
c.setopt(c.HTTPHEADER, ['Content-Type: application/json','Accept-Charset: UTF-8'])
c.setopt(c.POSTFIELDS, '@request.json')
c.perform()
c.close()
print response.getvalue()
response.close()

The error message is 'Parse Error'.Can anyone tell me how to fix it? or how to get response from the sever correctly?

Oct 11, 2018 in Python by aryya
• 7,460 points
94,462 views

3 answers to this question.

+1 vote

For sake of simplicity, maybe you should consider using the standard library Requests.

An example with json response content would be something like:

import requests
r = requests.get('https://github.com/timeline.json')
r.json()

If you look for further information, in the Quickstart section, they have lots of working examples.

EDIT:

For your specific curl translation:

import requests
url = 'https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere'
payload = open("request.json")
headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}
r = requests.post(url, data=payload, headers=headers)


Hope it works!!

If you are a beginner and need to know more about Python, It's recommended to go for Python Certification course today.

Thanks!

answered Oct 11, 2018 by charlie_brown
• 7,720 points
https://onlinedevtools.in/curl

Its convert your curl command to python request
Thanks, @Stephen for your contribution.

Please register at Edureka Community and earn credits for every contribution. A contribution could be asking a question, answering, commenting or even upvoting/downvoting an answer or question.

These credits can be used to get a discount on the course. Also, you could become the admin at Edureka Community with certain points.

Thanks!
0 votes

Curl is a linux command that allows a user to make requests to or from a network server. It is especially useful for making automated shell scripts for downloading files or posting data.

USE requests.get() OR requests.post() TO MAKE A CURL REQUEST

Call requests.get(url) and requests.post(url, data, headers) to make a curl request, or any web request. The url is the url of the specified endpoint, data is the payload to send, and headers should contain any relevant headers for the request.

url = "https://postman-echo.com/post"
headers = {"content-type": "application/json", "Accept-Charset": "UTF-8"}


r = requests.post(url, data={"sample":"data"}, headers=headers)

post data to url

data = r.json()

store response



print(data)

OUTPUT

{'args': {}, 'data': 'sample=data', 'files': {}, 'form': {}, 'headers': {'x-forwarded-proto': 'https', 'host': 'postman-echo.com', 'content-length': '11', 'accept': '*/*', 'accept-charset': 'UTF-8', 'accept-encoding': 'gzip, deflate', 'content-type': 'application/json', 'user-agent': 'python-requests/2.22.0', 'x-forwarded-port': '443'}, 'json': None, 'url': 'https://postman-echo.com/post'}
answered Dec 14, 2020 by Gitika
• 65,770 points
0 votes

You could use urllib as @roippi said:

import urllib2
data = '{"nw_src": "10.0.0.1/32", "nw_dst": "10.0.0.2/32", "nw_proto": "ICMP", "actions": "ALLOW", "priority": "10"}'
url = 'http://localhost:8080/firewall/rules/0000000000000001'
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
for x in f:
    print(x)
f.close()
answered Dec 14, 2020 by Roshni
• 10,480 points

Related Questions In Python

0 votes
1 answer

How do I use urllib to see if a website is 404 or 200 in Python?

For Python 3, try doing this: import urllib.request, ...READ MORE

answered Nov 29, 2018 in Python by Nymeria
• 3,560 points

edited Dec 11, 2018 by Nymeria 14,045 views
0 votes
2 answers

How can I write a program to add two numbers using functions in python?

there is sum() function as a built ...READ MORE

answered Oct 25, 2020 in Python by anonymous
23,868 views
0 votes
1 answer
0 votes
1 answer

How can I use the sleep function in a python program?

You can use sleep as below. import time print(" ...READ MORE

answered Jun 10, 2020 in Python by anonymous
1,009 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,454 views
0 votes
1 answer
+2 votes
2 answers

How can I plot a k-dsitance graph using python?

Hi there, instead of sklearn you could ...READ MORE

answered Apr 10, 2018 in Python by charlie_brown
• 7,720 points
5,060 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP