CS1116/CS5018 Lab 1
Together we will review some of the details mentioned in the lecture:
- If they are to run on the cs1 web server, your must store your server-side programs in
- They must have Unix-style new line characters; you can convert your program using
flip -u my_program.py
- They must have the right permisssions:
Note that if you give too many people write permission on your directories, then your programs won't run.
- chmod 604 for HTML, CSS, images, video, audio
- chmod 700 for server-side programs
- chmod 701 for cgi-bin and for directories/folders inside of cgi-bin
- Recall all the things that a server-side program needs to include:
- shebang in the very first column of the very first line
- import of cgitb so that run-time error messages get sent to your browser
- any other imports that you may need, e.g. FieldStorage
- print the content type
- print a blank line
- at the end, print a web page
#!/usr/local/bin/python3 from cgitb import enable enable() # Other imports can go here print("Content-type: text/html") print() # The heart of the program goes here, e.g. get the user data, do something with it print(""" <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title>Your title goes here</title> </head> <body> </body> </html>""")
- Recall how we go about debugging a Python server-side program:
- Type into pyCharm
- Fix any compile-time errors
- Run it from pyCharm. Assuming no compile-time errors, you will see either no error message or Traceback error messages
- At this point, run it in browser (remember to type the URL of the form, don't just double-click on the file, e.g. for student js21: http://cs1.ucc.ie/~js21/cgi-bin/greetings.html)
- If run-time errors are displayed in browser or there is a logic error, fix them
- Go back to step 2
public_html/cgi-bin directory, create a new directory called
(no spaces, all in lowercase!!!!!!). Change the directory permissions to 701.
Take a copy of the following two files (right-click on each link and choose 'Save link as…')
and save them in your
Don't forget to fix their permissions!
spies.html contains a form that allows a user to enter his/her firstname and surname.
Do not modify
spies.html in any way.
spies.py contains a server-side Python program. The program receives the
user's firstname and surname from the form. The program outputs a Web page that
greets the user in 007-fashion. For example, if the user enters 'James' and 'Bond', the
Web page will contain the following paragraph:
Your spy name is: Bond, James Bond.
If the user enters 'Kevin' and 'Kavanagh', the Web page will contain instead the following paragraph:
Your spy name is: Kavanagh, Kevin Kavanagh.
spy.py is full of bugs. Fix it.
Take a copy of the following file (right-click on the link and choose 'Save link as…')
and save it in your
lengths.html contains a form that allows a user to enter a length (a number) and to choose
units from a menu.
Do not modify
lengths.html in any way.
Write a server-side Python program in a file called
lengths.py in your
public_html/cgi-bin/lab1 directory. The program receives the
user's data from the form, and converts the number to the other units. It outputs all three values
in a Web page that contains an HTML table.
For example, if the user enters '13' and chooses 'Feet', then the table will look like this:
For example, if the user enters '22.5' and chooses 'Yards', then the table will look like this:
There is no formal submission of this week's exercises. However, for my records, this weekend I will run a program that checks to see whether you have done the work or not.