python怎样执行curl命令_如何使用python执行curl命令
如何使用python執行curl命令
我想在python中執行curl命令。
通常,我只需要在終端輸入命令并按回車鍵。 但是,我不知道它在python中是如何工作的。
該命令如下所示:
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
有一個request.json文件要發送以獲得響應。
我經常搜索并感到困惑。 我試著寫一段代碼,雖然我無法完全理解。 它沒用。
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()
錯誤信息是'Parse Error'。任何人都可以告訴我如何修復它? 或者如何正確地從服務器獲得響應?
Qiang Fu asked 2019-04-29T07:44:26Z
7個解決方案
106 votes
為簡單起見,您可以考慮使用標準庫要求。
json響應內容的示例如下:
import requests
r = requests.get('https://github.com/timeline.json')
r.json()
如果您要查找更多信息,請在“快速入門”部分中找到許多有用的示例。
編輯:
對于您的特定卷曲翻譯:
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)
otorrillas answered 2019-04-29T07:44:58Z
17 votes
只需使用這個網站。 它會將任何curl命令轉換為Python,Node.js,PHP,R或Go。
例:
curl -X POST -H 'Content-type: application/json' --data '{"text":"Hello, World!"}' https://hooks.slack.com/services/asdfasdfasdf
在Python中成為現實,
import requests
headers = {
'Content-type': 'application/json',
}
data = '{"text":"Hello, World!"}'
response = requests.post('https://hooks.slack.com/services/asdfasdfasdf', headers=headers, data=data)
Kyle Bridenstine answered 2019-04-29T07:45:31Z
12 votes
import requests
url = "https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere"
data = requests.get(url).json
也許?
如果您要發送文件
files = {'request_file': open('request.json', 'rb')}
r = requests.post(url, files=files)
print r.text, print r.json
啊,謝謝@LukasGraf現在我更好地理解他原來的代碼在做什么
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 req.json # maybe?
Joran Beasley answered 2019-04-29T07:46:04Z
11 votes
curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere
它的python實現就像
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)
檢查此鏈接,它將幫助將cURl命令轉換為python,php和nodejs
cryptoKTM answered 2019-04-29T07:46:35Z
3 votes
我的回答是WRT python 2.6.2。
import commands
status, output = commands.getstatusoutput("curl -H \"Content-Type:application/json\" -k -u (few other parameters required) -X GET https://example.org -s")
print output
我為沒有提供必要的參數而道歉,因為它是保密的。
apoorva_bhat answered 2019-04-29T07:47:06Z
1 votes
有一個很好的網站[https://curl.trillworks.com/]為您進行轉換。 它確實從cURL轉換為Python,Node.js,R,PHP,Go。
Edgar Manukyan answered 2019-04-29T07:47:30Z
-2 votes
這可以通過下面提到的偽代碼方法來實現
導入os導入請求Data = os.execute(curl URL)R = Data.json()
user9761315 answered 2019-04-29T07:48:01Z
總結
以上是生活随笔為你收集整理的python怎样执行curl命令_如何使用python执行curl命令的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Windows 11 on ARM In
- 下一篇: 领军全国嵌入式,ARM11重装上阵