user2351418 user2351418 - 1 year ago 231
Python Question

Python & URLLIB2 - Request webpage but don't wait for response

In python, how would I go about making a http request but not waiting for a response. I don't care about getting any data back, I just need to server to register a page request.

Right now I use this code:

urllib2.urlopen("COOL WEBSITE")

But obviously this pauses the script until a a response is returned, I just want to fire off a request and move on.

How would I do this?

Answer Source

What you want here is called Threading or Asynchronous.


  • Wrap the call to urllib2.urlopen() in a threading.Thread()


from threading import Thread

def open_website(url):
    return urllib2.urlopen(url)

Thread(target=open_website, args=[""]).start()


  • Unfortunately there is no standard way of doing this in the Python standard library.

Use the requests library which has this support.


from requests import async


There is also a 3rd option using the restclient library which has builtin (has for some time) Asynchronous support:

from restclient import GET

res = GET("", async=True, resp=True)