Python_Dude
Python_Dude

Reputation: 507

Urlib in python, how to check response received or not?

I am using urlib library in python, For any error in URL ,I am using try catch block to catch it. But sometimes I am getting empty data in url, how to check or validate the empty data from URL .And also using the timeout , given 25 seconds.is it good to give 25 seconds or it should be below 10?

Upvotes: 1

Views: 166

Answers (1)

dano
dano

Reputation: 94901

You can use whatever timeout length is appropriate for your program. If you expect that it might sometimes take whatever URL you're querying up to 25 seconds to respond, then 25 is appropriate. If it should never take more than a few seconds to respond, and you can safely assume that if it's taken longer than a few seconds the URL must be dead, then you can lower the timeout. In general I think it's a good idea to be conservative with timeouts. It's better to make the error case a little slower with a timeout that's too long, rather than falsely triggering an error with a timeout that's too short.

You can check for an empty response from urllib2 by doing something like this

fh = urllib2.urlopen(url)
response = fh.read()
if not response:
    # Do whatever error handling you want. You don't necessarily need to raise Exception.
    raise Exception("Empty response")

Is that what you're looking for?

Upvotes: 1

Related Questions