title: Python Web Proxy Error: No address associated with hostname
body: <p>I am writing a really basic web proxy in Python. In it's current state all I want it to do is take the request from the client and pass it straight through to the web, no caching or anything like that yet. My problem arises when I try to accept the response from the web server. I parse the client HTTP request in order to get the hostname of the web server, I go to the second line of the request and pull out the name from there. My program crashes with the error message <strong>[Errno -5] No address associated with hostname</strong></p>
  
  <p>I've pulled some similar code from Github and ran that code, they seem to parse the first line of the HTTP request and dig the hostname out of the url which, to me, seems like an over complication but their code relatively well. Am I taking in a newline character or something like that by taking from the second line?</p>
  
  <pre><code>import sys, socket
  
  # Constants
  PORT = 8080
  MAX_BUFFER = 4096
  HOST = 'localhost'
  
  def main():
  # Start socket
      try:
          s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          s.bind((HOST, PORT))
          s.listen(0)
          print ('Proxy running on %s:%d' % (HOST, PORT))
  
      except socket.error, (message):
          print(message)
          s.close()
          sys.exit(1)
  
  # Listen for requests
      while 1:
      # conn is the socket we can send and receive to/from the client
          conn, client_addr = s.accept()
          print('Got call from client')
  
          request = conn.recv(MAX_BUFFER)
          print(str(request))
  
      # Parsing
          first_line = request.split('\n')[0]
          second_line = request.split('\n')[1]
          url = first_line.split(' ')[1]
          webserver = second_line.split(' ')[1]
          print(webserver)
  
      # Heard from client, now forward to server
          try:
              c = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
              c.connect((webserver, 80))
              c.send(request)
              print('Sent request to webserver')
  
              while 1:
                  data = c.recv(MAX_BUFFER)
                  print('Received some data')
  
              # while there is data to receive from server
                  if len(data) &gt; 0:
                      conn.send(data)
                      print('Send data to client')
  
                  else:
                      print('break')
                      break
  
              c.close()
              conn.close()
  
          except socket.error, (message):
              print(message)
              if c:
                  c.close()
  
              if conn:
                  conn.close()
  
              sys.exit(1)
  
      print('Goodbye')
  #******************End of main*********************
  
  if __name__ == '__main__':
      main()
  </code></pre>
  
  <p>Here is the extract of the code from Github that does work:</p>
  
  <pre><code># Listen for requests
      while 1:
      # conn is the socket we can send and receive to/from the client
          conn, client_addr = s.accept()
          print('Got call from client')
  
          request = conn.recv(MAX_BUFFER)
          print(str(request))
  
      # Parsing
          first_line = request.split('\n')[0]
          url = first_line.split(' ')[1]
          http_pos = url.find('://')
          if http_pos == -1:
              temp = url
          else:
              temp = url[(http_pos + 3):]
  
          port_pos = temp.find(':')
  
          webserver_pos = temp.find('/')
          if webserver_pos == -1:
              webserver_pos = len(temp)
          webserver = ''
          port = -1
  
          if port_pos == -1 or webserver_pos &lt; port_pos:
              port = 80
              webserver = temp[:webserver_pos]
  
          else:
              port = int((temp[(port_pos + 1):])[:webserver_pos - port_pos -1])
              webserver = temp[:port_pos]
  
          print(webserver)
  
      # Heard from client, now forward to server
  </code></pre>
  
  <p>Credit: <a href="https://github.com/luugiathuy/WebProxyPython/blob/master/proxy.py" rel="nofollow noreferrer">https://github.com/luugiathuy/WebProxyPython/blob/master/proxy.py</a></p>