Python实现多并发访问网站功能示例
作者:微烟波 时间:2022-12-16 11:42:47
本文实例讲述了Python实现多并发访问网站功能。分享给大家供大家参考,具体如下:
# Filename:visitweb_threads.py
# Description:python visit web, get startTime, endTime, everytimes spentTime,threading
import threading
import urllib
import time
import datetime
print 'num web SpentTime'
def Process(url,n):
minSpan = 0.0
maxSpan = 0.0
sumSpan= 0.0
over1s = 0
file = open('data.txt','a') # save Data
for i in range(n):
startTime =datetime.datetime.now()
try:
urlItem = urllib.urlopen(url)
htmSource = urlItem.read()
urlItem.close()
except:
pass
endTime = datetime.datetime.now()
span = (endTime-startTime).total_seconds()
sumSpan = sumSpan + span
if span < minSpan:
minSpan = span
if span > maxSpan:
maxSpan = span
if span>1:
over1s=over1s + 1
print(u'%4d %s Spent:%7s seconds'%(i,url,span))
file.write(u'%4d %s ST:%s ET:%s Spent :%s seconds\n'%(i,url,startTime,endTime,span))
file.write('\n')
print(u'\n requested:%s times\n Total Spent:%s seconds\n avg:%s seconds\n max:%s seconds\n min:%s seconds\n over 1 secnod:%s times\n'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))
file.write(u' requested:%s times\n Total Spent:%s seconds\n avg:%s seconds\n max:%s seconds\n min:%s seconds\n over 1 secnod:%s times\n'%(n,sumSpan,sumSpan/n,maxSpan,minSpan,over1s))
file.close()
class ThreadClass(threading.Thread):
def run(self):
now = datetime.datetime.now()
print "%s says Hello World at time: %s" % (self.getName(), now)
file = open('threads_data.txt','a') # save threads_data
file.write( "%s says Hello World at time: %s\n" % (self.getName(), now))
Process('http://222.20.6.184/main.aspx',10) # visit website 网站的Url和每个进程的访问次数
now = datetime.datetime.now()
print "%s says Goodbye at time: %s" % (self.getName(), now)
file.write( "%s says Goodbye at time: %s\n" % (self.getName(), now))
file.close()
if __name__=='__main__':
# file = open('threads_data.txt','w')
# file.close()
# file = open('data.txt','w')
# file.close()
for i in range(1000): # 多少次同时并发访问
t = ThreadClass()
t.start()
希望本文所述对大家Python程序设计有所帮助。
标签:Python,并发,访问
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python截取两个单词之间的内容方法
2022-06-02 01:48:46
教你如何看懂SQL Server查询计划
2024-01-12 21:10:41
![](https://img.aspxhome.com/file/2023/1/122371_0s.gif)
详解vue中v-on事件监听指令的基本用法
2023-07-02 17:06:37
![](https://img.aspxhome.com/file/2023/0/139930_0s.png)
一个完整的PHP类包含的七种语法说明
2023-11-16 13:02:07
face++与python实现人脸识别签到(考勤)功能
2021-10-18 09:06:06
![](https://img.aspxhome.com/file/2023/4/84044_0s.jpg)
Django 解决由save方法引发的错误
2022-04-07 06:06:56
Go语言使用swagger生成接口文档的方法
2023-08-28 06:23:23
![](https://img.aspxhome.com/file/2023/4/100794_0s.jpg)
Python实战之基于OpenCV的美颜挂件制作
2022-08-30 20:46:30
![](https://img.aspxhome.com/file/2023/9/97279_0s.jpg)
Python with语句上下文管理器两种实现方法分析
2023-03-21 21:50:18
基于python实现判断字符串是否数字算法
2022-10-15 00:46:58
![](https://img.aspxhome.com/file/2023/1/83831_0s.png)
Python正则表达式中的量词符号与组问题小结
2022-12-14 15:24:24
![](https://img.aspxhome.com/file/2023/3/64673_0s.jpg)
使用Python的SymPy库解决数学运算问题的方法
2021-04-19 19:36:35
mysql 索引使用及优化详情
2024-01-24 16:06:56
![](https://img.aspxhome.com/file/2023/2/118462_0s.png)
查看python安装路径及pip安装的包列表及路径
2021-10-25 01:36:48
![](https://img.aspxhome.com/file/2023/7/75287_0s.jpg)
Python面向对象实现方法总结
2022-03-11 08:50:41
MySQL 加密/压缩函数
2024-01-23 23:51:14
![](https://img.aspxhome.com/file/2023/8/121568_0s.gif)
python 中的9个实用技巧,助你提高开发效率
2021-05-01 08:26:25
Pandas之Fillna填充缺失数据的方法
2022-11-17 20:17:12
ASP向Excel导数据(图片)终结版 ASP操作Excel
2011-03-25 10:48:00
小米正式开源 SQL 智能优化与改写工具 SOAR
2024-01-20 09:34:56
![](https://img.aspxhome.com/file/2023/0/119260_0s.jpg)