how to deal with socket.error: (10060, 'Operation timed out')

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

    #1

    how to deal with socket.error: (10060, 'Operation timed out')

    I wrote a script to get 100 pages from a server.
    like below:

    1:import httplib
    2:conns = httplib.HTTPCon nection("www.my test.com")
    3:conn.request( "GET", "/")

    sometimes a socket error was raised.

    File "D:\usr\bin\lib \httplib.py", line 627, in connect
    raise socket.error, msg
    socket.error: (10060, 'Operation timed out')

    how to catch this kind of error then retry the "GET" operation?

    btw:
    If I want to get many page on same server, which lib is the best
    choice? I only know httplib,urllib and urllib2.
    thanks a lot.

  • gregarican

    #2
    Re: how to deal with socket.error: (10060, 'Operation timed out')

    JuHui wrote:

    I wrote a script to get 100 pages from a server.
    like below:
    [color=blue]
    > 1:import httplib
    > 2:conns = httplib.HTTPCon nection("www.my test.com")
    > 3:conn.request( "GET", "/")
    >
    >
    > sometimes a socket error was raised.
    >[/color]
    [color=blue]
    > File "D:\usr\bin\lib \httplib.py", line 627, in connect
    > raise socket.error, msg
    > socket.error: (10060, 'Operation timed out')
    >
    >
    > how to catch this kind of error then retry the "GET" operation?[/color]

    You can use this below if you only want one retry ---

    import httplib
    conns = httplib.HTTPCon nection("www.my test.com")

    try:
    conn.request("G ET", "/")
    except:
    try:
    conns.request(" GET", "/")
    except:
    print "I bombed out!"

    # blah blah blah

    Comment

    • Fredrik Lundh

      #3
      Re: how to deal with socket.error: (10060, 'Operation timed out')

      "JuHui" wrote:
      [color=blue]
      > I wrote a script to get 100 pages from a server.
      > like below:
      >
      > 1:import httplib
      > 2:conns = httplib.HTTPCon nection("www.my test.com")
      > 3:conn.request( "GET", "/")[/color]
      [color=blue]
      > sometimes a socket error was raised.
      >
      > File "D:\usr\bin\lib \httplib.py", line 627, in connect
      > raise socket.error, msg
      > socket.error: (10060, 'Operation timed out')[/color]

      (given the code you quoted, a NameError would be more likely...)
      [color=blue]
      > how to catch this kind of error then retry the "GET" operation?[/color]

      the same way as you'd catch any other kind of error in Python:

      try:
      <operation>
      except <exception>:
      <error handler>

      if you want to repeat an operation until it succeeds, use a loop:

      while 1:
      try:
      <operation>
      except <exception>:
      <error handler>
      else:
      break # success

      for details, see



      </F>



      Comment

      • JuHui

        #4
        Re: how to deal with socket.error: (10060, 'Operation timed out')

        thanks!
        I try the "try" before, but can't catch the socket error. so strange,
        maybe my code was error.
        I wrote a retry code block as below.

        import httplib,time

        while 1:
        try:
        conn = httplib.HTTPCon nection("www.te st.com")
        conn.request("G ET", "/")
        print "success"
        break
        except:
        print "error"
        time.sleep(2)

        Comment

        Working...