python执行curl_使用 Python 执行 Curl 命令(demo)

本文提供了两个示例,分别展示了如何使用Curl命令及对应的Python代码发送POST请求。示例1介绍如何获取访问令牌,示例2演示了如何调用特定的RPC方法。

示例 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 = '{\t"appId": appId,\t"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',

}

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()

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值