使用python写接口自动化
import requests
import json
import random
import pymysql
class Call():
def reg_stu(self):
header = {
"Content-Type": "application/json"
}
reg_url = "http://open-test.zmlearn.com/api/list/users/register-appointment"
data = '''{
"bu": 5,
"mobile": %s,
"name": "名单测试",
"stuGrade": "小五"
}'''
new_data = json.loads(data % (self.call()))
body = json.dumps(new_data, ensure_ascii=False).encode('utf-8')
res = requests.request('post', reg_url, data=body, headers=header).json()
return res['data']['id']
def call(self):
prelist = ["130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "147", "150", "151", "152",
"153", "155", "156", "157", "158", "159", "186", "187", "188"]
return random.choice(prelist) + "".join(random.choice("0123456789") for i in range(8)) #随机生成手机号
header:
User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:10.0.2) Gecko/20100101 Firefox/10.0.2 请求头
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
Accept-Language: zh-cn,zh;q=0.5
Accept-Encoding: gzip, deflate
Connection: keep-alive
Referer: http://localhost/
Content-Length:25
Content-Type:application/x-www-form-urlencoded
body:
一般存放请求参数,键值对形式{key:value}