how to check if URL cannot be opened

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • john

    #1

    how to check if URL cannot be opened

    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
  • Josef Meile

    #2
    Re: how to check if URL cannot be opened

    Hi John,
    [color=blue]
    > 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?[/color]

    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=sock et.getdefaultti meout()
    socket.setdefau lttimeout(timeo ut)
    found=1
    try:
    urllib2.urlopen (url)
    except (urllib2.HTTPEr ror, urllib2.URLErro r,
    socket.error, socket.sslerror ):
    found=0
    socket.setdefau lttimeout(defTi meOut)
    return found

    Please note that I use the setdefaulttimeo ut 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

    Comment

    • Josef Meile

      #3
      Re: how to check if URL cannot be opened

      > This is the code I use with python greater than 2.3.3:[color=blue]
      >
      > import urllib2
      > import socket
      >
      > def checkUrl(url, timeout=5, SSL=0):
      > """Checks an url for a python version greater
      > than 2.3.3.
      > """
      >
      > defTimeOut=sock et.getdefaultti meout()
      > socket.setdefau lttimeout(timeo ut)
      > found=1
      > try:
      > urllib2.urlopen (url)
      > except (urllib2.HTTPEr ror, urllib2.URLErro r,
      > socket.error, socket.sslerror ):
      > found=0
      > socket.setdefau lttimeout(defTi meOut)
      > return found[/color]
      Ops, sorry, the SSL parameter isn't being used there, so you can delete it.

      Comment

      Working...