Multiple (asynchronous) connections with urllib2 or other http library?

前端 未结 6 1019
耶瑟儿~
耶瑟儿~ 2020-11-29 04:43

I have code like this.

for p in range(1,1000):
    result = False
    while result is False:
        ret = urllib2.Request(\'http://server/?\'+str(p))
               


        
6条回答
  •  情深已故
    2020-11-29 05:18

    You can use asynchronous IO to do this.

    requests + gevent = grequests

    GRequests allows you to use Requests with Gevent to make asynchronous HTTP Requests easily.

    import grequests
    
    urls = [
        'http://www.heroku.com',
        'http://tablib.org',
        'http://httpbin.org',
        'http://python-requests.org',
        'http://kennethreitz.com'
    ]
    
    rs = (grequests.get(u) for u in urls)
    grequests.map(rs)
    

提交回复
热议问题