What is the fastest way to send 100,000 HTTP requests in Python?

前端 未结 16 968
暖寄归人
暖寄归人 2020-11-22 07:12

I am opening a file which has 100,000 URL\'s. I need to send an HTTP request to each URL and print the status code. I am using Python 2.6, and so far looked at the many con

16条回答
  •  太阳男子
    2020-11-22 07:20

    Use grequests , it's a combination of requests + Gevent module .

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

    Usage is simple:

    import grequests
    
    urls = [
       'http://www.heroku.com',
       'http://tablib.org',
       'http://httpbin.org',
       'http://python-requests.org',
       'http://kennethreitz.com'
    ]
    

    Create a set of unsent Requests:

    >>> rs = (grequests.get(u) for u in urls)
    

    Send them all at the same time:

    >>> grequests.map(rs)
    [, , , , ]
    

提交回复
热议问题