473,408 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

[urllib2] No time-out?

Hello

I'm using urllib2 to download web pages. The strange thing in the code
below, is that it seems like urllib2.urlopen retries indefinitely by
itself instead of raising an exception:

=====
timeout = 30
socket.setdefaulttimeout(timeout)

i = 0
while i < 5:
try:
url = 'http://www.acme.com'
print url
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()
except:
#Never called :-/
print Timed-out."
if i == 4:
print "Exiting."
connection.close(True)
sys.exit()
else:
print "Trying again"
i = i + 1
time.sleep(10)
continue
=====

I haven't found a switch within urllib2 that would tell it to raise an
exception after it times out trying to download a web page. Any idea
how to have it stop trying after 5 tries?

Thank you.
Nov 16 '08 #1
1 3063
On Sun, 16 Nov 2008 12:04:02 +0100, Gilles Ganault wrote:
Hello

I'm using urllib2 to download web pages. The strange thing in the code
below, is that it seems like urllib2.urlopen retries indefinitely by
itself instead of raising an exception:
Try this instead (untested):

timeout = 30
socket.setdefaulttimeout(timeout)
url = 'http://www.acme.com'
for i in range(5):
try:
print url
req = urllib2.Request(url, None, headers)
response = urllib2.urlopen(req).read()
break
except urllib2.URLError:
print "Timed-out."
time.sleep(10)
else:
print "Exiting."
connection.close(True) # What is this?
sys.exit() # Do you really want the application to exit?

--
Steven
Nov 16 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Matthew Wilson | last post by:
I am writing a script to check on my router's external IP address. My ISP refreshes my IP very often and I use dyndns for the hostname for my computer. My Netgear mr814 router has a webserver that...
2
by: John F Dutcher | last post by:
Can anyone comment on why the code shown in the Python error is in some way incorrect...or is there a problem with Python on my hoster's site ?? The highlites don't seem to show here...but line...
1
by: Doug Farrell | last post by:
Hi all, I'm trying to build a web page crawler to help us build our websites, which are driven by static pages after they are called the first time. Anyway, I can use urllib2.urlopen() no...
4
by: Fuzzyman | last post by:
urllib2 (under windows) will auto-detect your proxy settings and use those. Normally that's a good thing (I guess), except when it's not ! How do I switch off this behaviour ? I'm behind a...
0
by: Dan M | last post by:
I'm writing a system monitor script that needs to load web pages. I'm using urllib2.urlopen to get the pages, and I'm attempting to set the timeout value using socket.defaulttimeout. ...
7
by: itay_k | last post by:
Hi, I dont understand why this is so complicated, just to add one line of cookie header on the GET request. This is my unworking code: import time import Cookie import cookielib, urllib2
0
by: Chaos | last post by:
For the Program I Am Making I Make Multiple HTTP Request to My Server. I found that using urllib2 it was pretty slow, when I activated the program and tested it it would hang from 2secs-5secs since...
2
by: ken | last post by:
Hi, i have the following code to load a url. My question is what if I try to load an invalide url ("http:// www.heise.de/"), will I get an IOException? or it will wait forever? Thanks for any...
1
by: darran | last post by:
I'm struggling with using urllib2 to access the Harvest time-tracking web service (http://www.getharvest.com/api). GET is working fine. POST is giving me a problem. Here is an example that...
1
by: leo1980 | last post by:
i use urllib2 to open the websites and get info. it works for a while, then will be stopped, the error in idle is http error 404 no such domain. but i just changed the parameters in the url by...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.