title: Accessing python server (web server) using ngrok
body: <p>I have a python network server code. </p>
  
  <pre class="lang-py prettyprint-override"><code>import socket
  
  HOST, PORT = '', 5000
  listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  listen_socket.bind((HOST, PORT))
  listen_socket.listen(1)
  
  print('Serving HTTP on port %s ...' % PORT)
  while True:
      client_connection, client_address = listen_socket.accept()
      request = client_connection.recv(1024)
      print(request)
      http_response = """\
  HTTP/1.1 200 OK
  Content-Type: text/html; charset=utf-8
  
  &lt;H1&gt;Hello, World!&lt;/H1&gt;
  """
      client_connection.sendall(http_response.encode())
      client_connection.close()
  </code></pre>
  
  <p>I have a client code that accesses the server. </p>
  
  <pre class="lang-py prettyprint-override"><code>import socket
  
  HOST = '127.0.0.1'
  PORT = 5000        # The port used by the server
  
  try: 
      s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
      print "Socket successfully created"
      s.connect((HOST, PORT))
      s.sendall('GET /')
      data = s.recv(1000)
      print('Received', repr(data))
      s.close
  except socket.error as err: 
      print "socket creation failed with error %s" %(err)
  </code></pre>
  
  <p>It works fine with the expected output when I executed the server and client. </p>
  
  <pre><code>Socket successfully created
  ('Received', "'HTTP/1.1 200 OK\\nContent-Type: text/html; charset=utf-8\\n\\n&lt;H1&gt;Hello, World!&lt;/H1&gt;\\n'")
  </code></pre>
  
  <p>Then, I tried to execute the python server using <code>ngrok.</code> </p>
  
  <pre><code>Session Status                online
  Account                       ...
  Version                       2.3.34
  Region                        United States (us)
  Web Interface                 http://127.0.0.1:4040
  Forwarding                    http://d2fccf7f.ngrok.io -&gt; http://localhost:5000
  </code></pre>
  
  <p>Using <code>curl</code>, I could access the webserver with ngrok.</p>
  
  <pre><code>&gt; curl http://d2fccf7f.ngrok.io 
  &lt;H1&gt;Hello, World!&lt;/H1&gt;
  </code></pre>
  
  <p>However, when I tried to use the same client code with minor modifications, the server doesn't seem to respond. </p>
  
  <pre class="lang-py prettyprint-override"><code>import socket
  ip = socket.gethostbyname('d2fccf7f.ngrok.io')
  print(ip)
  HOST = ip
  PORT = 5000 
  # the rest of the code is the same
  </code></pre>
  
  <p>I changed the PORT to 80 or 8080, but I had same results.</p>
  
  <p>What might be wrong? </p>