如何在python中执行curl命令?

如何在python中执行curl命令?,python,python-3.x,python-2.7,typescript,Python,Python 3.x,Python 2.7,Typescript,我有这个命令,需要用python执行我的程序 curl -H 'Content-Type: application/x-ndjson' -XPOST '192.168.1.149:9200/shakespeare/doc/_bulk?pretty' --data-binary @file.json 使用,它允许您发送任何您喜欢的HTTP查询 import requests r = requests.post('192.168.1.149:9200/shakespeare/doc/_bulk?p

我有这个命令,需要用python执行我的程序

curl -H 'Content-Type: application/x-ndjson' -XPOST '192.168.1.149:9200/shakespeare/doc/_bulk?pretty' --data-binary @file.json
使用,它允许您发送任何您喜欢的HTTP查询

import requests
r = requests.post('192.168.1.149:9200/shakespeare/doc/_bulk?pretty', json=datadict)
print(r.text)

试试这样的方法:

import requests
import json

headers = {
    'Content-Type': 'application/x-ndjson',
}

params = (('pretty', ''),)

data = open('file.json', 'rb').read()
response = requests.post('http://192.168.1.149:9200/shakespeare/doc/_bulk', headers=headers, params=params, data=data)
print(response.text)
注意:根据curl命令,这只是一个示例


谢谢!希望对你有帮助!:)

这与Python2.7和Python3.x有何关系?请参阅此elasticsearch?可能与