title: Python: How to redirect from a Python CGI script to a PHP page and keep POST data
body: <p>I have an upload.php page that sends some data to a Python CGI script through a form, I then process the data in the background and I want to redirect to another php page, response_page.php, which displays info based on the processed data. I cannot send the data back to PHP and make the redirect at the same time, though.</p>
  
  <p>My code:</p>
  
  <pre><code>#!/usr/bin/env python
  
  import cgi
  import cgitb
  cgitb.enable()
  
  try:
      form = cgi.FieldStorage()
      fn = form.getvalue('picture_name')
      cat_id = form.getvalue('selected')
  except KeyError:
      print "Content-type: text/html"
      print
      print "&lt;html&gt;&lt;head&gt;"
      print "&lt;/head&gt;&lt;body&gt;error&lt;/body&gt;&lt;/html&gt;"
  else:
      ...
      # here I processed the form data and stored it in data_to_be_displayed 
      # data to be processed and displayed in the response page
      data_to_be_displayed = [1,2,3]
  
      import httplib, json, urllib
      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')
      postData = urllib.urlencode({'matches':data_to_be_displayed})
      conn.request("POST", "/response_page.php", postData, headers)
      response = conn.getresponse()
  
      if response.status == 200:
          print "Location: /response_page.php"
          print # to end the CGI response headers.
  
      conn.close()
  </code></pre>
  
  <p>I found this: <a href="https://stackoverflow.com/questions/1258428/how-to-make-python-urllib2-follow-redirect-and-keep-post-method">How to make python urllib2 follow redirect and keep post method</a> , but I don't understand how I should use the urllib2.HTTPRedirectHandlerClass in my code.</p>