title: Python CGI: How to redirect to another page after processing POST data
body: <p>I'm trying to write a Python script, in pyscripts/find_match.py that will process data received from a POST in an upload.php page, send it to connect.php and then redirect to another PHP page, response.php that will display information based on my processed data and that has an </p>
  
  <pre><code>&lt;?php include '/connect_database.php';?&gt;
  </code></pre>
  
  <p>line. </p>
  
  <p>Up till now, I am able to get the POST info, process it and send it through a JSON to connect.php, but I am not able to make find_match.py redirect to response.php. My code looks like this:</p>
  
  <p>In pyscripts/find_match.py:</p>
  
  <pre><code>print "Content-type: text/html"
  print
  print "&lt;html&gt;&lt;head&gt;"
  print "&lt;/head&gt;&lt;body&gt;"
  import cgi
  import cgitb
  cgitb.enable()
  
  try:
      form = cgi.FieldStorage()
      fn = form.getvalue('picture_name')
      cat_id = form.getvalue('selected')
  except KeyError:
      print 'error'
  else:
      # code to process data here
  
      data_to_be_displayed = # data to be used in connect.php; it's an array of ids
  
      import httplib, json, urllib2
      headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
      conn = httplib.HTTPConnection('192.168.56.101:80')
      #converting list to a json stream
      data_to_be_displayed = json.dumps(data_to_be_displayed, ensure_ascii = 'False')
      conn.request("POST", "/connect_database.php", data_to_be_displayed, headers)
      response = conn.getresponse()
      text = response.read()
      # print response.status, text
      conn.close()
  
      # WHAT I WANT TOT DO HERE
      if response.status == 200:
          redirect('/response.php')
  print "&lt;/body&gt;&lt;/html&gt;"
  </code></pre>
  
  <p>In response.php:</p>
  
  <pre><code>&lt;!DOCTYPE html&gt;
  &lt;html&gt;
  
  &lt;head&gt;
      &lt;title&gt;Response Page&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
          &lt;div id="main"&gt;
              &lt;?php include '/connect_database.php';?&gt;
          &lt;/div&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>I have found some info about the urllib.HTTPRequestHandler class and the Location header, but I don't know how to use them.
  Tried using</p>
  
  <pre><code>&lt;meta http-equiv="refresh" content="0;url=%s" /&gt;
  </code></pre>
  
  <p>in the HEAD tag, but it doesn't work.
  Please help.</p>