python http请求及多线程应用
2024-08-24 03:25:51
概述
- 今天, 使用
python3
模拟下发包,http get
请求, 然后 采用tomorrow
多线程.
代码如下:
# coding=utf-8
from urllib import request
import json
from tomorrow import threads
import time
@threads(100)
def testgetHtml(url):
requests = request.urlopen(url)
return requests
start = time.time()
url = "https://www.baidu.com/"
print('begin')
responses = [testgetHtml(url) for i in range(100)]
# html = [(json.loads(response.read().decode('utf-8')))['total']
html = [response.read().decode('utf-8') for response in responses]
for x in html:
print(x)
tomorrow
包准备
pip install tomorrow
运行环境遇到的问题
- 移动python目录后, pip 默认安装路径与实际项目不匹配. 现象来说就是: cd 进入项目目录后运行如上安装命令是成功的, 但是运行python程序依然找不到
tomorrow
包, 开始怀疑默认pip路径安装问题. 解决方法步骤如下:- 更改了
path
系统环境, 修改python 及 python\scripts 的路径, 不生效, 错误依旧. - 重新安装
pip
. 方法: pip 官网下载后, 解压.tar.gz
的格式, 然后python setup.py install
安装成功, 错误消失
- 更改了
其他尝试未果
- 尝试一个方法内完成Request 和 response 读取显示.: 各种报错, 未继续.
- 可能的原因: 涉及异步过程, 无法合理追踪, 与预期的多线程有出入.
最新文章
- IKONS – 赞!264 款手工打造的免费矢量图标
- vs2008环境nmake编译 apache 2.2.29 openssl 1.0.1g mod_ssl 不知道如何生成“";..\..\srclib\openssl\inc32\openssl\store.h";”
- 【leetcode】length of last word (easy)
- Python学习路程day8
- JMeter2.13进行压力测试
- opencv实现KNN手写数字的识别
- C#读取Word模板替换相应的字符串(标签)生成新的Word
- java检测端口号是否配占用
- [转]一个备份MySQL数据库的简单Shell脚本
- 【HDOJ】1274 展开字符串
- SQL Server 阻塞排除的 2 方法
- IntentService的使用
- (转)C++中返回对象的情形及RVO
- sencha touch视频教程
- Linux指令--性能监控和优化命令相关指令
- spring 5.1.2 mvc RequestMappingHandlerMapping 源码初始化过程
- eclipse中设置python的版本
- 《HTTP权威指南》1-HTTP概要
- make太慢了,加快编译速度的方法 make -j
- ActiveMQ(2)---ActiveMQ原理分析之消息发送