0

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.

My 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 "<html><head>"
    print "</head><body>error</body></html>"
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()

I found this: How to make python urllib2 follow redirect and keep post method , but I don't understand how I should use the urllib2.HTTPRedirectHandlerClass in my code.

Community
  • 1
  • 1
AdinaP
  • 331
  • 1
  • 3
  • 10

1 Answers1

0

Why don't you post to response_page.php using liburl2?

import urllib
import urllib2
headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
data_to_be_displayed = json.dumps(data_to_be_displayed, ensure_ascii = 'False')
postData = urllib.urlencode({'matches':data_to_be_displayed})
req = urllib2.Request(url, data, headers)
response = urllib2.urlopen(req)
the_page = response.read()

For reference I've used the idea from pythons' documentation:
https://docs.python.org/2/howto/urllib2.html#headers

you might also consider using Twisted apt for higher level code:
https://twistedmatrix.com/

EDIT:

After understanding better what are your asking for, I've found this post referring that redirect 307 is EXACTLY what you want (if now I understand right):

https://softwareengineering.stackexchange.com/questions/99894/why-doesnt-http-have-post-redirect

Community
  • 1
  • 1
matan7890
  • 521
  • 3
  • 22
  • I need to do a page redirection in the browser from my script to response_page.php, this piece of code seems to only fetch a response. – AdinaP Aug 07 '14 at 13:09
  • "redirect while using post" means to fetch the new page using your post data, am I not right? Your application is not the "browser"? (Maybe I haven't got it right) – matan7890 Aug 07 '14 at 13:16
  • I am confused now. So, I have an upload.php page that sends some data to my script thorugh 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. – AdinaP Aug 07 '14 at 13:28
  • OH ok, now it's much more clear. I'll try to fix my solution for this purpose. I think you should write this in the question too. – matan7890 Aug 07 '14 at 13:34