如何在python中执行curl命令?

2024-05-15 00:53:01 发布

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

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

curl -H 'Content-Type: application/x-ndjson' -XPOST '192.168.1.149:9200/shakespeare/doc/_bulk?pretty' --data-binary @file.json

Tags: 命令程序datadocapplicationtypeprettybulk
2条回答

使用requests - HTTP for humans,它允许您发送任何您喜欢的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命令,这只是一个示例。你知道吗

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

相关问题 更多 >

    热门问题