How to Speed Up Python's urllib2 when doing multiple requests

后端 未结 3 1511
深忆病人
深忆病人 2020-12-09 03:24

I am making several http requests to a particular host using python\'s urllib2 library. Each time a request is made a new tcp and http connection is created which takes a no

3条回答
  •  一生所求
    2020-12-09 03:56

    If you switch to httplib, you will have finer control over the underlying connection.

    For example:

    import httplib
    
    conn = httplib.HTTPConnection(url)
    
    conn.request('GET', '/foo')
    r1 = conn.getresponse()
    r1.read()
    
    conn.request('GET', '/bar')
    r2 = conn.getresponse()
    r2.read()
    
    conn.close()
    

    This would send 2 HTTP GETs on the same underlying TCP connection.

提交回复
热议问题