Repository: Qiskit/qiskit

Issue:
ConnectionRefusedError when importing from qiskit.tools.visualization
<!-- ⚠️ If you do not respect this template, your issue will be closed -->
<!-- ⚠️ Make sure to browse the opened and closed issues -->

### Informations

Qiskit 0.6.1
Python 3.5.6
MacOS Mojave 10.14.1

### What is the current behavior?

A ConnectionRefusedError occurs as soon as you attempt to load any visualization package from qiskit.tools.visualization.

`---------------------------------------------------------------------------
ConnectionRefusedError                    Traceback (most recent call last)
<ipython-input-2-d8b2a64d996f> in <module>()
      3 
      4 # import state tomography functions
----> 5 from qiskit.tools.visualization import plot_histogram, plot_state

/anaconda3/envs/Qiskitenv/lib/python3.5/site-packages/qiskit/tools/visualization/__init__.py in <module>()
     17 
     18 if ('ipykernel' in sys.modules) and ('spyder' not in sys.modules):
---> 19     if _has_connection('https://qvisualization.mybluemix.net/', 443):
     20         from .interactive._iplot_state import iplot_state as plot_state
     21         from .interactive._iplot_histogram import iplot_histogram as \

/anaconda3/envs/Qiskitenv/lib/python3.5/site-packages/qiskit/_util.py in _has_connection(hostname, port)
    218     try:
    219         host = socket.gethostbyname(hostname)
--> 220         socket.create_connection((host, port), 2)
    221         return True
    222     except socket.gaierror:

/anaconda3/envs/Qiskitenv/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    710 
    711     if err is not None:
--> 712         raise err
    713     else:
    714         raise error("getaddrinfo returns an empty list")

/anaconda3/envs/Qiskitenv/lib/python3.5/socket.py in create_connection(address, timeout, source_address)
    701             if source_address:
    702                 sock.bind(source_address)
--> 703             sock.connect(sa)
    704             return sock
    705 

ConnectionRefusedError: [Errno 61] Connection refused`

### Steps to reproduce the problem

Load qiskit-tutorial and navigate to this notebook

https://github.com/Qiskit/qiskit-tutorial/blob/master/qiskit/basics/qiskit_visualizations.ipynb


### What is the expected behavior?

No errors should occur during import of packages. 

### Suggested solutions





Discussion:

Resolving patch:
diff --git a/qiskit/_util.py b/qiskit/_util.py
--- a/qiskit/_util.py
+++ b/qiskit/_util.py
@@ -4,7 +4,7 @@
 # This source code is licensed under the Apache License, Version 2.0 found in
 # the LICENSE.txt file in the root directory of this source tree.
 
-# pylint: disable=too-many-ancestors
+# pylint: disable=too-many-ancestors,broad-except
 
 """Common utilities for QISKit."""
 
@@ -91,6 +91,9 @@ def _has_connection(hostname, port):
     """Checks to see if internet connection exists to host
     via specified port
 
+    If any exception is raised while trying to open a socket this will return
+    false.
+
     Args:
         hostname (str): Hostname to connect to.
         port (int): Port to connect to
@@ -98,13 +101,10 @@ def _has_connection(hostname, port):
     Returns:
         bool: Has connection or not
 
-    Raises:
-        gaierror: No connection established.
     """
     try:
         host = socket.gethostbyname(hostname)
         socket.create_connection((host, port), 2)
         return True
-    except socket.gaierror:
-        pass
-    return False
+    except Exception:
+        return False


Test patch: