CSC111 WebPage.py

From CSclasswiki
Jump to: navigation, search

--Thiebaut 22:26, 15 April 2009 (UTC)



# webpage.py
# D. Thiebaut
# creates a web page from a few pieces of information
# gathered from the user.
# the output page is called userinfo.htm and is automatically
# stored in the public_html directory, and its permission is
# also set to readable by all (no need to do it by hand!)

import os          # needed for function chmod

#----------------------------------------------------------------
# getUserInfo: gets first, and last names of user, address and
#  class
# returns 4 strings: first, last, address, class, in this order
#----------------------------------------------------------------
def getUserInfo():
   # get user information
   fn = raw_input( "first name? " )
   ln = raw_input( "last name?  " )
   add= raw_input( "address?    " )
   cl = raw_input( "class of?   " )

   return ( fn, ln, add, cl )

#----------------------------------------------------------------
# createWebPage: receives a file name and 4 strings representing
#    the user names, address and class, and creates a simple Web
#    page
# returns nothing
#----------------------------------------------------------------
def createWebPage( fileName, fn, ln, addr, cl ):

   # add public_html/ to the file name
   fileName = "public_html/"+fileName
   print "\n\nwriting html information to "+fileName

   # open the file and create html text 
   f = open( fileName, 'w' )
   f.write( "<html>\n" )
   f.write( "<head>\n" )
   f.write( "<title>Page of %s %s</title>\n" % ( fn, ln ) )
   f.write( "</head>\n" )
   f.write( "<body>\n" )
   f.write( "<h1>%s %s, '%s</h1>\n" % ( fn, ln, cl ) )
   f.write( "<h2>%s</h2>\n" % ( addr ) )
   f.write( "</body>\n" )
   f.write( "</html>\n" )
   f.close()
   
   # change the privileges of the file to 744
   # to make it readable by all (and in particular by the Http
   # server) user:rwx group:r other:r
   
   os.chmod( fileName, 0644 )
   print "file %s is now readable by all!" % ( fileName )
                               
#----------------------------------------------------------------
#                              M A I N
#----------------------------------------------------------------
def main():
   #--- get user information ---
   (firstName, lastName, address, userClass) = getUserInfo()

   #--- create the Web page
   createWebPage( "userinfo.htm", firstName, lastName, address, userClass )


main()