zoukankan      html  css  js  c++  java
  • 使用 Python 执行 Curl 命令(demo)

    参考链接:https://blog.csdn.net/weixin_43420032/article/details/84646041

    示例 1

    Curl命令:

    curl --request POST --url https://open.workec.com/auth/accesstoken --header 'cache-control: no-cache' --header 'content-type: application/json' --data '{ "appId": appId, "appSecret": "appSecret"}'

    Python实现上述命令:

    import requests   # requests模块需要使用 pip 命令安装
    headers = { 'cache-control': 'no-cache', 'content-type': 'application/json', }
    data = '{ "appId": appId, "appSecret": "appSecret"}' response = requests.post('https://open.workec.com/auth/accesstoken', headers=headers, data=data)



    示例 2

    Curl命令:

    curl -X POST --data '{"jsonrpc":"2.0","method":"cfx_getNextNonce","params":["0x1c13f30fa2b59d76191325b4a80032558c1b3b73"],"id":1}' -H "Content-Type: application/json" http://39.107.127.68:12537
    

    Python实现上述命令:

    import requests
    import json
    
    headers = {
        'content-type': 'application/json',
    }
    url = 'http://39.107.127.68:12537'
    data = '{"jsonrpc":"2.0","method":"cfx_getNextNonce","params":["0x1c13f30fa2b59d76191325b4a80032558c1b3b73"],"id":1}'
    
    response = requests.post(url, headers=headers, data=data2)
    print(response)
    print(response.content)
    print(type(int(json.loads(response.content)["result"])))   # 取出响应内容中 "result" 字段的值
    print(int(json.loads(response.content)["result"], 16))   # 16 进制字符串转成整型 int
    print(type(int(json.loads(response.content)["result"], 16)))
    
    ### 注:
    ### 在 Linux 下需要对 response.content进行解码,将其从 bytes 类型转为 str 类型
    ### 具体操作:
    ### bytes.decode(response.content) 或者 response.content.decode()
    

      

      

      

  • 相关阅读:
    具体解释协方差与协方差矩阵
    百度地图SDK for Android v2.1.3全新发布
    奇妙的等式
    Canny边缘检测及C++实现
    移动火柴问题
    移动火柴问题
    奇妙的等式 && 精妙的证明(二)
    奇妙的等式 && 精妙的证明(二)
    拉马努金恒等式
    拉马努金恒等式
  • 原文地址:https://www.cnblogs.com/skzxc/p/12688423.html
Copyright © 2011-2022 走看看