Solved

Redirect to another html page with python cgi

Posted on 2014-01-06
5
2,506 Views
Last Modified: 2014-01-20
I have a python cgi script that processes form values on mydomain.com:

if 'Submit1' in form:
        url = 'http://www.domain.com/page1.html'
        print urllib2.urlopen(url).read()
                           
elif 'Submit2' in form:
        url = 'http://www.domain.com/page2.html'
        print urllib2.urlopen(url).read() 

Open in new window


When the user selects a Submit button, the destination page is loaded.  But the url continues to show the script (http://www.domain.com/myscript.py) instead of the page url.  Bookmarking the script url will fail and it doesn't look right: depending on how the page is accessed by the user, two different urls will appear in the browser location bar for the same resource.  

I'm testing this on localhost running apache and nothing works so far. Help.
0
Comment
Question by:sara_bellum
  • 4
5 Comments
 
LVL 83

Expert Comment

by:Dave Baldwin
ID: 39758701
The methods you are using just read the content of the URL and do not redirect.  I think this http://docs.python.org/2/library/urllib2.html#httpredirecthandler-objects is what you need.
0
 

Accepted Solution

by:
sara_bellum earned 0 total points
ID: 39760789
I know. Thanks for the link, but I had tried a number of the solutions suggested on the page and the result was always a blank page with no change to the url.  

But thankfully there's a way around the problem that I share in case it's useful to someone. My script now contains:

if hd and x and y and z:
             
        title = ('%s, %s %s %s' % (hd, x, y, z))

    elif rd and x and y and z:
        
         title = ('%s, %s %s %s' % (rd, x, y, z))
        
    build_url = os.path.join(web_base, file_name + '.html')
    url = os.path.join('http://localhost', build_url)    
            
    try:
        t = Template(open(template_form).read())
        print(t.substitute(title=title, x=x, y=y, url=url))    

Open in new window


The html page now contains a form for each submit button pointing to their respective urls. The links are not entirely static, but this works because the script's template substitution passes on the values as arguments:

<form action="${url}" method="get">
    <input type="hidden" name="x" value="${x}">
    <input type="hidden" name="y" value="${y}">  
    <input type="Submit" name="Submit1" value="Return to ${y}">&nbsp;
    </form>
   
    <form action="/path-to-page2.html" method="get">
    <input type="submit" value="Home" name="Submit2" />
    </form>

Open in new window

0
 

Author Comment

by:sara_bellum
ID: 39760792
I added cgi.escape to each string substitution in the title but I'm not sure that this provides any protection because the arguments are listed in the location bar when the user clicks on a submit button.  Let me know if I should use that feature in this case thanks.
0
 

Author Comment

by:sara_bellum
ID: 39784394
cgi.escape should be used whenever user input is involved.
0
 

Author Closing Comment

by:sara_bellum
ID: 39793773
Because my solution worked.
0

Featured Post

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How To Loop - Python 19 101
linux crontab output 3 80
WordPress website error - Unable to access website. 5 70
Python to .bat or Powershell 2 30
Introduction On September 29, 2012, the Python 3.3.0 was released; nothing extremely unexpected,  yet another, better version of Python. But, if you work in Microsoft Windows, you should notice that the Python Launcher for Windows was introduced wi…
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question