python多線程請(qǐng)求帶參數(shù)的多個(gè)接口問題
多線程請(qǐng)求帶參數(shù)的多個(gè)接口
對(duì)于進(jìn)程/線程/攜程/異步的內(nèi)容 有時(shí)間準(zhǔn)備寫寫了 真的一直用for去循環(huán)慢到懷疑人生 需要運(yùn)用的場(chǎng)景也會(huì)很多 所以分開一點(diǎn)點(diǎn)總結(jié)一下
先上代碼看一下內(nèi)容,多線程請(qǐng)求接口
imoprt threading # 首先運(yùn)用到threading模塊 class BrushGifts: ?? ?# 以下是兩個(gè)相同的接口 send_gift_room_one()是送禮的接口 ?? ?# 也就是說我想完成的狀態(tài)是A送B B送A 兩個(gè)用戶同時(shí)想對(duì)方贈(zèng)送禮物 ? ? def giftt(self, uid, recvUid, giftId): ? ? ?? ?""" ? ? ? ? 送禮接口 ? ? ? ? :param uid: 送禮用戶 ? ? ? ? :param recvUid: 收禮用戶 ? ? ? ? :param giftId: 禮物道具id ? ? ? ? :return:? ? ? ? ? """ ? ? ? ? VoiceRoom().send_gift_room_one(uid, recvUid, giftId) ? ? def giftt_a(self, uid, recvUid, giftId): ? ? ?? ?# 與上面的接口相同 不多做敘述 ? ? ?? ?# 沒什么區(qū)別 就是寫著好理解倆接口的概念 ? ? ? ? VoiceRoom().send_gift_room_one(recvUid, uid, giftId) if __name__ == '__main__': ?? ?# 因?yàn)槲业馁~號(hào)是儲(chǔ)存在yaml文件內(nèi) 所以需要先倒出需要用戶的uid列表 ?? ?# 送禮人與收禮人分別倒出 ? ? uid_list = YamlHandler(YamlThePath().voice_room_cpNew).get_uid_list(10) ? ? rUid_list = YamlHandler(YamlThePath().number_old).get_scope_uid(1500, 10) ? ? # 這里暫時(shí)先展示最簡(jiǎn)單的AB同時(shí)互送~ ? ? for (i, j) in zip(uid_list, rUid_list): ? ? ?? ?# target內(nèi)需要傳入方法名 不要帶()因?yàn)閹Юㄌ?hào)就等于去調(diào)用了該方法 會(huì)直接開始執(zhí)行 ? ? ?? ?# args內(nèi)傳入輸入帶入的參數(shù) 可以按順序來 也可以整理成元組或字典接收 ? ? ? ? t1 = threading.Thread(target=BrushGifts().giftt, args=(i, j, 51620)) ? ? ? ? # 兩個(gè)threading.Thread就是我請(qǐng)求并發(fā)兩個(gè)接口 ? ? ? ? t2 = threading.Thread(target=BrushGifts().giftt, args=(j, i, 51620)) ? ? ? ? # t1.start()為執(zhí)行 ? ? ? ? t1.start() ? ? ? ? t2.start()
主線程:
主線程的概念就是這個(gè)py文件內(nèi)從頭到位的執(zhí)行 當(dāng)我執(zhí)行完最后一行代碼t2.start()后 主線程會(huì)進(jìn)入等待狀態(tài),等待子線程結(jié)束后主線程才會(huì)結(jié)束
子線程:
子線程的概念就是在主線程執(zhí)行的過程中我執(zhí)行到了這里 當(dāng)然這里是通過t1.start()方法來進(jìn)行調(diào)用的
t1 = threading.Thread(target=BrushGifts().giftt, args=(i, j, 51620))
這時(shí)候系統(tǒng)會(huì)再開辟出一條子線程進(jìn)行BrushGifts().giftt方法的執(zhí)行與調(diào)用
主線程在進(jìn)行的時(shí)候會(huì)執(zhí)行t1.start()與t2.start()但是他不會(huì)等待他們執(zhí)行結(jié)束,主線程會(huì)繼續(xù)往下,所以t1.start()與t2.start()是同時(shí)請(qǐng)求,當(dāng)t1.start()與t2.start()兩個(gè)子進(jìn)程都結(jié)束時(shí),主線程才會(huì)結(jié)束~
因?yàn)橹骶€程執(zhí)行t1.start()與t2.start()是一行一行的執(zhí)行 而且python無法做到準(zhǔn)確意義上的并發(fā) 所以如果對(duì)數(shù)據(jù)速度要求很高的時(shí)候 此方法不適用
如何用python請(qǐng)求接口
作用:用來重復(fù)插入數(shù)據(jù)
1、首先下載好python 然后 cmd 輸入 python 判斷是否全局安裝成功
2、下載好PcIdea
3、下載好自己需要的庫

