【问题标题】:Convert curl command into a working script将 curl 命令转换为工作脚本
【发布时间】:2019-03-07 18:50:31
【问题描述】:

我正在使用来自 check-host.net 的 API 来 ping 一个网站。 我现在的问题是我不知道如何将 curl 命令 api 转换为工作 python 脚本。我尝试了在这里找到的不同方法,但遗憾的是没有一种方法能提供我正在寻找的输出。

工作 curl 命令:

curl -H "Accept: application/json" \   https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1

响应看起来像这样:

{ “ok”:1,“request_id”:“29”,“permanent_link”: “https://check-host.net/check-report/29”、“节点”:{ "7f000001": ["it","Italy", "Marco"] } }

【问题讨论】:

  • 请求库你试过了吗
  • @JibinMathews ,我做到了,但遗憾的是我没有得到正确的回应。也许我没有正确使用它。

标签: python-3.x curl python-requests


【解决方案1】:

您必须在请求中发送 Accept: application/json 标头。您也可以在请求中使用builtin json decoder

import requests
headers={
'Accept': 'application/json'
}
r=requests.get('https://check-host.net/check-tcp?host=smtp://gmail.com&max_nodes=1',headers=headers)
print(r.json())

输出

{'nodes': {'us2.node.check-host.net': ['us', 'USA', 'New Jersey', '199.38.245.218', 'AS394625', 'OK']}, 'ok': 1, 'permanent_link': 'https://check-host.net/check-report/a462c3ck399', 'request_id': 'a462c3ck399'}

【讨论】:

    猜你喜欢
    • 2011-05-01
    • 2016-03-20
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    相关资源
    最近更新 更多