如何在python中使用curl-XGET

2024-04-19 05:31:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个curl命令,我想在python2.7版本中使用。 卷曲-k-XGET''-d'' 但我在Request库或{a2}库中找不到任何合适的函数。我只能在其中找到简单的curl get命令替代。但是我需要将一个JSON对象发布到elasticsearch。有什么帮助吗?谢谢!在


Tags: 对象函数命令版本jsona2getrequest
2条回答

您可以使用urllib2发布json并返回响应:

import json
import urllib2
data = json.dumps(['a', 'b', 'c'])
url = <define the url to be called>
req = urllib2.Request(url, data, {'Content-Type': 'application/json'})
f = urllib2.urlopen(req)
resp = f.read()
f.close()

对于get请求,可以使用库请求http://docs.python-requests.org/en/master/

import requests

response  = requests.get(url, data=your_data)

你的数据是json格式的dict。在

相关问题 更多 >