5 回复
caden medium avatar
#1 - 一年前
caden
import requests

def get_token(ip, username, password):
url2 = 'http://' + ip + ':3000/login'
r = requests.request('POST', url2, data={'username': username,
'password': password}).json()
print(r)
return r['token']

def get_task(ip):
token = get_token(ip, 'ofagaq', 'ofagaq')
url = 'http://' + ip + ':3000/api/tasks'
headers = {
'Authorization': 'Bearer ' + token,
}
res = requests.post(url=url, headers=headers).json()
print(res)

if __name__ == '__main__':
ip = input("请输入测试环境IP:\n")
get_task(ip)

willcheng medium avatar
#2 - 一年前
willcheng
import requests
from sys import argv
def login(username,password):

url = "http://"+ case_id +"/login"

global payload
payload = {'username':username,'password':password}

res = requests.request("post",url,data=payload).json()
print(res)
return res['token']

def get_tasks(token):
url = "http://"+ case_id +"/api/tasks"
headers = {
'Authorization': 'Bearer ' + token
}
data = requests.request("get",url,data=payload,headers=headers).json()
print(data)

script, case_id = argv
get_tasks(login('admin','admin'))

运行结果:

E:\>python practice.py 139.199.192.100:3000
{'id': 1, 'username': 'admin', 'token': 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWQiOjEsImlhdCI6MTUxNTc1MDY2OCwiZXhwIjoxNjAyMTUwNjY4fQ.tY_bi36iubygpW7lVT7VlL8cz-kLvMQ6XBbIKSyV8qw'}
[{'id': 3873, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-11T02:13:29.000Z', 'updatedAt': '2018-01-11T02:13:29.000Z'}, {'id': 3872, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-11T01:53:11.000Z', 'updatedAt': '2018-01-11T01:53:11.000Z'}, {'id': 3871, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T23:03:13.000Z', 'updatedAt': '2018-01-10T23:03:13.000Z'}, {'id': 3870, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T22:47:02.000Z', 'updatedAt': '2018-01-10T22:47:02.000Z'}, {'id': 3869, 'title': 'huluwa_tcnmfylxjd', 'desc': 'qpmcmcqziouhxlmbdpjq', 'done': False, 'createdAt': '2018-01-10T06:29:57.000Z', 'updatedAt': '2018-01-10T06:29:57.000Z'}, {'id': 3868, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:19:03.000Z', 'updatedAt': '2018-01-10T02:19:03.000Z'}, {'id': 3867, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:18:18.000Z', 'updatedAt': '2018-01-10T02:18:18.000Z'}, {'id': 3866, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:17:51.000Z', 'updatedAt': '2018-01-10T02:17:51.000Z'}, {'id': 3865, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:14:43.000Z', 'updatedAt': '2018-01-10T02:14:43.000Z'}, {'id': 3864, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:11:04.000Z', 'updatedAt': '2018-01-10T02:11:04.000Z'}, {'id': 3863, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:06:56.000Z', 'updatedAt': '2018-01-10T02:06:56.000Z'}, {'id': 3862, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:05:01.000Z', 'updatedAt': '2018-01-10T02:05:01.000Z'}, {'id': 3861, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:04:48.000Z', 'updatedAt': '2018-01-10T02:04:48.000Z'}, {'id': 3860, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:01:49.000Z', 'updatedAt': '2018-01-10T02:01:49.000Z'}, {'id': 3859, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:00:47.000Z', 'updatedAt': '2018-01-10T02:00:47.000Z'}, {'id': 3858, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T02:00:04.000Z', 'updatedAt': '2018-01-10T02:00:04.000Z'}, {'id': 3857, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T01:59:22.000Z', 'updatedAt': '2018-01-10T01:59:22.000Z'}, {'id': 3856, 'title': None, 'desc': None, 'done': False, 'createdAt': '2018-01-10T01:49:33.000Z', 'updatedAt': '2018-01-10T01:49:33.000Z'}, {'id': 3855, 'title': 'huluwa_qemfvyglwi', 'desc': 'vvwzybuaiixedmrujoax', 'done': False, 'createdAt': '2018-01-09T02:16:03.000Z', 'updatedAt': '2018-01-09T02:16:03.000Z'}, {'id': 3854, 'title': 'huluwa_jfpcndveey', 'desc': 'aurqzxgjzrbfzonsgotg', 'done': False, 'createdAt': '2018-01-09T02:15:48.000Z', 'updatedAt': '2018-01-09T02:15:48.000Z'}]

willcheng medium avatar
#3 - 一年前
willcheng

尴尬了,留言有点乱

greatbu medium avatar
#4 - 一年前
greatbu
import requests
import unittest

class TestHomework(unittest.TestCase):
def test01(self):
ip = input(" 请输入url:")
username = input("用户名:")
pwd = input("密码:")
url = self.ip + "/login"
res = requests.request("POST", url, data={'username': username, 'password': pwd}).json()
return res['token']

if __name__ == "__main__":
unittest.main()


测试学习小组 — 软件测试学习讨论区
现在注册
已注册用户请 登入
内容分享

欢迎关注我们的公众号: 测试圈TC


小贴士

你的问题在测试教程网中能找到答案