A Hello-World Webpage Using Python


The web programs below print the message entered from the Web by using:
HTML (8.html) ⇒ Python (Hello.py) ⇒ the Web

where the CGI function is used to receive the web user inputs via the following code, for example:
   #!/usr/bin/python                   # where Python is located at
   import cgi                          # importing the CGI module
   form    = cgi.FieldStorage( )       # preparing to read web inputs
   act     = form["act"    ].value     # which button is clicked on
   message = form["message"].value     # the input message
  # A Hello-World Webpage Using Python print Command

  print 
           
~/public_html/course/101/8/8.html (simplified)
<form method="post" target="view1"
    action="http://undcemcs01.und.edu/~wen.chen.hu/cgi-bin/101/8/hello/Hello.py">
  A Hello-World Webpage Using Python print Command
  print 
  <input type="submit" name="act" value="Run" />
  <input type="submit" name="act" value="HTML source">
  <input type="submit" name="act" value="Python source">
  <input type="submit" name="act" value="Help">
  <input type="reset"  value="Reset">
  <iframe name="view1"></iframe>
</form>
~/public_html/cgi-bin/101/8/hello/Hello.py (simplified)
#!/usr/bin/python
#
# Print the message entered from the Web.
#

import cgi       # CGI module

form    = cgi.FieldStorage( )     # Reads the HTML form data.
act     = form["act"    ].value
message = form["message"].value

if ( act == "Run" ):
  # Print HTML.
  print( "Content-type: text/html\n\n" )
  # Command issued:
  print( "/usr/bin/python Hello.py '" + message + "'")
  print( message )

elif ( act == "HTML source" ):
  print( "Content-type: text/plain\n\n" )
  file = open( "/home/wen.chen.hu/public_html/course/101/8/8.html", "r" )
  print( file.read( ) )
  file.close( )

elif ( act == "Python source" ):
  print( "Content-type: text/plain\n\n" )
  file = open( "Hello.py", "r" )
  print( file.read( ) )
  file.close( )

elif ( act == "Help" ):
  print( "Content-type: text/html\n\n" )
  file = open( "../Help.html", "r" )
  print( file.read( ) )
  file.close( )

else:
  print( "Content-type: text/html\n\n" )
  print( "No such option: " + act )