Python 3.x 在python 3中,urllib.request.urlretrieve在失败时挂起

Python 3.x 在python 3中,urllib.request.urlretrieve在失败时挂起,python-3.x,urllib,Python 3.x,Urllib,参考Python3中的以下代码从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 urlr

参考Python3中的以下代码从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')
现在,问题是,如果urllib.request.urlretrieve()内部的连接失败,那么程序只需等待,什么也不做

本例中的输出是-

输入urlretrieve

但是,我的要求是终止打印-

连接问题

当此函数内部发生异常时


请帮忙

您可以在代码顶部使用这一行

socket.setdefaulttimeout(15)

如果您想了解更多信息,请查看

您的要求“连接失败”是“连接问题”的拼写错误吗?您是正确的,我已修复了拼写错误。