python之requests urllib3 连接池
0.目录
1.参考
2. pool_connections 默认值为10,一个站点主机host对应一个pool
(4)分析
host A>>host B>>host A page2>>host A page3
限定只保留一个pool(host),根据TCP源端口可知,第四次get才能复用连接。
3. pool_maxsize 默认值为10,一个站点主机host对应一个pool, 该pool内根据多线程需求可保留到某一相同主机host的多条连接
(4)分析
多线程启动时到特定主机host的连接数没有收到 pool_maxsize 的限制,但是之后只有min(线程数,pool_maxsize ) 的连接数能够保留。
后续线程(应用层)并不关心实际会使用到的具体连接(传输层源端口)
1.参考
Requests' secret: pool_connections and pool_maxsize
Python - 体验urllib3 -- HTTP连接池的应用
通过wireshark抓取包:
所有http://ent.qq.com/a/20111216/******.htm对应的src port都是13136,可见端口重用了
2. pool_connections 默认值为10,一个站点主机host对应一个pool
(1)代码
#!/usr/bin/env python
# -*- coding: UTF-8 -* import time
import requests
from threading import Thread import logging logging.basicConfig()
logging.getLogger().setLevel(logging.DEBUG)
requests_log = logging.getLogger("requests.packages.urllib3")
requests_log.setLevel(logging.DEBUG)
requests_log.propagate = True url_sohu_1 = 'http://www.sohu.com/sohu/1.html'
url_sohu_2 = 'http://www.sohu.com/sohu/2.html'
url_sohu_3 = 'http://www.sohu.com/sohu/3.html'
url_sohu_4 = 'http://www.sohu.com/sohu/4.html'
url_sohu_5 = 'http://www.sohu.com/sohu/5.html'
url_sohu_6 = 'http://www.sohu.com/sohu/6.html' url_news_1 = 'http://news.163.com/air/'
url_news_2 = 'http://news.163.com/domestic/'
url_news_3 = 'http://news.163.com/photo/'
url_news_4 = 'http://news.163.com/shehui/'
url_news_5 = 'http://news.163.com/uav/5/'
url_news_6 = 'http://news.163.com/world/6/' s = requests.Session()
s.mount('http://', requests.adapters.HTTPAdapter(pool_connections=1))
s.get(url_sohu_1)
s.get(url_news_1)
s.get(url_sohu_2)
s.get(url_sohu_3)
(2)log输出
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): www.sohu.com #host A
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/1.html HTTP/1.1" 404 None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): news.163.com #host B
DEBUG:urllib3.connectionpool:http://news.163.com:80 "GET /air/ HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): www.sohu.com #host A
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/2.html HTTP/1.1" 404 None #host A page2
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/3.html HTTP/1.1" 404 None #host A page3
(3)wireshark抓包 https过滤方法?用tcp syn? ping m.10010.com 然后 tcp.flags == 0x0002 and ip.dst == 157.255.128.111

(4)分析
host A>>host B>>host A page2>>host A page3
限定只保留一个pool(host),根据TCP源端口可知,第四次get才能复用连接。
3. pool_maxsize 默认值为10,一个站点主机host对应一个pool, 该pool内根据多线程需求可保留到某一相同主机host的多条连接
(1)代码
def thread_get(url):
s.get(url)
def thread_get_wait_3s(url):
s.get(url)
time.sleep(3)
s.get(url)
def thread_get_wait_5s(url):
s.get(url)
time.sleep(5)
s.get(url) s = requests.Session()
s.mount('http://', requests.adapters.HTTPAdapter(pool_maxsize=2))
t1 = Thread(target=thread_get_wait_5s, args=(url_sohu_1,))
t2 = Thread(target=thread_get, args=(url_news_1,))
t3 = Thread(target=thread_get_wait_3s, args=(url_sohu_2,))
t4 = Thread(target=thread_get_wait_5s, args=(url_sohu_3,))
t1.start()
t2.start()
t3.start()
t4.start()
t1.join()
t2.join()
t3.join()
t4.join()
(2)log输出
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): www.sohu.com #pool_sohu_connection_1_port_54805
DEBUG:urllib3.connectionpool:Starting new HTTP connection (1): news.163.com #pool_163_connection_1_port_54806
DEBUG:urllib3.connectionpool:Starting new HTTP connection (2): www.sohu.com #pool_sohu_connection_2_port_54807
DEBUG:urllib3.connectionpool:Starting new HTTP connection (3): www.sohu.com #pool_sohu_connection_3_port_54808
DEBUG:urllib3.connectionpool:http://news.163.com:80 "GET /air/ HTTP/1.1" 200 None
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/3.html HTTP/1.1" 404 None
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/2.html HTTP/1.1" 404 None
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/1.html HTTP/1.1" 404 None
WARNING:urllib3.connectionpool:Connection pool is full, discarding connection: www.sohu.com #pool_sohu_connection_1_port_54805 被丢弃?最初host sohu能够建立3条连接,之后终究只能保存2条???
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/2.html HTTP/1.1" 404 None #pool_sohu_connection_2_port_54807 3秒后sohu/2复用了原来sohu/2的端口
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/3.html HTTP/1.1" 404 None #pool_sohu_connection_2_port_54807 5秒后sohu/3复用了原来sohu/2的端口
DEBUG:urllib3.connectionpool:http://www.sohu.com:80 "GET /sohu/1.html HTTP/1.1" 404 None #pool_sohu_connection_3_port_54807 5秒后sohu/1复用了原来sohu/3的端口
(3)wireshark抓包

