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

Fredrik Lundh fredrik at pythonware.com
Sat Mar 18 13:41:19 CET 2006


"JuHui" wrote:

> I wrote a script to get 100 pages from a server.
> like below:
>
> 1:import httplib
> 2:conns = httplib.HTTPConnection("www.mytest.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')

(given the code you quoted, a NameError would be more likely...)

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

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

    http://docs.python.org/tut/node10.html

</F>






More information about the Python-list mailing list