[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Re: [python] How to detect a remote webpage is accessible? (in HTTP

Astan Chee

1/18/2008 5:29:00 AM

How about:

import socket, urllib2

timeout = 10
socket.setdefaulttimeout(timeout)
try:
auth_handler = urllib2.HTTPBasicAuthHandler()
opener = urllib2.build_opener(auth_handler) #this used if we need
authentication
urllib2.install_opener(opener)
req = urllib2.Request('http://websit...)
f = urllib2.urlopen(req)
notes= f.readlines()
f.close()
print "Everything is ok"
except IOError, r:
p = str(r)
if re.search(r'urlopen error timed out',p):
print "Web page timed out"

You'll need to set up the timeout to whatever duration your website
takes to load.
Cheers
Astan

?? wrote:
> Howdy, all,
> I want to use python to detect the accessibility of website.
> Currently, I use urllib
> to obtain the remote webpage, and see whether it fails. But the problem is that
> the webpage may be very large; it takes too long time. Certainly, it
> is no need to download
> the entire page. Could you give me a good and fast solution?
> Thank you.
> --
> ShenLei
>
1 Answer

coldpizza

1/18/2008 9:21:00 AM

0

I suppose that if the file is really big and you don't need to read
all of it
then instead of f.readlines() you could use f.read(256) to read just
the first 256 bytes.

On Jan 18, 7:28 am, Astan Chee <st...@al.com.au> wrote:
> How about:
>
> import socket, urllib2
>
> timeout = 10
> socket.setdefaulttimeout(timeout)
> try:
> auth_handler = urllib2.HTTPBasicAuthHandler()
> opener = urllib2.build_opener(auth_handler) #this used if we need
> authentication
> urllib2.install_opener(opener)
> req = urllib2.Request('http://websit...)
> f = urllib2.urlopen(req)
> notes= f.readlines()
> f.close()
> print "Everything is ok"
> except IOError, r:
> p = str(r)
> if re.search(r'urlopen error timed out',p):
> print "Web page timed out"
>
> You'll need to set up the timeout to whatever duration your website
> takes to load.
> Cheers
> Astan
>
>
>
> ?? wrote:
> > Howdy, all,
> >      I want to use python to detect the accessibility of website.
> > Currently, I use urllib
> > to obtain the remote webpage, and see whether it fails. But the problem is that
> > the webpage may be very large; it takes too long time. Certainly, it
> > is no need to download
> > the entire page. Could you give me a good and fast solution?
> >     Thank you.
> > --
> > ShenLei