how to check if URL cannot be opened

Discussion in 'Python' started by john, Oct 27, 2004.

  1. john

    john Guest

    Im new to python. So I was hoping someone could provide the following.
    How would I go about checking if a user can be opened. For example if
    user provides www.x.com, how would I check if there is such a url?
    What methods would I need to invoke?

    Thank you very much for your time
     
    john, Oct 27, 2004
    #1
    1. Advertising

  2. john

    Josef Meile Guest

    Hi John,

    > Im new to python. So I was hoping someone could provide the following.
    > How would I go about checking if a user can be opened. For example if
    > user provides www.x.com, how would I check if there is such a url?
    > What methods would I need to invoke?


    This is the code I use with python greater than 2.3.3:

    import urllib2
    import socket

    def checkUrl(url, timeout=5, SSL=0):
    """Checks an url for a python version greater
    than 2.3.3.
    """

    defTimeOut=socket.getdefaulttimeout()
    socket.setdefaulttimeout(timeout)
    found=1
    try:
    urllib2.urlopen(url)
    except (urllib2.HTTPError, urllib2.URLError,
    socket.error, socket.sslerror):
    found=0
    socket.setdefaulttimeout(defTimeOut)
    return found

    Please note that I use the setdefaulttimeout method of the module socket
    because sometimes, specially if you type invalid ssl urls, the main
    thread will take a long time till you see an answer. With the timeout,
    it will wait for 5 seconds, then it will return. I also use the urllib2
    because its urlopen method is better than the original of urllib: Some
    webservers like zope, return an error page when an url isn't found; with
    urllib.urlopen, this page will be considered as a normal page. On the
    other hand, the urllib2.urlopen will raise an exception.

    Regards,
    Josef
     
    Josef Meile, Oct 27, 2004
    #2
    1. Advertising

  3. john

    Josef Meile Guest

    > This is the code I use with python greater than 2.3.3:
    >
    > import urllib2
    > import socket
    >
    > def checkUrl(url, timeout=5, SSL=0):
    > """Checks an url for a python version greater
    > than 2.3.3.
    > """
    >
    > defTimeOut=socket.getdefaulttimeout()
    > socket.setdefaulttimeout(timeout)
    > found=1
    > try:
    > urllib2.urlopen(url)
    > except (urllib2.HTTPError, urllib2.URLError,
    > socket.error, socket.sslerror):
    > found=0
    > socket.setdefaulttimeout(defTimeOut)
    > return found

    Ops, sorry, the SSL parameter isn't being used there, so you can delete it.
     
    Josef Meile, Oct 27, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. George2

    check file opened or not?

    George2, Apr 2, 2008, in forum: C Programming
    Replies:
    0
    Views:
    291
    George2
    Apr 2, 2008
  2. Michael Lee

    Cannot appendChild() to a newly opened window

    Michael Lee, Jul 2, 2004, in forum: Javascript
    Replies:
    2
    Views:
    117
    Myron Turner
    Jul 3, 2004
  3. Kuhl
    Replies:
    6
    Views:
    110
    Tim Greer
    Nov 13, 2008
  4. Daniele Baroncelli

    Closing a window by not being the url that opened it

    Daniele Baroncelli, Jan 31, 2005, in forum: Javascript
    Replies:
    1
    Views:
    97
    Dietmar Meier
    Jan 31, 2005
  5. Replies:
    1
    Views:
    97
Loading...

Share This Page