【问题标题】:how execute curl command in python?如何在python中执行curl命令?
【发布时间】:2018-10-04 09:27:53
【问题描述】:

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

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

【问题讨论】:

标签: python python-3.x python-2.7 typescript


【解决方案1】:

使用requests - HTTP for humans,它允许你发送任何你喜欢的HTTP查询

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

【讨论】:

    【解决方案2】:

    试试这样的:

    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 命令的示例。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-10-18
      • 2014-11-17
      • 1970-01-01
      • 2016-08-30
      • 1970-01-01
      • 1970-01-01
      • 2015-07-31
      相关资源
      最近更新 更多