Ik wil een curl commando uitvoeren in python.
Normaal moet ik gewoon het commando in terminal invoeren en op de return toets drukken. Ik weet echter niet hoe het in python werkt.
The command shows below:
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
Er is een request.json bestand dat verstuurd moet worden om respons te krijgen.
Ik heb veel gezocht en raakte in de war. Ik probeerde een stukje code te schrijven, hoewel ik het niet helemaal begreep. Het werkte niet.
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()
De foutmelding is 'Parse Error'.Kan iemand mij vertellen hoe ik dit kan oplossen? Of hoe ik correct antwoord krijg van de sever?
Misschien moet je omwille van de eenvoud overwegen de Requests bibliotheek te gebruiken.
Een voorbeeld met json response inhoud zou zoiets zijn als:
import requests
r = requests.get('https://github.com/timeline.json')
r.json()
Als je verder kijkt, in de Quickstart sectie, hebben ze veel werkende voorbeelden.
EDIT:
Voor uw specifieke curl vertaling:
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)
import requests
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere"
data = requests.get(url).json
misschien?
Als je probeert een bestand te versturen
files = {'request_file': open('request.json', 'rb')}
r = requests.post(url, files=files)
print r.text, print r.json
ahh bedankt @LukasGraf nu begrijp ik beter wat zijn originele code doet
import requests,json
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere"
my_json_data = json.load(open("request.json"))
req = requests.post(url,data=my_json_data)
print req.text
print
print req.json # maybe?
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
de python implementatie is als
import requests
headers = {
'Content-Type': 'application/json',
}
params = (
('key', 'mykeyhere'),
)
data = open('request.json')
response = requests.post('https://www.googleapis.com/qpxExpress/v1/trips/search', headers=headers, params=params, data=data)
#NB. Original query string below. It seems impossible to parse and
#reproduce query strings 100% accurately so the one below is given
#in case the reproduced version is not "correct".
# response = requests.post('https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere', headers=headers, data=data)
check deze link, het zal helpen bij het omzetten van cURl commando naar python, php en nodejs