0

Referring to the following code in python 3 to download an image from url-

import urllib.request
try:
    print('entering urlretrieve')
    # If connection failed after entering the function urllib.request.urlretrieve()
    urllib.request.urlretrieve('img_url','temp_location')
    print('exited urlretrieve')
except:
    print('connection problem')
print('program ended')

Now, problem is if connection fails inside urllib.request.urlretrieve() then the program simply waits and do nothing

Output in this case is -

entering urlretrieve.

But, my requirement is to terminate printing -

connection problem

when exception occur inside this function.

Please Help!

2
  • Is your requirement "connection failed" a typo for 'connection problem'? Commented Feb 27, 2017 at 19:06
  • you were correct, I have fixed the typo. Commented Feb 27, 2017 at 21:09

1 Answer 1

1

You can use this line at the top of your code

socket.setdefaulttimeout(15)

Please check out this answer if you want to learn more

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.