Solved

Redirect to another html page with python cgi

Posted on 2014-01-06
5
2,276 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
New OSQA server has a ton of fake users 4 55
Bulk Changes to WP permalinks in .htaccess - blog post only 4 78
How To Loop - Python 19 79
Cassandra Select Query 1 55
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
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 modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…

920 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now