473,799 Members | 2,727 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Question about using urllib2 to load a url

ken
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 help.

opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
urllib2.install _opener(opener)

txheaders = {'User-agent': 'Mozilla/5.0 (X11; U; Linux i686; en-
US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'}

try:
req = Request(url, txdata, txheaders)
handle = urlopen(req)
except IOError, e:
print e
print 'Failed to open %s' % url
return 0;

Apr 1 '07 #1
2 12401
On Apr 2, 2:52 am, "ken" <ken.carl...@gm ail.comwrote:
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?
Depends on why the URL is invalid. If the URL refers to a non-
existent domain, a DNS request will result in error and you will get
an "urllib2.URLErr or: <urlopen error (-2, 'Name or service not
known')>". If the name resolves but the host is not reachable, the
connect code will timeout (eventually) and result in an
"urllib2.URLErr or: <urlopen error (113, 'No route to host')>". If the
host exists but does not have a web server running, you will get an
"urllib2.URLErr or: <urlopen error (111, 'Connection refused')>". If a
webserver is running but the requested page does not exist, you will
get an "urllib2.HTTPEr ror: HTTP Error 404: Not Found".

The URL you gave above does not meet any of these conditions, so
results in a valid handle to read from.

If, at any time, an error response fails to reach your machine, the
code will have to wait for a timeout. It should not have to wait
forever.
Thanks for any help.

opener = urllib2.build_o pener(urllib2.H TTPCookieProces sor(cj))
urllib2.install _opener(opener)

txheaders = {'User-agent': 'Mozilla/5.0 (X11; U; Linux i686; en-
US; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3'}

try:
req = Request(url, txdata, txheaders)
handle = urlopen(req)
except IOError, e:
print e
print 'Failed to open %s' % url
return 0;
--
Kushal

Apr 2 '07 #2
"Kushal Kumaran" <ku************ @gmail.comwrite s:
[...]
If, at any time, an error response fails to reach your machine, the
code will have to wait for a timeout. It should not have to wait
forever.
[...]

....but it might have to wait a long time. Even if you use
socket.setdefau lttimeout(), DNS lookups can block for a long time.
The way around that is to use Python threads (no need to try to "kill"
the thread that's doing the urlopen() -- just ignore that thread if it
takes too long to finish).

Looks like 2.6 will have socket timeouts exposed at the urllib2 level
(so no need to call socket.setdefau lttimeout() any more), but the need
to use threads with urllib2 to get timeouts will remain in many cases,
due to the DNS thing (the same applies to urllib, or any other module
that ends up doing DNS lookups).
John

Apr 4 '07 #3

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

Similar topics

4
4463
by: bmiras | last post by:
I've got a problem using urllib2 to get a web page. I'm going through a proxy using user/password authentification and i'm trying to get a page asking for a HTTP authentification. And I'm using python 2.3 Here is an exemple of the piece of code I use: import urllib2 #Proxy handler proxy_handler = urllib2.ProxyHandler({"http" :
0
2659
by: jacob c. | last post by:
When I request a URL using urllib2, it appears that urllib2 always makes the request using HTTP 1.0, and not HTTP 1.1. I'm trying to use the "If-None-Match"/"ETag" HTTP headers to conserve bandwidth, but if I'm not mistaken, these are HTTP 1.1 headers, so I can't reasonably expect a web server to respond correctly to my requests. (In my limited testing, it looks like some servers respond correctly with an HTTP 304 status, while some...
1
2830
by: dmbkiwi | last post by:
I've been using urllib2 to try and automate logging into the google adsense page. I want to download the csv report files, so that I can do some analysis of them. However, I don't really know how web forms work, and the examples on the python.org/doc site aren't really helpful. I've found working scripted login code using javascript, but I don't speak javascript. These are the relevant functions:
0
1951
by: abrahamvk | last post by:
Hi, How to load data into Japanese DB2 Database using DB2 Load Utility, where the table column names are in Japanese in windows environment. We could successfully load Japanese data into a table which has English column names only. The DB2 Load Utility could not understand a table that has column names in Japanese. We tried loading data after setting the following 1. Setting the DB2CodePage to 1208 2. Setting the environment...
1
1370
by: yinglcs | last post by:
I have the following code to load a url (address). When I have a url like this, http://www.testcom.co.uk/dev_12345/www.cnn.com i get an error "Failed to open http://www.testcom.co.uk/dev_12345/www.cnn.com". Is there something wrong with my URL? or something wrong with my code? Thank you for any help.
3
2179
by: Peter | last post by:
Hi I can create types from dynamic loaaded assemblies (Assembly.Load) with Assembly.GetTyp( <typename) - OK I need a reference to the assembly (just Type.GetType() do not work ) now my question. 1) If i need different Types/Objects of the same Assembly in different app.sections, should I use Assembly.Load() several times or shoud I
1
1942
by: chainsinthewall | last post by:
Me and a friend of mine are trying to make a file sharing program using python. I have a book that recommends using urllib2 for file transfers. But which ever we end up using, i cant find any explanation on how to use either for simple file transfers.
5
1735
by: SM | last post by:
Hello I have simple question using simpleXML and PHP. i have an xml file that looks like this: <?xml version="1.0" encoding="UTF-8"?> <discography version="0.01"> <CD> <title>Moonlight</title> <year>1978</year>
3
1974
by: Alexnb | last post by:
Okay, I tried to follow that, and it is kinda hard. But since you obviously know what you are doing, where did you learn this? Or where can I learn this? Maric Michaud wrote: class=r>.*?</h2>', -- View this message in context: http://www.nabble.com/using-urllib2-tp18150669p18160312.html
0
9689
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9550
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10269
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10248
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10032
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6811
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5469
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5597
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2942
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.