使用 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 = '{\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',
}
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()

  

  

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM