python通过多线程去调用接口注册新用户【杭州多测师】【杭州多测师_王sir】

 

#!/usr/bin/python3
import threading
import time
import json
import requests
class myThread (threading.Thread):
    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.delay = delay

    def register(self,count):
        """
        '''定义一个注册接口、循环进行注册'''
        :param count: 这个控制需要注册多少次
        :return:
        """
        mobile = 15311112222
        for i in range(count):
            url = 'https://m.snt-art.com/api/login'
            data ={"code": "123456",
                   "loginType": 1,
                   "mobile": str(mobile+i),
                   "password": "abc123456",
                   "invitationCode": ""}
            headers = {"Content-Type":"application/json"}
            response = requests.post(url,data=json.dumps(data),headers=headers)
            print(response.text)

    def run(self):
        print ("开启线程: " + self.name)
        # 获取锁,用于线程同步
        threadLock.acquire()
        self.register(10)
        # print_time(self.name, self.delay, 3)
        # 释放锁,开启下一个线程
        threadLock.release()

def print_time(threadName, delay, counter):
    while counter:
        time.sleep(delay)
        print ("%s: %s" % (threadName, time.ctime(time.time())))
        counter -= 1

threadLock = threading.Lock()
threads = []

# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)

# 开启新线程
thread1.start()
thread2.start()

# 添加线程到线程列表
threads.append(thread1)
threads.append(thread2)

# 等待所有线程完成
for t in threads:
    t.join()
print ("退出主线程")

 

posted @ 2022-09-02 16:10  多测师_王sir  阅读(19)  评论(0编辑  收藏  举报