title: Python HTTP server send JSON response
body: <h1>What im doing</h1>
  
  <p>Im trying to get my hands dirty with python and im making a very simple http server so i can send commands to my arduino via serial. Im validating the commands as i sgould and everything works as it ahould be.</p>
  
  <h1>The concept</h1>
  
  <p>Im using the HTTP server in order to recieve <code>POST</code> requests from remote computers and smartphones and execute code with my arduino via Serial.
  It has few cool features like users and user permission levels.</p>
  
  <h1>The problem</h1>
  
  <p>I would like to give feedback when a request arrives. Specificaly JSON feedback with the outcome of the execution like error, notes and success.
  Im thinking to make a dictionary in python and add whatever i want to send back the the frontend then enclode it in json and send it as a response. (Something like php's <code>json_encode()</code> which takes an array and outputs json)</p>
  
  <h1>The code</h1>
  
  <pre><code>import SimpleHTTPServer
  import SocketServer
  import logging
  import cgi
  import time
  import sys
  
  class ServerHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
  
  def do_GET(self):
      logging.warning("======= GET STARTED =======")
      logging.warning(self.headers)
      SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
  
  def do_POST(self):
      form = cgi.FieldStorage(
          fp=self.rfile,
          headers=self.headers,
          environ={'REQUEST_METHOD':'POST',
                   'CONTENT_TYPE':self.headers['Content-Type'],
                   })
  
      if self.check_auth(username, passcode):
          self.send_response(200)
          self.send_header('Content-Type', 'application/json')
          self.end_headers()
      else:
          print cur_time + "failed to login " + form.list[0].name + form.list[0].value
          self.send_response(500)
          self.send_header('Content-Type', 'application/json')
          self.end_headers()
  
  SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
  Handler = ServerHandler
  httpd = SocketServer.TCPServer(("", PORT), Handler)
  httpd.serve_forever()
  </code></pre>
  
  <p>I've tried to minimize the code so you can see the clear HTTP part.</p>
  
  <p>As you can see im sending the headers and the response type (200 for successful login and 500 for unsuccessful login)</p>
  
  <h1>Objectives i need to accomplish</h1>
  
  <ul>
  <li><p>Pass inside a dictionary the stuff i want to encode</p>
  
  <ul>
  <li>Exemple: out = {'status': 'ok', 'executed': 'yes'}</li>
  </ul></li>
  <li>Respond with json
  <ul>
  <li><a href="https://www.dropbox.com/s/uhxezdeqmvr0804/Screen%2018-33-01.png">https://www.dropbox.com/s/uhxezdeqmvr0804/Screen%2018-33-01.png</a></li>
  </ul></li>
  </ul>
  
  <p>The complet code as a gist: <a href="https://gist.github.com/FlevasGR/1170d2ea47d851bfe024">https://gist.github.com/FlevasGR/1170d2ea47d851bfe024</a></p>
  
  <p>I know this might not be the best you you've ever seens in your life but its my first time writing in <code>Python</code> :)</p>