Python request 普通请求的链接不断变成TIME-WAIT的问题

太忙又太懒,很少写日志。

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 快)
img
链接不断变成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
img
img

    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 修改后只会保留一个长连接

img

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值