user2351418 user2351418 - 4 months ago 88
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

What you want here is called Threading or Asynchronous.

Threading:

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

Example:

from threading import Thread

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

Thread(target=open_website, args=["http://google.com"]).start()

Asynchronous:

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

Use the requests library which has this support.

Example:

from requests import async

async.get("http://google.com")

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("http://google.com", async=True, resp=True)
Comments