Using Python to execute the Curl command (demo)

Reference link: https://blog.csdn.net/weixin_43420032/article/details/84646041

Example 1

Curl command:

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 implements the above command:

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)



Example 2

Curl command:

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 implements the above command:

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"]))) # Retrieve the value of the "result" field in the response content 
print (int (json.loads (response.content) ["result"], 16)) # 16 hexadecimal string converted to integer int 
print (type (int (json.loads (response.content) ["result"], 16))) 

### Note: 
### Response is required under Linux. Decode content and convert it from bytes type to str type 
### Specific operation: 
### bytes.decode (response.content) or response.content.decode ()

  

  

  

 

Guess you like

Origin www.cnblogs.com/skzxc/p/12688423.html