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

J

JuHui

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')

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.
 
F

Fredrik Lundh

JuHui said:
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>
 
G

gregarican

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')


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

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

import httplib
conns = httplib.HTTPConnection("www.mytest.com")

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

# blah blah blah
 
J

JuHui

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.HTTPConnection("www.test.com")
conn.request("GET", "/")
print "success"
break
except:
print "error"
time.sleep(2)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top