How to catch socket timeout?

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

    How to catch socket timeout?

    Hi,

    I'm using Python 2.3s timeout sockets and have code like this to read a page
    from web:

    request = ...
    self.page = urllib2.urlopen (request)

    and later:

    try:
    self.data = self.page.read( )
    except socket.error,e: ...
    except socket.timeout: ...
    except timeout: ...

    but none of these excepts catches the the timeout while reading the data. I
    still get the following exception, which I cannot handle:

    ....
    File "F:\CrawlingFra mework\Rules\To ols\__init__.py ", line 91, in __init__
    self.data = self.page.read( )
    File "C:\Python23\li b\socket.py", line 283, in read
    data = self._sock.recv (recv_size)
    timeout: timed out

    Any hint on how to handle this exception or what's going wrong?

    regards,
    Achim


  • Peter Otten

    #2
    Re: How to catch socket timeout?

    Achim Domma wrote:
    [color=blue]
    > I'm using Python 2.3s timeout sockets and have code like this to read a
    > page from web:
    >
    > request = ...
    > self.page = urllib2.urlopen (request)
    >
    > and later:
    >
    > try:
    > self.data = self.page.read( )
    > except socket.error,e: ...
    > except socket.timeout: ...
    > except timeout: ...
    >
    > but none of these excepts catches the the timeout while reading the data.
    > I still get the following exception, which I cannot handle:[/color]

    socket.timeout is a subclass of socket.error, so the timeout exception
    should be caught by the first except clause.

    However, I could reproduce your uncaught exception with the following
    minimalist code:

    from urllib2 import urlopen
    import socket

    slowurl = "http://127.0.0.1/timeout?delay=1 00"
    socket.setdefau lttimeout(1)
    data = urlopen(slowurl )

    try:
    data.read()
    except: # should catch ANY exception
    print "Timeout raised and caught" # this never shows

    So it seems there is *no* way to catch the error.
    I think you should file a bug report.

    Peter

    Comment

    • Achim Domma

      #3
      Re: How to catch socket timeout?

      "Peter Otten" <__peter__@web. de> wrote in message
      news:bkk1ea$7aj $04$1@news.t-online.com...
      [color=blue]
      > So it seems there is *no* way to catch the error.
      > I think you should file a bug report.[/color]

      Where/How do I do that?

      Achim


      Comment

      • Alan Kennedy

        #4
        Re: How to catch socket timeout?

        Peter Otten wrote:[color=blue]
        > socket.timeout is a subclass of socket.error, so the timeout exception
        > should be caught by the first except clause.[/color]
        [color=blue]
        > So it seems there is *no* way to catch the error.
        > I think you should file a bug report.[/color]

        Hmmm.

        What is wrong with the following code? It seems to do what you need:

        #============== =============== =============== ===========
        from urllib2 import urlopen
        import socket
        import sys

        slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wai t=10"
        socket.setdefau lttimeout(1)

        try:
        data = urlopen(slowurl )
        data.read()
        except socket.error:
        errno, errstr = sys.exc_info()[:2]
        if errno == socket.timeout:
        print "There was a timeout"
        else:
        print "There was some other socket error"
        #============== =============== =============== ==============

        regards,

        --
        alan kennedy
        -----------------------------------------------------
        check http headers here: http://xhaus.com/headers
        email alan: http://xhaus.com/mailto/alan

        Comment

        • Peter Otten

          #5
          Re: How to catch socket timeout?

          Achim Domma wrote:
          [color=blue][color=green]
          >> So it seems there is *no* way to catch the error.
          >> I think you should file a bug report.[/color]
          >
          > Where/How do I do that?[/color]

          http://www.python.org/dev has an outside link to Bug Tracker leading to a
          Sourceforge page where you can submit a short description of the bug.

          Have a look at the list of bugs both to see if your bug has already been
          submitted by others as well as for example submissions.


          Peter

          Comment

          • Peter Otten

            #6
            Re: How to catch socket timeout?

            Alan Kennedy wrote:
            [color=blue]
            > Hmmm.
            >
            > What is wrong with the following code? It seems to do what you need:
            >
            > #============== =============== =============== ===========
            > from urllib2 import urlopen
            > import socket
            > import sys
            >
            > slowurl = "http://127.0.0.1/cgi-bin/longWait.py?wai t=10"
            > socket.setdefau lttimeout(1)
            >
            > try:
            > data = urlopen(slowurl )
            > data.read()
            > except socket.error:
            > errno, errstr = sys.exc_info()[:2]
            > if errno == socket.timeout:
            > print "There was a timeout"
            > else:
            > print "There was some other socket error"
            > #============== =============== =============== ==============[/color]

            You are right. I did not read the traceback carefully.

            Peter


            Comment

            • Bob Halley

              #7
              Re: How to catch socket timeout?

              "Achim Domma" <domma@procoder s.net> writes:
              [color=blue]
              > I'm using Python 2.3s timeout sockets and have code like this to read a page
              > from web:
              >
              > request = ...
              > self.page = urllib2.urlopen (request)
              >
              > and later:
              >
              > try:
              > self.data = self.page.read( )
              > except socket.error,e: ...
              > except socket.timeout: ...
              > except timeout: ...[/color]

              As another poster pointed out, socket.timeout is a subclass of
              socket.error. (This was so you could write code that treated all
              socket errors alike if you wanted timeouts but didn't need to deal
              with them separately.)

              Section 7.4 of the Language Reference says:

              [...] When an exception occurs in the try suite, a search for
              an exception handler is started. This search inspects the
              except clauses in turn until one is found that matches the
              exception. [...]

              So, all you need to do is put the socket.timeout except clause before
              any socket.error clause:

              try:
              self.data = self.page.read( )
              except socket.timeout: ...
              except socket.error,e: ...

              /Bob

              Comment

              • Achim Domma

                #8
                Re: How to catch socket timeout?

                "Bob Halley" <halley@play-bow.org> wrote in message
                news:mailman.10 64178330.17024. python-list@python.org ...[color=blue]
                > So, all you need to do is put the socket.timeout except clause before
                > any socket.error clause:
                >
                > try:
                > self.data = self.page.read( )
                > except socket.timeout: ...
                > except socket.error,e: ...[/color]

                Thanks, that works fine, but I don't understand why the exception was not
                catched in my case. If I write it like this

                try:
                self.data = self.page.read( )
                except socket.error,e: ...
                except socket.timeout: ...

                the exception should be catched by the socket.error handler. Or am I wrong?
                In my case it was not catched at all. Very mysterious from my point of view,
                but it works now.

                Achim


                Comment

                • Peter Otten

                  #9
                  Re: How to catch socket timeout?

                  Achim Domma wrote:
                  [color=blue]
                  > "Bob Halley" <halley@play-bow.org> wrote in message
                  > news:mailman.10 64178330.17024. python-list@python.org ...[color=green]
                  >> So, all you need to do is put the socket.timeout except clause before
                  >> any socket.error clause:
                  >>
                  >> try:
                  >> self.data = self.page.read( )
                  >> except socket.timeout: ...
                  >> except socket.error,e: ...[/color]
                  >
                  > Thanks, that works fine, but I don't understand why the exception was not
                  > catched in my case. If I write it like this
                  >
                  > try:
                  > self.data = self.page.read( )
                  > except socket.error,e: ...
                  > except socket.timeout: ...
                  >
                  > the exception should be catched by the socket.error handler. Or am I
                  > wrong? In my case it was not catched at all. Very mysterious from my point
                  > of view, but it works now.
                  >
                  > Achim[/color]

                  Achim, both you and me got it wrong the first time. The important part is to
                  put both urlopen() and page.read() into the try clause, as Alan Kennedy has
                  already shown. Both statements can throw a timeout exception, so it's sheer
                  luck that it worked this time.
                  And yes, if you put

                  except socket.error:

                  before

                  except socket.timeout:

                  the latter will never be executed. General structure:


                  from urllib2 import urlopen
                  import socket

                  slowurl = "http://127.0.0.1/timeout?delay=1 00"
                  socket.setdefau lttimeout(1)

                  try:
                  data = urlopen(slowurl )
                  data.read()
                  except socket.timeout:
                  print "Timeout raised and caught"

                  Peter

                  Comment

                  • Paul

                    #10
                    Re: How to catch socket timeout?

                    What's the best way to do this under Python 2.1?
                    I believe socket.timeout was a 2.3 feature.
                    Wrap it in threads?

                    I'd like to do something similar for the Gibraltar
                    Linux firewall CD, but it only comes with Python 2.1.

                    Many thanks in advance.

                    -- Paul

                    Comment

                    • Michael Hudson

                      #11
                      Re: How to catch socket timeout?

                      Paul <paul@oz.net> writes:
                      [color=blue]
                      > What's the best way to do this under Python 2.1?
                      > I believe socket.timeout was a 2.3 feature.
                      > Wrap it in threads?[/color]

                      There's Tim O'Malley's timeoutsocket.p y (google for it), which was the
                      inspiration for the feature in 2.3 (though I don't think any code from
                      there actually survived).

                      Cheers,
                      mwh

                      --
                      31. Simplicity does not precede complexity, but follows it.
                      -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html

                      Comment

                      • Michael Hudson

                        #12
                        Re: How to catch socket timeout?

                        Paul <paul@oz.net> writes:
                        [color=blue]
                        > What's the best way to do this under Python 2.1?
                        > I believe socket.timeout was a 2.3 feature.
                        > Wrap it in threads?[/color]

                        There's Tim O'Malley's timeoutsocket.p y (google for it), which was the
                        inspiration for the feature in 2.3 (though I don't think any code from
                        there actually survived).

                        Cheers,
                        mwh

                        --
                        31. Simplicity does not precede complexity, but follows it.
                        -- Alan Perlis, http://www.cs.yale.edu/homes/perlis-alan/quotes.html

                        Comment

                        • Ed

                          #13
                          Re: How to catch socket timeout?

                          Have a look at:



                          Paul wrote:
                          [color=blue]
                          > What's the best way to do this under Python 2.1?
                          > I believe socket.timeout was a 2.3 feature.
                          > Wrap it in threads?
                          >
                          > I'd like to do something similar for the Gibraltar
                          > Linux firewall CD, but it only comes with Python 2.1.
                          >
                          > Many thanks in advance.
                          >
                          > -- Paul[/color]

                          Comment

                          • Ed

                            #14
                            Re: How to catch socket timeout?

                            Have a look at:



                            Paul wrote:
                            [color=blue]
                            > What's the best way to do this under Python 2.1?
                            > I believe socket.timeout was a 2.3 feature.
                            > Wrap it in threads?
                            >
                            > I'd like to do something similar for the Gibraltar
                            > Linux firewall CD, but it only comes with Python 2.1.
                            >
                            > Many thanks in advance.
                            >
                            > -- Paul[/color]

                            Comment

                            • Mickel Grönroos

                              #15
                              Re: How to catch socket timeout?

                              On Wed, 24 Sep 2003, Michael Hudson wrote:
                              [color=blue]
                              > Paul <paul@oz.net> writes:
                              >[color=green]
                              > > What's the best way to do this under Python 2.1?
                              > > I believe socket.timeout was a 2.3 feature.
                              > > Wrap it in threads?[/color]
                              >
                              > There's Tim O'Malley's timeoutsocket.p y (google for it), which was the
                              > inspiration for the feature in 2.3 (though I don't think any code from
                              > there actually survived).[/color]

                              I can't get the timeoutsocket module to work on my Redhat Linux! (The
                              timeout works fine on Windows 2000.) I'm running Python 2.2.2.

                              Here's the test code:

                              import timeoutsocket
                              timeoutsocket.s etDefaultSocket Timeout(1)
                              s = timeoutsocket.s ocket(timeoutso cket.AF_INET, timeoutsocket.S OCK_STREAM)
                              s.connect(("www .google.com", 80))
                              s.close()

                              Any ideas?

                              /Mickel G.

                              Comment

                              Working...