前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >python怎样执行curl命令_如何使用python执行curl命令

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

作者头像
用户7886150
修改2023-09-24 16:42:07
5.9K0
修改2023-09-24 16:42:07
举报
文章被收录于专栏:bit哲学院bit哲学院

参考链接: 在PycURL和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

 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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档