title: How to check if urllib.urlretrieve is able to reach the host with python?
body: <p>I'm trying to collect images from imgnet linkes and save them in folder.
  Using urllib.urlretrieve , and till know it's working great.
  <strong>The main problem is when the function is trying to reach host that the DNS can't resolve.</strong>
  My link is: <a href="http://www.fuminnaosu.com/image/person/kitamura.jpg" rel="nofollow noreferrer">http://www.fuminnaosu.com/image/person/kitamura.jpg</a></p>
  
  <p>When every DNS can't resolve it (May be deleted), but still it's on the list.
  Try&amp;Except can't be using in my project for now, there is any way to pass and continue collect without using Try&amp;Except ?
  My Error is:</p>
  
  <pre><code>  File "C:\Anaconda2\lib\socket.py", line 557, in create_connection
  for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  IOError: [Errno socket error] [Errno 11001] getaddrinfo failed
  </code></pre>
  
  <p>My Code:</p>
  
  <pre><code>import cv2
  import numpy as np
  import os
  import urllib
  
  def get_images_to_dir():
      images_list_lnk = 'http://image-net.org/api/text/imagenet.synset.geturls?wnid=n09618957'
      images_url = urllib.urlopen(images_list_lnk)
      urls = images_url.read()
  
      img_typ = 'neg'
  
      if not os.path.exists(img_typ):
          os.mkdir(img_typ)
  
      images_counter = 1
  
      for url in urls.split('\n'):
              img_name = img_typ+'/'+str(images_counter)+'.jpg'
              urllib.urlretrieve(url, img_name)
              img = cv2.imread(img_name, cv2.IMREAD_GRAYSCALE)
              if (img is not None): # If connection success, but no image was found
                  img = cv2.resize(img, (100, 100))
                  cv2.imwrite(img_name, img)
                  images_counter += 1
  </code></pre>
  
  <p>Full error report:</p>
  
  <pre><code>Traceback (most recent call last):
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1664, in &lt;module&gt;
      main()
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1658, in main
      globals = debugger.run(setup['file'], None, None, is_module)
    File "C:\Program Files\JetBrains\PyCharm Community Edition 2018.1.4\helpers\pydev\pydevd.py", line 1068, in run
      pydev_imports.execfile(file, globals, locals)  # execute the script
    File "C:/Users/xdavi/PycharmProjects/Face_and_eyes_detect/get-image-by-url.py", line 40, in &lt;module&gt;
      get_images_to_dir()
    File "C:/Users/xdavi/PycharmProjects/Face_and_eyes_detect/get-image-by-url.py", line 23, in get_images_to_dir
      urllib.urlretrieve(url, img_name)
    File "C:\Anaconda2\lib\urllib.py", line 98, in urlretrieve
      return opener.retrieve(url, filename, reporthook, data)
    File "C:\Anaconda2\lib\urllib.py", line 245, in retrieve
      fp = self.open(url, data)
    File "C:\Anaconda2\lib\urllib.py", line 213, in open
      return getattr(self, name)(url)
    File "C:\Anaconda2\lib\urllib.py", line 350, in open_http
      h.endheaders(data)
    File "C:\Anaconda2\lib\httplib.py", line 1038, in endheaders
      self._send_output(message_body)
    File "C:\Anaconda2\lib\httplib.py", line 882, in _send_output
      self.send(msg)
    File "C:\Anaconda2\lib\httplib.py", line 844, in send
      self.connect()
    File "C:\Anaconda2\lib\httplib.py", line 821, in connect
      self.timeout, self.source_address)
    File "C:\Anaconda2\lib\socket.py", line 557, in create_connection
      for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  IOError: [Errno socket error] [Errno 11001] getaddrinfo failed
  </code></pre>