Python request 普通请求的链接不断变成TIME-WAIT的问题
来源:互联网 发布:aso优化 app排名 编辑:程序博客网 时间:2024/06/05 05:07
Table of Contents
- 1 问题描述
- 2 解决方法
- 3 修改后只会保留一个长连接
太忙又太懒,很少写日志。
1 问题描述
import request from time import sleep while True: request.post(your_url, data={"a": 0}) sleep(2)
shell> ss -ant | grep TIME-WAIT (ss 比 netstat 快)
链接不断变成TIME-WAIT状态,每次均开新端口后又关闭,不会自动复用已有port连接,
一来耗资源二来重新连接耗时间。
2 解决方法
2.1 修改request.post写法
参考 http://docs.python-requests.org/en/master/user/advanced/#keep-alive
及request源码 /usr/lib64/python2.7/site-packages/requests/api.py:53
import request from time import sleep sess = request.Session() # session should be outside, use the same session to send req while True: req = request.Request('POST', your_url, data={"a": 0}) prep = sess.prepare_request(req) sess.send(prep, stream=False) sleep(2)
2.2 使用更底层的urllib3发请求
参考
https://urllib3.readthedocs.io/en/latest/user-guide.html
https://urllib3.readthedocs.io/en/latest/advanced-usage.html
import urllib3 from time import sleep http = urllib3.PoolManager(num_pools=100, headers={'Connection':'keep-alive'}, maxsize=100, block=True) # num_pools is for each host, while maxsize (keep opened connections, not max usable) is for ports per host, according to the docs. data = {"a": 0} while True: encoded_body = json.dumps(data).encode('utf-8') http.request('POST', your_url, body=encoded_body) sleep(2)
3 修改后只会保留一个长连接
0 0
- Python request 普通请求的链接不断变成TIME-WAIT的问题
- 请求的链接提取request.path
- kernel: TCP: time wait bucket table overflow的问题
- 关于HttpServer的time-wait
- 关于HttpServer的time-wait
- SNMP++ 01-SNMP request time out问题的解决
- 把普通的JavaProject变成MavenProject
- 修改Tcp的Time-Wait的方法
- 普通win32程序 链接 glewInit不成功的问题
- TCP连接的TIME-WAIT状态
- 点击Ajax.Action生成的链接后,没有启用ajax请求,而是普通超链接的效果
- kernel: TCP: time wait bucket table overflow的问题剖析及解决方法
- 客户端发送的post请求,request中的中文乱码问题
- Visual Studio 2013中在IE浏览器浏览localhost网站时候,发现会不断有下面链接的请求
- tomcat大量time wait问题
- 获取 request 请求的值
- request 获取请求的正文
- 修改Request的请求路径
- php 桥接模式案例
- c++跨平台技术学习(三)--使用标准API
- java-基本的Socket编程-实现服务器端和客户端通信
- iOS10注册极光推送
- 使用jedis 链接redis数据库
- Python request 普通请求的链接不断变成TIME-WAIT的问题
- UIButton背景半透明,字体不透明的实现效果
- Spring之LoadTimeWeaver——一个需求引发的思考
- Android 4.2wifidisplay采集
- USB免驱摄像头采集图像【VS2012+opencv+directShow(CcameraDS)实现】
- 混合移动应用中地图导航功能开发与应用
- Git 学习(篇四 --Git别名)
- Javascript高级ajax、jsonp
- LeetCode: Generate Parentheses