前排提醒:本文代码仅用于测试个人网站,请不要在未经他人允许的情况下压测或攻击他人网站,发生的后果及法律纠纷与本作者无关;代码对于低性能电脑可能会造成死机等故障,请知悉

1.前言

继上篇更新了网站架构、缓存、服务器后,是时候改压测一下网站了,翻遍了小破站和百度,始终找不到一款够“狠”的压测软件,干脆自己写一款

2.代码

2.1运行库

注意,代码需要:os、time、threading、requests

threading可以使用

pip install threading

安装

2.2代码

import os
import time
from threading import *

#UA头,访问速度较快,可以在防火墙内加白关键词为:Speedtest的UA
ua={"User-Agent": "Speedtest/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36"}

ua=str(ua)

def stop():#访问计算函数
    print('计算请求中,请稍等')
    time.sleep(4)
    kbs = 0
    with open('./200.txt', 'r', encoding='utf-8') as f:
        reads = f.read()
    for a in reads:
        if a == '^':
            kbs += 1
    print('正常访问数量:' + str(kbs))
    print('运行结束')

class startTest():#主函数
    def del_file(self,path):#清除临时文件
        ls = os.listdir(path)
        for i in ls:
            c_path = os.path.join(path, i)
            if os.path.isdir(c_path):
                self.del_file(c_path)
            else:
                os.remove(c_path)
        print('请求发送完成,临时文件已清空,请等待进程结束生成结果')

    def main(self,luarch,url,pythonpath):#函数入口
        print('开始运行')
        try:os.system('mkdir tmp')
        except Exception as e:pass
        with open('./done.txt','w',encoding='utf-8') as f:
            f.write('')
        with open('./200.txt','w',encoding='utf-8') as f:
            f.write('')
        self.makedir(luarch,url,pythonpath)
    def startcode(self,dir,pythonpath):#启动代码函数
        os.system('"'+pythonpath+'" '+dir)
    def makedir(self,num,url,pythonpath):#生成文件并开线程启动
        for i in range(num):
            with open('./tmp/'+str(i)+'.py','w',encoding='utf-8') as f:
                f.write("import requests\nimport os\nimport sys\ntry:\n    res=requests.get('"+url+"',headers="+ua+")\nexcept Exception as e:\n    print('线程'+os.path.basename(__file__).replace('.py','')+' Leave')\n    sys.exit()\nwith open('./done.txt','a',encoding='utf-8') as f:\n    f.write('^')\nif str(res.status_code) == '200':\n    with open('./200.txt','a',encoding='utf-8') as f:\n        f.write('^')\nprint('线程'+os.path.basename(__file__).replace('.py','')+' Leave')\nsys.exit()")
                Thread(target=self.startcode, args=(os.getcwd() + '\\tmp\\' + str(i) + '.py',pythonpath)).start()#启动线程
                print('线程'+str(i)+' Start')
        self.waiting(num)
        self.del_file(os.getcwd()+'\\tmp')

    def waiting(self,num):#等待所有线程结束
        nubs=0
        for i in range(25):
            with open('./done.txt', 'r', encoding='utf-8') as f:
                reads=f.read()
            for a in reads:
                if a=='^':
                    nubs+=1
            if nubs>=int(num-1):
                break
            time.sleep(1)



with open('./conf.txt','r',encoding='utf-8') as f:
    conf=f.read().split(',')

startTest.main(startTest(), int(conf[0]), conf[1], conf[2])#启动
Thread(target=stop, args=()).run()#启动计算线程


time.sleep(1000)

2.3使用方法

在使用的时候要在Python代码同步录下新建一个名为:conf.txt的文本文档,里面写入

也可以直接下载样例压缩包

https://libs.lowion.cn/PythonREQ.zip

3.完成

咕咕咕咕咕咕咕咕咕咕咕咕咕咕(翻译:这篇文章好水啊我都看不下去了)