How to detect a remote webpage is accessible? (in HTTP)

coldpizza vriolk at gmail.com
Fri Jan 18 04:20:40 EST 2008


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... at 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://website.com')
> 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




More information about the Python-list mailing list