python 并发请求,转发
http://t.csdnimg.cn/snSm5
另外:如果想要随机入参,可以参考加入随机数
#coding=utf-8
import requests
import json
import threading
import time
import uuid
import random
class postrequests():
def init(self):
self.url = 'https://***.*****.com/user/user/login' #请求链接
self.data = {'phone': '176*****286','code':'12***6',paras1+str(random.randrange(12,200,1))} #传递参数
self.headers = {'content-type': 'application/json'} #请求头
self.data = json.dumps(self.data)
def post(self):
try:
r = requests.post(self.url, self.data, headers=self.headers)
print(r.text)
except Exception as e:
print(e)
def kquan_bf():
login = postrequests()
return login.post()
try:
i = 0
# 开启线程数目
tasks_number = 1
print(‘测试启动’)
time1 = time.perf_counter()
while i < tasks_number:
t = threading.Thread(target=kquan_bf)
t.start()
i +=1
time2 = time.perf_counter()
times = time2 - time1
print(times/tasks_number) #每次请求用时
except Exception as e:
print(e) #请求结果