How to redirect a page to another page in Python 3 cgi

10,753
#! /usr/bin/python3
import cgi
form_data=cgi.FieldStorage()
print('Content-type:text/html\n\n')
if 'username' not in form_data or 'password' not in form_data:
   redirectURL = "http://localhost:8081/"
   print('<html>')
   print('  <head>')
   print('    <meta http-equiv="refresh" content="0;url='+str(redirectURL)+'" />') 
   print('  </head>')
   print('</html>')
else:
   print(form_data['username'].value)
   print(form_data['password'].value)

I did lot of R&D and finally found this and would be the perfect solution so far.If you feel not appropriate ,please correct it.Thank you

Share:
10,753
Prad
Author by

Prad

I love programming. I know python, DevOps, automation, cloud programming and travelling is my hobby. I would love to play the violin and love to learn Network security.

Updated on June 04, 2022

Comments

  • Prad
    Prad almost 2 years

    I am studying Python recently and a beginner. I am unable set a logic to as, if the post data does not qualify, post page will be redirected to origin page.

    After some research I found this how to redirect one page to another, but still having confusion regarding redirecting page.

    Here is my html code :

    <form action="generate_timing_data.py" method="POST"> <p>Select an athlete from the list to work with: </p><input type="radio" name="which_athlete" value="Mikey McManus"> Mikey McManus<br /> <input type="radio" name="which_athlete" value="Julie Jones"> Julie Jones<br /> <input type="radio" name="which_athlete" value="James Lee"> James Lee<br /><input type="radio" name="which_athlete" value="Sarah Sweeney"> Sarah Sweeney<br /><p> </p><input type=submit value="Select"> </form> And this is my python code :

    import cgi
    import athletemodel
    import yate
    form_data = cgi.FieldStorage()
    if form_data['which_athlete'].value != '':
       //calculation stuff
    else :
       //redirect stuff
    

    In here, where to put header code and all the staff in form and how to redirect to origin page from the post page, how to set all the things? please help me to understand how the process flow?

    • Daniel Roseman
      Daniel Roseman over 6 years
      It's hard not to downvote when you've given such limited details. Why have you only shown the html? All the stuff you mention happens in the Python code.
    • Prad
      Prad over 6 years
      sure wait ,I am giving you py codes
    • bruno desthuilliers
      bruno desthuilliers over 6 years
      wrt to your "how to redirect one page to another" link: it has very few to do with your problem (the question you link to is about HTTP client code, not about HTTP server code which is what you're doing). Also if you hope to do any web programming, you need to understand the HTTP protocol (both the request and response parts).
    • Prad
      Prad over 6 years
      @burno desthuilliers ,Is this really required to understand HTTP protocol for sending form post data to a CGI py page ?
    • Prad
      Prad over 6 years
      Please anyone ,need little help .