系列Python开发
前言
一、python实现http服务调用
服务端开发后通常需要做功能测试、性能测试,通常postman、curl等作功能测试使用,长跑服务性能postman并不太适合,如用c++实现http client,开发效率太低效,本文介绍一种用python来实现http server的测试。
二、代码实现
1. http 申请token代码
import json
import requests
import time
import threading
import time
from threading import Thread
import sys
import signal
#-*- coding: UTF-8 -*-
requests.packages.urllib3.disable_warnings()
username="admin"
password="admin123"
sever_ip="10.20.40.80"
my_token=""
def allocToken(platform_ip):
url='http://%s/user/allocToken' %(platform_ip)
headers = {
"Content-Type": "application/json",
}
data={
"validateMethod":"name+password",
"username":str(username),
"userpassword":str(password),
"refreshInterval":3600,
"requestid":"1"
}
response = requests.post(url, headers=headers, verify=False, data=json.dumps(data))
resp = json.loads(response.text)
print("token: "+ str(resp["token"]))
return resp["token"]
def ConcurrencyAllocToken(sequence,platform_ip):
while True:
try:
start = time.time()
my_token=allocToken(platform_ip)
t_exec = time.time() - start
print(str(sequence) + " call alloc token used: " + str(t_exec))
break
except:
break
if __name__=="__main__":
cpucores = 1
for i in range(cpucores):
thread = Thread(target=ConcurrencyAllocToken, args=(i,sever_ip,))
thread.start()
time.sleep(1)
2. 结果如下:
{
"requestid": "1",
"resultDesc": "成功",
"resultcode": 0,
"token": "BA680401-92D5-535A-38D4-633C8456ED9E",
"userInfo": {
"userid": "5aa2d40b-87f6-4a82-abf3-212df0677309",
"loginName": "admin",
"userName": "admin123"
}
}
总结
本文实现了最简单的http申请token的流程,可以做其他的扩展使用。文章来源:https://www.toymoban.com/news/detail-516055.html
授之以鱼不如授之以渔,如果喜欢,请点赞收藏。文章来源地址https://www.toymoban.com/news/detail-516055.html
到了这里,关于python实现http服务调用的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!