# python请求实例讲解
python请求实例
# 概览
本文主讲python请求实例,带POST,GET 请求,开发者可自行封装模块,工具类调用,参数有详细讲解
当前方法为GET请求,json请求格式,需要引入json,time,requests模块,请使用PIP下载
import json
import time
import requests
# MD5请使用下方的MD5加密模块实例
import md5
# 秘钥askKey
askKey = ""
# 加解密秘钥
encryKey = ""
# 验签字符串
sinStr = ""
# 请求入口
baseUrl='https://api.potatocloud.cn/api'
def get(url):
# 当前时间
now=int(time.time()*1000)
headers = {
"askKey": askKey,
"sign":md5.encrypt(sinStr+str(now)),
"time":str(now),
}
print(headers)
response=requests.get(url=(baseUrl+url),headers=headers)
return json.loads(response.text)
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
当前方法为POST请求,json请求格式
import json
import time
import requests
# MD5请使用下方的MD5加密模块实例
import md5
# 秘钥askKey
askKey = ""
# 加解密秘钥
encryKey = ""
# 验签字符串
sinStr = ""
# 请求入口
baseUrl='https://api.potatocloud.cn/api'
def post(url,param):
# 当前时间
now=int(time.time()*1000)
headers = {
"Content-Type":"application/json",
"askKey": askKey,
"sign":md5.encrypt(sinStr+str(now)),
"time":str(now),
}
print(headers)
response=requests.post(url=(baseUrl+url),json=param,headers=headers)
return json.loads(response.text)
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
测试方法
if __name__ == '__main__':
result=post("/login",{'userNumber':"1","passWord":1})
print(result['code'])
print(result['message'])
1
2
3
4
2
3
4
Md5 加密方法
import hashlib
# md5加密
def encrypt(value):
# 创建md5加密对象
md5 = hashlib.md5()
# 指定需要加密的字符串
md5.update(value.encode('utf-8'))
# 加密后的字符串
str_md5 = md5.hexdigest()
return str_md5
pass
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13