【问题标题】:Send curl from python从 python 发送 curl
【发布时间】:2014-02-02 06:02:41
【问题描述】:

我有以下 json 格式的配置文件:

{"end_point":"http://domain.com","auth_token":"e91a0ffe758c194f2326eb4daed0","widgets":{"host_info":"fd8a63a33d1d15521e3499289a312","memory_info":"0bc2c3345ba7b129f3328bc4c507ad","cpu_info":"5a2b41cd21d56773250dc41dbd73681"}}

我正在尝试使用子进程发送数据。问题是如何从 json 文件中获取数据:

#Send using subprocess
import subprocess
subprocess.call([
'curl',
'-X',
'POST',
'-H',
'Content-Type: application/json',
'-d',
'{ "auth_token": "GETFROMCONFIGFILE(authtoken)", "widget": "GETFROMCONFIGFILE(widgets)", "title": "Something", "items":[{"label": "TEST", "value": "11"}, {"label": "1", "value": "11"}] }',
'GETFROMFILE(endpoint)'

提前谢谢你。

【问题讨论】:

    标签: python curl subprocess


    【解决方案1】:

    解码对象,如果f是已经打开读取的文件对象:

    x = json.load(f)
    

    你像往常一样使用 dict

    x["authtoken"]
    

    【讨论】:

    • 这不是答案。我要求使用子进程和 python 发送数据。
    • 您正在使用子进程,因此您可以使用命令行选项。也许您要的是 pycurl,但您的问题中没有 pycurl。
    • 好吧,我改了答案,打开文件,json.load,然后你用x["authtoken"],等等。我第一次读到您想将整个配置文件放入 curl 请求中。
    猜你喜欢
    • 2023-02-06
    • 1970-01-01
    • 1970-01-01
    • 2019-08-21
    • 2013-12-11
    • 2022-01-06
    • 1970-01-01
    • 2018-08-13
    • 1970-01-01
    相关资源
    最近更新 更多