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

前端 未结 16 1262
暖寄归人
暖寄归人 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:29

    A solution using tornado asynchronous networking library

    from tornado import ioloop, httpclient
    
    i = 0
    
    def handle_request(response):
        print(response.code)
        global i
        i -= 1
        if i == 0:
            ioloop.IOLoop.instance().stop()
    
    http_client = httpclient.AsyncHTTPClient()
    for url in open('urls.txt'):
        i += 1
        http_client.fetch(url.strip(), handle_request, method='HEAD')
    ioloop.IOLoop.instance().start()
    

提交回复
热议问题