【Python】requests.post請求註冊實例

#encoding=utf-8

import requests import json import time import random import multiprocessing from multiprocessing import Process,Value,Lock,Manager import string def register(lock,success_count,failure_count): data_dict={'username': 'xxdddy', 'password': 'wcx123wacs', 'email': 'lsily@qq.com'} for i in range(2): data_dict["username"]="".join(random.sample(string.lowercase,10))+str(i) #建立不一樣username的用戶,是字典類型
        str1=json.dumps(data_dict) #將字典類型數據轉換成string類型並傳入post請求中
        request_result=requests.post("http://127.0.0.1:8080/register/",data=str1) #返回json類型數據
        print str(request_result.json()) #用json的規則,將服務器接口返回的數據轉換成字典類型
        if request_result.json()["code"]=="0" and isinstance(request_result.json()["userid"],int): lock.acquire() success_count.value+=1 lock.release() else: lock.acquire() failure_count.value+=1 lock.release() if __name__=="__main__": lock=Lock() manager=Manager() success_count=manager.Value("i",0) failure_count=manager.Value("i",0) process_list=[Process(target=register,args=(lock,success_count,failure_count)) for i in range(2)] for p in process_list: p.start() for p in process_list: p.join() print success_count.value print failure_count.value
相關文章
相關標籤/搜索