【问题标题】:Python : curl command option with `-d` to pythonPython:带有`-d`的curl命令选项到python
【发布时间】:2014-08-20 07:49:41
【问题描述】:

Python : curl 命令选项与-d 到 python

我有以下curl bash 脚本,它在终端中返回多行。

curl 'http://localhost/_search?pretty' -d '
{
  "query" :{ 
    "range" : {
      "created_at" : {
        "gte": "now-2h",
        "lte": "now"
      }
    }
  }
}'

我想在 python 中运行这个 bash 脚本或curl 命令并在 python 中接收输出。我应该怎么办? requests 似乎不支持curl 命令的选项。

【问题讨论】:

标签: python unix curl python-requests


【解决方案1】:

据我了解,您希望使用 Python 在后期数据中发送 JSON。在请求中,您可以这样做:

import requests, json

data =  {
   "query": { 
    "range" : {
      "created_at" : {
        "gte": "now-2h",
        "lte": "now"
      }
    }
  }
}

url = "http://localhost/_search?pretty"

response = requests.post(url, data = json.dumps(data))

讨论了在 python 中请求 ElasticSearch here

【讨论】:

    猜你喜欢
    • 2020-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-04
    • 1970-01-01
    • 2014-09-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多