title: Send image over http python
body: <p>I need to build a http server without using an HTTP library.</p>
  
  <p>I have the server running and an html page beeing loaded but my <code>&lt;img src="..."/&gt;</code> tags are not beeing loaded, I recive the call but cannot preset the png/JPEG in the page.</p>
  
  <h2>httpServer.py</h2>
  
  <pre><code># Define socket host and port
  SERVER_HOST = '0.0.0.0'
  SERVER_PORT = 8000
  
  # Create socket
  server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
  server_socket.bind((SERVER_HOST, SERVER_PORT))
  server_socket.listen(1)
  print('Listening on port %s ...' % SERVER_PORT)
  
  while True:
      # Wait for client connections
      client_connection, client_address = server_socket.accept()
  
      # Handle client request
      request = client_connection.recv(1024).decode()
      content = handle_request(request)
  
      # Send HTTP response
      if content:
          response = 'HTTP/1.1 200 OK\n\n'
          response += content
      else:
          response = 'HTTP/1.1 404 NOT FOUND\n\nFile Not Found'
  
      client_connection.sendall(response.encode())
      client_connection.close()
  
  # Close socket
  server_socket.close()
  </code></pre>
  
  <h3>Function where handles the call</h3>
  
  <pre><code> def handle_request(request):
      http = HttpHandler.HTTPHandler
  
      # Parse headers
      print(request)
      headers = request.split('\n')
      get_content = headers[0].split()
  
      accept = headers[6].split()
      type_content = accept[1].split('/')
  
      try:
          # Filename
          filename = get_content[1]
  
          if get_content[0] == "GET":
              content = http.get(None, get_content[1], type_content[0])
  
  
          return content
      except FileNotFoundError:
          return None
  </code></pre>
  
  <h2>class to handle the http verbs</h2>
  
  <pre><code>class HTTPHandler:
  
      def get(self, args, type):
          if args == '/':
              args = '/index.html'
              fin = open('htdocs' + args)
          if type != "image":
              fin = open('htdocs/' + args)
  
          if type == "image":
              fin = open('htdocs/' + args, 'rb')
  
          # Read file contents
          content = fin.read()
          fin.close()
          return content
  </code></pre>
  
  <p>Realize that I´m trying to make an HTTP 1.1, if you see anything out of pattern fell free to say thanks in advance.</p>