469,929 Members | 1,748 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,929 developers. It's quick & easy.

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

Mar 18 '06 #1
3 16298
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

Mar 18 '06 #2
"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>

Mar 18 '06 #3
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)

Mar 18 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by willitfw | last post: by
reply views Thread by Ajay Abhyankar | last post: by
11 posts views Thread by Sumit Acharya | last post: by
reply views Thread by Vidds | last post: by
reply views Thread by Just D. | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.