title: How can be vanilla python used with AJAX?
body: <p>I am working on a project which requires to show me smart search suggestions much like google search but for my own database.
  I researched the web a bit and found AJAX. And being a python devotee , I didnt like the way PHP looks and feels.
  But the problem is 99% of the documentations , examples and references you find on the web for python CGI programming is based on a web framework.
  Well I would loved to use a web framework but my buddy who is also working on the website is using PHP and looking in the documentation to deploy and Python web app with some other things just drives me crazy.
  So I am using vanilla python with the inbuilt cgi module.
  I dont know both my AJAX script and python were working well but then I changed the search algorithm a little bit and now its not working.
  I tried using a local variable to test it and guess what it works but it doesnt work from my web page.</p>
  
  <p>Here's my webpage:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;title&gt;Search&lt;/title&gt;
  &lt;script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"&gt;&lt;/script&gt;
  &lt;!-- Only When there is no internet connection in my house/anywhere --&gt;
  &lt;!-- &lt;script type="text/javascript" src = 'js/jquery3.js'&gt;&lt;/script&gt; --&gt;
  &lt;style type="text/css"&gt;
      body{
          margin:0px;
          font-family:arial,sans-serif;
      }
      /* Div Ids Begin */
      #parentHeader{
          background-color:#6B1C0D;
          width:100%;
      }
      #childHeader{
          padding-top:20px;
          padding-bottom:30px;
          margin-left:22%;
      }
      /* Div Ids End */
      /* The text field */
      #searchBox{
          width:70%;
          resize:none;
          font-size:15px;
      }
      /* For the box with suggestions */
      #ajaxResults{
          background: #ffffff;
          padding: 5px 10px;
          max-height: 400px;
          overflow: auto;
          position: absolute;
          z-index: 99;
          border: 1px solid #A9A9A9;
          border-width: 0 1px 1px 1px;
      }
  &lt;/style&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;div id="parentHeader"&gt;
  &lt;div id='childHeader'&gt;
      &lt;input type="textbox" id="searchBox" name="searchQuery" placeholder="Search Here..."&gt;&lt;br /&gt;
      &lt;div id="ajaxResults"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;!-- JavaScript Begins --&gt;
  &lt;script type="text/javascript"&gt;
  // Make the ResultBox Hidden
  $('#ajaxResults').hide();
  // On Every Key Press
  $('#searchBox').keyup(function() {
  // Value Obtained
  val = $('#searchBox').val();
  // Check Whether its empty or not
  if(val.length == 0){
      $('#ajaxResults').hide();
      $('#ajaxResults').html();
  }else{
      // Performs flawlessly , isnt it?
      // Afterall it took me hours to find out how can I use Vanilla Python with AJAX
      // Just if I forget my Hardwork
      $('#ajaxResults').show();
      $('#ajaxResults').load('/cgi-bin/suggest.py',{'q':val});
  }
  });
  &lt;/script&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>And my python script:</p>
  
  <pre><code>#!/usr/bin/python3
  
  
  #CGI Thingies Begin
  
  import cgi
  import cgitb
  
  cgitb.enable()
  
  
  form = cgi.FieldStorage()
  reqVal = form.getvalue('q')
  
  #CGI Thingies End
  
  # reqVal = "Maths T"
  
  
  text_raw = open("suggestions.txt","r") ##TO_DO : Change input from text file to Database
  text = text_raw.read()
  suggestions = text.split('\n')
  
  
  retStr = "" #String which is to be returned
  
  queryLength = len(reqVal)
  
  retLen = len(reqVal.split(' '))
  
  # pick a suggestion from suggestions
  # get the reqVal and extract the word which is going to be typed
  # take the whole string with the suggestion but only return upto the word
  # which is going to be typed but not completly written
  # if the user is going to type a word show the available suggestion upto the next word
  # The principle loop which does the sorting
  
  
  
  for suggestion in suggestions:
      temp = suggestion
      suggestion = suggestion[:queryLength]
      if(suggestion == reqVal):
          tempArr = temp.split(' ')
          for i in range(0,retLen,1):
              retStr = retStr + tempArr[i] + " "
          retStr = retStr + " "
  
  # Function to sort out Duplis
  
  def sortOut(checkVar):
      seen = set()
      seenAdd = seen.add
      return [x for x in checkVar if not (x in seen or seenAdd(x))]
      pass
  
  checkStr = retStr.split(' ')
  
  retVar = sortOut(checkStr)
  retStr = ""
  
  for var in retVar:
      retStr = retStr +var+ '&lt;br&gt;'
  
  retStr = retStr[:-4] #To remove that extra &lt;br&gt;
  
  #Prints
  
  print ("Content-type:text/html\r\n\r\n")
  print ('&lt;html&gt;')
  print ('&lt;body&gt;')
  print (retStr)
  print ('&lt;/body&gt;')
  print ('&lt;/html&gt;')
  </code></pre>
  
  <p>Thanks</p>