(4)分析
多线程启动时到特定主机host的连接数没有收到 pool_maxsize 的限制,但是之后只有min(线程数,pool_maxsize ) 的连接数能够保留。
后续线程(应用层)并不关心实际会使用到的具体连接(传输层源端口)
python之requests urllib3 连接池的更多相关文章
- python中实现mysql连接池
python中实现mysql连接池 import pymysql from DBUtils.PooledDB import PooledDB MYSQL_HOST = 'localhost' USER ...
- 【转载-译文】requests库连接池说明
转译自:https://laike9m.com/blog/requests-secret-pool_connections-and-pool_maxsize,89/ Requests' secret: ...
- python socketpool:通用连接池(转)
简介 在软件开发中经常要管理各种“连接”资源,通常我们会使用对应的连接池来管理,比如mysql数据库连接可以用sqlalchemy中的池来管理,thrift连接可以通过thriftpool管理,red ...
- python socketpool:通用连接池
简介 在软件开发中经常要管理各种“连接”资源,通常我们会使用对应的连接池来管理,比如mysql数据库连接可以用sqlalchemy中的池来管理,thrift连接可以通过thriftpool管理,red ...
- Python下Mysql数据连接池——单例
# coding:utf-8 import threading import pymysql from DBUtils.PooledDB import PooledDB from app.common ...
- python操作Redis安装、支持存储类型、普通连接、连接池
一.python操作redis安装和支持存储类型 安装redis模块 pip3 install redis 二.Python操作Redis之普通连接 redis-py提供两个类Redis和Strict ...
- Python使用urllib,urllib3,requests库+beautifulsoup爬取网页
Python使用urllib/urllib3/requests库+beautifulsoup爬取网页 urllib urllib3 requests 笔者在爬取时遇到的问题 1.结果不全 2.'抓取失 ...
- urllib3使用池管理发送请求和requests常用方法的基本使用+session使用
使用urllib3的池管理器 urllib3是在urllib进行更加深入的改进,最大的好处就是在urllib的基础上添加了池管理,以至于我们不需要再去注意我们需要由那个链接去发送请求,而只需要获取到链 ...
- 用python自定义实现db2的连接池
想要模仿zabbix的oracle插件orabix来实现对db2的监控,但是Java能力有限,就用python来实现了.但是python常用的连接池PooledDB似乎并不支持db2,一直报这样的错误 ...
随机推荐
- linux系统的三种网络连接模式
VMWare提供了三种工作模式,它们是bridged(桥接模式).NAT(网络地址转换模式)和host-only(主机模式).要想在网络管理和维护中合理应用它们,你就应该先了解一下这三种工作模式. 1 ...
- 洛谷:P3281 [SCOI2013]数数 (优秀的解法)
刷了这么久的数位 dp ,照样被这题虐,还从早上虐到晚上,对自己无语...(机房里又是只有我一个人,寂寞.) 题目:洛谷P3281 [SCOI2013]数数 题目描述 Fish 是一条生活在海里的鱼, ...
- 线性回归,逻辑回归,神经网络,SVM的总结
目录 线性回归,逻辑回归,神经网络,SVM的总结 线性回归,逻辑回归,神经网络,SVM的总结 详细的学习笔记. markdown的公式编辑手册. 回归的含义: 回归就是指根据之前的数据预测一个准确的输 ...
- 通过python统计nginx日志定位php网站响应慢的问题
# 公司网站反映很慢,可能是一些页面的访问方法或者页面引起,通过程序统计nginx访问日志的页面和具体的action方法访问次数以及平均响应时间可以为程序开发的同事提供参考定位具体的代码 # 默认的n ...
- 前端 -----函数和伪数组(arguments)
函数 函数:就是将一些语句进行封装,然后通过调用的形式,执行这些语句. 函数的作用: 将大量重复的语句写在函数里,以后需要这些语句的时候,可以直接调用函数,避免重复劳动. 简化编程,让编程模块化. ...
- 基于MVC 的Quartz.Net组件实现的定时执行任务调度
新建mvc项目之后,首先引用Quartz组件.工具-->NuGet包管理器-->管理解决方案的 NuGet包管理器 组件安装完成. Quartz.Net一个最简单任务至少包括三部分实现:j ...
- CSS入门(二)
一.组合选择器 每个选择器位可以是任意基础选择器或选择器组合 1.群组选择器 可以一次性控制多个选择器 选择器之间用逗号(,)隔开 div,.d1,#div{ color:red; } 2.子代(后代 ...
- Win10 SQL Server 2017安装教程
Win10 SQL Server 2017安装教程 1:下载地址 2:开始安装 1:安装环境预备说明 还要注意就是要先下载这个VC++的更新,可以解决服务器安装不上的问题,下载链接 :Microsof ...
- Confluence 6 关于 Decorators
Confluence 是使用开源的 SiteMesh 库构建的.一个 Web 页面的布局系统,这个布局系统能够在全站点中提供完整统一的界面和外观.SiteMesh 是通过 "decorato ...
- Python安装、卸载第三方模块
pip command ModuleName command:用于指定要执行的命令(install:安装,uninstall:卸载) ModuleName:需要安装的模块名称 示例: 安装第三方模块n ...