Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

urllib2

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?

like image 724
user2351418 Avatar asked Dec 14 '13 07:12

user2351418


3 Answers

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)
like image 159
James Mills Avatar answered Oct 01 '22 15:10

James Mills


Use thread:

import threading

threading.Thread(target=urllib.urlopen, args=('COOL WEBSITE',)).start()

Don't forget args argument should be tuple. That's why there's trailing ,.

like image 41
falsetru Avatar answered Oct 01 '22 14:10

falsetru


You can do this with requests library as follows

import requests
try:
    requests.get("http://127.0.0.1:8000/test/",timeout=10)
except requests.exceptions.ReadTimeout: #this confirms you that the request has reached server
    do_something
except:
    print "unable to reach server"
    raise

from the above code you can send async requests without getting response. Specify timeout according to your need. if not it will not time out.

like image 32
Mani Avatar answered Oct 01 '22 13:10

Mani