python怎样执行curl命令_如何使用python执行curl命令

在Python中执行curl命令,可以使用requests库。例如,要执行curl -d @request.json --header "Content-Type: application/json" https://www.googleapis.com/qpxExpress/v1/trips/search?key=mykeyhere,可以将代码替换为:import requests; payload = open('request.json'); headers = {'content-type': 'application/json', 'Accept-Charset': 'UTF-8'}; r = requests.post(url, data=payload, headers=headers); r.json()。其他答案提供了更多转换curl到Python的方法。" 104153985,7460703,PAT甲级真题1018:公共自行车管理系统C++实现(Dijkstra算法),"['算法', '数据结构', 'dijkstra']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

如何使用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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值