4、查看請(qǐng)求網(wǎng)絡(luò)
- 按好F12
- 對(duì)接口進(jìn)行請(qǐng)求
- 先將需要的header請(qǐng)求頭標(biāo)數(shù)據(jù)進(jìn)行驗(yàn)證

- 查看正文請(qǐng)求
5、給你一個(gè)模板自己慢慢體會(huì)
import requests #導(dǎo)入requests包
import random
import json
import time
# response = requests.get('http://www.baidu.com')
# print(response.status_code) # 打印狀態(tài)碼
# print(response.url) # 打印請(qǐng)求url
# print(response.headers) # 打印頭信息
# print(response.cookies) # 打印cookie信息
# print(response.text) #以文本形式打印網(wǎng)頁源碼
# print(response.content) #以字節(jié)流形式打印
# print(response.json())
AddCarURL = "http://localhost:10086/jeecg-boot/cable/insurance/add"
AddCarCookies = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjkwOTUyNzcsInVzZXJuYW1lIjoiYWRtaW4ifQ.Kn1jB5gUkCdnszSVxnjVVKtRMbx_WJSVZW6G-yJaid8"
randomStr = "ABCDEFGHIJKLMNOPQRST"
randomInsurance = ["泰康人壽","陽光保險(xiǎn)","新華保險(xiǎn)","太平人壽","同方全球人壽","友邦保險(xiǎn)"," 招商仁和","平安保險(xiǎn)"]
randomBox = ["石鮮倉儲(chǔ)","明順倉儲(chǔ)","雄星倉儲(chǔ)","金盛倉儲(chǔ)","旺恒倉儲(chǔ)","中豹倉儲(chǔ)","飛翔倉儲(chǔ)","速發(fā)倉儲(chǔ)","春天倉儲(chǔ)"]
sess = requests.session()
headers = {
"X-Access-Token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE2MjkxMDUyMjUsInVzZXJuYW1lIjoiYWRtaW4ifQ.EUAjJYACel8QHFw4AFERIaDjXZJTwOOyp8uncqx3Jps",
"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.19041",
"Referer":"http://localhost:3000/cable/VehicleList",
"Origin":"http://localhost:3000",
"Host":"localhost:10086",
"Content-Type":"application/json; charset=utf-8"
}
a1=(2017,1,1,0,0,0,0,0,0) #設(shè)置開始日期時(shí)間元組(1976-01-01 00:00:00)
a2=(2019,3,16,23,59,59,0,0,0) #設(shè)置結(jié)束日期時(shí)間元組(1990-12-31 23:59:59)
a3=(2019,3,16,0,0,0,0,0,0) #設(shè)置開始日期時(shí)間元組(1976-01-01 00:00:00)
a4=(2021,8,16,23,59,59,0,0,0) #設(shè)置結(jié)束日期時(shí)間元組(1990-12-31 23:59:59)
start=time.mktime(a1) #生成開始時(shí)間戳
end=time.mktime(a2) #生成結(jié)束時(shí)間戳
start1=time.mktime(a3) #生成開始時(shí)間戳
end2=time.mktime(a4) #生成結(jié)束時(shí)間戳
# for i in range(10):
# t=random.randint(start,end) #在開始和結(jié)束時(shí)間戳中隨機(jī)取出一個(gè)
# date_touple=time.localtime(t) #將時(shí)間戳生成時(shí)間元組
# date=time.strftime("%Y-%m-%d",date_touple) #將時(shí)間元組轉(zhuǎn)成格式化字符串(1976-05-21)
#
# t1 = random.randint(start1,end2)
# date_touplen=time.localtime(t1) #將時(shí)間戳生成時(shí)間元組
# dateOne=time.strftime("%Y-%m-%d",date_touplen) #將時(shí)間元組轉(zhuǎn)成格式化字符串(1976-05-21)
# print(date + " " +dateOne)
def addCar(num:int):
# {"type": "2", "carryingCapacity": "3噸", "license": "粵A888", "engineNumber": "123456", "state": 0}
# {"vehicleId": "2", "insuraName": "人壽保險(xiǎn)", "insurancePolicy": "UY1254SD5492W", "insuranceDateBegin": "2021-08-16",
# "insuranceDateEnd": "2021-08-31", "strongPolicy": "UY1254SD5492E", "strongDateBegin": "2021-07-01",
# "strongDateEnd": "2021-08-28", "license": "2"}
for i in range(num):
t = random.randint(start, end) # 在開始和結(jié)束時(shí)間戳中隨機(jī)取出一個(gè)
date_touple = time.localtime(t) # 將時(shí)間戳生成時(shí)間元組
date = time.strftime("%Y-%m-%d", date_touple) # 將時(shí)間元組轉(zhuǎn)成格式化字符串(1976-05-21)
t1 = random.randint(start1, end2)
date_touplen = time.localtime(t1) # 將時(shí)間戳生成時(shí)間元組
dateOne = time.strftime("%Y-%m-%d", date_touplen) # 將時(shí)間元組轉(zhuǎn)成格式化字符串(1976-05-21)
datas = {}
datas["vehicleId"] = "2"
datas["insuraName"] = random.choice(randomInsurance)
datas["insurancePolicy"] = random.choice(randomStr) + str(random.randint(10000, 99999)) + random.choice(randomStr) + str(random.randint(10000, 99999));
datas["insuranceDateBegin"] = date
datas["insuranceDateEnd"] = dateOne
datas["strongPolicy"] = random.choice(randomStr) + str(random.randint(10000, 99999)) + random.choice(randomStr) + str(random.randint(10000, 99999));
datas["strongDateBegin"] = date
datas["strongDateEnd"] = dateOne
datas["license"] = i
print(datas)
res=requests.post(url=AddCarURL, data=json.dumps(datas), headers=headers)
print(res.text)
addCar(100)
記得要通過 token 驗(yàn)證,還有根據(jù)數(shù)據(jù)轉(zhuǎn)json 即可
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
python繪制散點(diǎn)圖詳細(xì)步驟(從0到1必會(huì))
這篇文章主要介紹了如何使用Python繪制散點(diǎn)圖,包括導(dǎo)入包、準(zhǔn)備數(shù)據(jù)、繪制圖像、修飾圖像(添加標(biāo)題、坐標(biāo)軸標(biāo)簽、顏色圖例)以及整合所有代碼,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2024-12-12
python實(shí)現(xiàn)多線程及線程間通信的簡(jiǎn)單方法
這篇文章主要為大家介紹了python實(shí)現(xiàn)多線程及線程間通信的簡(jiǎn)單方法示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-07-07
python 基于selectors庫實(shí)現(xiàn)文件上傳與下載
這篇文章主要介紹了python 基于selectors庫實(shí)現(xiàn)文件上傳與下載的示例代碼,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
pytorch中model.train()和model.eval()用法及說明
在PyTorch中,model.train()用于啟用BatchNormalization和Dropout,保證模型在訓(xùn)練階段能夠有效地利用這些層的特性,而model.eval()則是用于測(cè)試階段,確保BatchNormalization和Dropout不會(huì)影響測(cè)試結(jié)果,保持模型的穩(wěn)定性2024-09-09

