[Tutor] Running a python script from another python script

Kent Johnson kent37 at tds.net
Sun Mar 27 14:23:45 CEST 2005


M.Sinan ORUN wrote:
> Hello,
> 
> I am a newbee in python and trying to make a small script for my school 
> project . I try to run  a python cgi script from another phyton script 
> as a result of an action . What is the neccesary command for this action 
> and is there a special class have to be imported for this command.

One way to do this is to put a redirect in the response to the login page. This will make the 
browser go to the second page. This page tells how:
http://www.instant-web-site-tools.com/html-redirect.html

Note that you are not actually password protecting your second page; there is nothing to prevent a 
user from navigating directly to the second page. For real protection the second page needs to check 
if the user is logged in using some kind of session mechanism, and redirect to the login page if the 
user is not authenticated. There are many Python web frameworks that can help with this.

BTW you should try using triple-quoted strings for your blocks of HTML, then you can just type 
literally what you want, no embedded 'print' or '\n' or escaped quotes, e.g.

    print """\
<HTML>
<HEAD>
     <TITLE>Menu List</TITLE>
</HEAD>
<BODY BGCOLOR = white>
     <H3>Please choice a operation.</H3>
<FORM METHOD = post ACTION = "index1.cgi">
"""
etc.

Kent



> 
> below i am giving so for what i have writen and what am i trying to do. 
> Thant you for your help
> 
> this is my first script for a basic log in action. i want to run the 
> second script from this script as a result of login action
> 
> 
> ##### index.cgi##### login script
> #!/usr/bin/python
> import cgi
> print "Content-Type: text/html\n\n"
> 
> # Define function to generate HTML form.
> def generate_form():
>    print "<HTML>\n"
>    print "<HEAD>\n"
>    print "\t<TITLE>Info Form</TITLE>\n"
>    print "</HEAD>\n"
>    print "<BODY BGCOLOR = white>\n"
>    print "\t<H3>Please, enter your UserName and Password.</H3>\n"
>    print "\t<TABLE BORDER = 0>\n"
>    print "\t\t<FORM METHOD = post ACTION = \"index.cgi\">\n"
>    print "\t\t<TR><TH>UserName:</TH><TD><INPUT type = text name = 
> \"UserName\"></TD><TR>\n"
>    print "\t\t<TR><TH>Password:</TH><TD><INPUT type = password name = \ 
> \"Password\"></TD></TR>\n"
>    print "\t</TABLE>\n"
>    print "\t<INPUT TYPE = hidden NAME = \"action\" VALUE = \"display\">\n"
>    print "\t<INPUT TYPE = submit VALUE = \"Enter\">\n"
>    print "\t</FORM>\n"
>    print "</BODY>\n"
>    print "</HTML>\n"
> 
> # Define main function.
> def main():
> 
> form = cgi.FieldStorage()
> if (form.has_key("action") and form.has_key("UserName") and 
> form.has_key("Password")):
>  if (form["UserName"].value == "sinan" and form["Password"].value== "666"):
>   print "Welcome Boss !!"
>   #### here i need a command to move on my second script 
> (index1.cgi)#########
>  
>  else:
>   print " You are not recognized !!"
>   generate_form()
> else:
>  print " You are not recognized !!"
>  generate_form()
> main()
> 
> 
> 
> 
> ########## index1.cgi ############# this is my second script whish has 
> to be runned from the first script
> #### as a result of login action#####
> #!/usr/bin/python
> import cgi
> print "Content-Type: text/html\n\n"
> 
> # Define function to generate HTML form.
> 
> #menulist
> def menulist():
>    print "<HTML>\n"
>    print "<HEAD>\n"
>    print "\t<TITLE>Menu List</TITLE>\n"
>    print "</HEAD>\n"
>    print "<BODY BGCOLOR = white>\n"
>    print "\t<H3>Please choice a operation.</H3>\n"
>    print "<FORM METHOD = post ACTION = \"index1.cgi\">\n"
>    print "<select name=\"operations\">"
>    print "<option value=\"People\">People"
>    print "<option value=\"Lessons\">Lessons"
>    print "<option value=\"Projects\">Projects"
>    print "</SELECT>"
>    print "<BR>"
>    print "<INPUT TYPE=\"reset\">"
>    print "<INPUT TYPE=\"submit\" NAME=\"submit\" VALUE=\"Submit\">"
>    print "</FORM>"
>    print "</BODY>\n"
>    print "</HTML>\n"
> 
> def Menu():
> form = cgi.FieldStorage()
> if (form.has_key("operations")):
>    if (form["operations"].value == "People"):
>        print "people menu"
>    elif (form["operations"].value == "Lessons"):
>        print "lesson menu"
>    elif (form["operations"].value == "Projects"):
>        print "project menu"
>    else:
>        print "error"
> else:
>    menulist()
> Menu()
> 
> 
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 



More information about the Tutor mailing list