當前位置:
首頁 > 知識 > 用Python做大批量請求發送

用Python做大批量請求發送

大批量請求發送需要考慮的幾個因素:

1. 伺服器承載能力(網路帶寬/硬體配置);

2. 客戶端IO情況, 客戶端帶寬, 硬體配置;

方案:

1. 方案都是相對的;

2. 因為這裡我的情況是客戶機只有一台,所以不能考慮使用分散式了, 伺服器承載能力也非常有限(經過不斷調試得知);

3. 這裡沒有使用Jmeter, 雖然jmeter也是可以做到的.

Python裡面支持發送大批量的方案有很多, 這裡只介紹我所用過的幾種:

grequests可以一次性發送超大批量的請求, 但是底層聽說修改了socket通信, 可能不穩定或者不安全? 而且如果你需要校驗對比每個請求的發送信息與返回信息, 比較不方便, 因為它是批量發送,然後批量收回, 示例代碼:

1 import grequests
2 import time
3 from collections import OrderedDict
4 import hashlib
5 import os
6 import xlrd
7 import json
8 import datetime
9
10 class voiceSearchInterface:
11 @classmethod
12 def formatUrlAndHeader(self, des, singer, songName):
13 #演算法邏輯, 根據傳進來的參數生成url和header45 return url, h
46
47 @classmethod
48 def exeRequests(self):
49 errorLog = open("errorLog.txt","w+")
50 startTime = datetime.datetime.now
51 rightCount = 0
52 errCount = 0
53 descr = ["播放", "搜索", "搜", "聽", "我要聽", "我想聽", "來一首", "來一個", "來一段", "來一曲", "來首", "來個", "來段", "來曲"]
54 orgPath = os.path.join(os.path.dirname(os.getcwd), "test","SongsAndSingers","singersAndSongs3.txt")
55 f = open(orgPath,"rb")
56 i = 0
57 urlsAndHs =
58 for line in f.readlines:
59 temp = line.decode.split(" ")
60 orgSinger = temp[0]
61 orgSong = temp[1].replace("
","")
62 for k in descr:
63 urlAndH = self.formatUrlAndHeader(k, orgSinger,orgSong)
64 urlsAndHs.append(urlAndH)
65 f.close
66 rs = (grequests.get(u[0], headers = u[1], stream = False) for u in urlsAndHs)
#size表示一次發出去的請求數量 67 rsText = grequests.imap(rs, size=20) 68 for r in rsText: 69 executingLog = open("Log.txt","w+") 70 i+=1 71 try: 72 searchResult = json.loads(r.text) 73 searchItem = searchResult["data"]["searchitem"] 74 tt = searchItem.split("Split") 75 searchSinger = tt[1] 76 searchSong = tt[-1] 77 resultSinger = searchResult["data"]["sounds"][0]["singer"] 78 resultSong = searchResult["data"]["sounds"][0]["title"] 79 if(searchSinger==resultSinger and searchSong==resultSong): 80 81 rightCount += 1 82 else: 83 84 errCount += 1 85 print(searchSinger, " ",resultSinger, " ",searchSong," ", resultSong) 86 except Exception: 87 errCount += 1 88 errorLog.write((r.text+"
").encode("latin-1").decode("unicode_escape")) 89 print(i) 90 executingLog.write(str(int(i/14))) 91 errorLog.close 92 executingLog.close 93 endTime = datetime.datetime.now 94 print("耗時: %d秒, 正確數: %d, 異常數: %d, 總數: %d, 通過率: %.2f%%" % ((endTime-startTime).seconds, rightCount, errCount, i, (rightCount)/i*100)) 95 96 voiceSearchInterface.exeRequests

喜歡這篇文章嗎?立刻分享出去讓更多人知道吧!

本站內容充實豐富,博大精深,小編精選每日熱門資訊,隨時更新,點擊「搶先收到最新資訊」瀏覽吧!


請您繼續閱讀更多來自 科技優家 的精彩文章:

C 6.0 內插字元串 (Interpolated Strings)
nginx實現請求的負載均衡 + keepalived實現nginx的高可用
程序員的自我修養三目標文件里有什麼
Http跨域時的Option請求
ARP與RARP協議及arp腳本

TAG:科技優家 |

您可能感興趣

Python Web 之百萬並發請求
蘋果「徹底爆發」:iPhoneX Plus再次確認 「請求」老用戶回來
謹防Windows PowerShell憑證請求提示
php中curl同時發送多個請求curl_multi函數集的用法
Servlet 客戶端 HTTP 請求
使用 gorilla/mux 進行 HTTP 請求路由和驗證
高性能的 PHP 封裝的 HTTP Restful 多線程並發請求庫-MultiHttp
VSCode 引入 Python 語言伺服器;Canonical 請求幫助測試 Ubuntu 伺服器安裝程序
SpringMVC 開發 — 使用 Swagger 搭建介面請求頁面
OpTic經理曬Uzi合影,Arrow請求:求求你別洗手,把Uzi能量帶給我!
netty整合springMVC,實現高效的HTTP服務請求
Tinder正在請求Unicode為異族夫婦添加專用表情符號
微軟承包商仍在收聽Cortana請求和Skype電話
前端基於angular6的ionic3,後端基於springmvc post get請求實現
玩家郵件請求遊戲打折,Devolver Digital就真的照辦了
高通再訴蘋果,請求禁售iPhone XS/XR
Nginx配置SSL實現https請求並重定向http請求的實現
看我如何利用Webhook繞過支付請求
蝶式鍵盤引發蘋果MacBook Pro危機 評價天壤之別 部分用戶請求召回
Kotlin項目下的Retrofit2網路請求框架