title: HTTPServer: "Cannot assign requested address" on localhost vs 127.0.0.1
body: <p>With the below Python 3 code:</p>
  
  <pre><code>from http.server import HTTPServer, SimpleHTTPRequestHandler
  s = HTTPServer(('localhost', 0), SimpleHTTPRequestHandler)
  </code></pre>
  
  <p>I get the following error:</p>
  
  <pre><code>Traceback (most recent call last):
    File "repro.py", line 2, in &lt;module&gt;
      s = HTTPServer(('localhost', 0), SimpleHTTPRequestHandler)
    File "/usr/lib/python3.6/socketserver.py", line 453, in __init__
      self.server_bind()
    File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
      socketserver.TCPServer.server_bind(self)
    File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
      self.socket.bind(self.server_address)
  OSError: [Errno 99] Cannot assign requested address
  </code></pre>
  
  <p>I've noticed that 3 things will prevent this from happening:</p>
  
  <ol>
  <li>Disabling WiFi.</li>
  <li>Switching to a different WiFi network (from my home router to tethering on my phone).</li>
  <li>Changing <code>'localhost'</code> in the code to <code>'127.0.0.1'</code>.</li>
  </ol>
  
  <p>So my question is, what's going wrong here? Why does this code fail when it does?</p>
  
  <p>P.S. <a href="https://gist.github.com/kerrickstaley/b1fe713144f8506cdf7ab4b6bbf10baf" rel="nofollow noreferrer">Here is ip a's output when connected to my home router</a>, and <a href="https://gist.github.com/kerrickstaley/9c16dcac4d7cde5313c3ce46c81da2ba" rel="nofollow noreferrer">here's its output when tethering</a>.</p>