Python requests speed up using keep-alive

前端 未结 1 1467
一整个雨季
一整个雨季 2020-12-07 18:41

In the HTTP protocol you can send many requests in one socket using keep-alive and then receive the response from server at once, so that will significantly speed up whole p

相关标签:
1条回答
  • 2020-12-07 19:06

    Yes, there is. Use requests.Session and it will do keep-alive by default.

    I guess I should include a quick example:

    import logging
    import requests
    
    logging.basicConfig(level=logging.DEBUG)
    s = requests.Session()
    s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
    s.get('http://httpbin.org/cookies/set/anothercookie/123456789')
    r = s.get("http://httpbin.org/cookies")
    print(r.text)
    

    You will note that these log message occur

    INFO:requests.packages.urllib3.connectionpool:Starting new HTTP connection (1): httpbin.org
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies/set/sessioncookie/123456789 HTTP/1.1" 302 223
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 55
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies/set/anothercookie/123456789 HTTP/1.1" 302 223
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90
    

    If you wait a little while, and repeat the last get call

    INFO:requests.packages.urllib3.connectionpool:Resetting dropped connection: httpbin.org
    DEBUG:requests.packages.urllib3.connectionpool:"GET /cookies HTTP/1.1" 200 90
    

    Note that it resets the dropped connection, i.e. reestablishing the connection to the server to make the new request.

    0 讨论(0)
提交回复
热议问题