----------------------- Exercise #6 for NET2003 due February 28, 2005 ----------------------- -Ian! D. Allen - idallen@idallen.ca Remember - knowing how to find out an answer is more important than memorizing the answer. Learn to fish! RTFM! (Read The Fine Manual) Global weight: 3% of your total mark this term Due date: 10h00 Monday February 28, 2005. The deliverables for this exercise are to be submitted online on the Course Linux Server using the "datsubmit" method described in the exercise description, below. No paper; no email; no FTP. Late-submission date: I will accept without penalty exercises that are submitted late but before 22h00 on Wednesday, March 2. After that late-submission date, the exercise is worth zero marks. Exercises submitted by the *due date* will be marked online and your marks will be sent to you by email after the late-submission date. This exercise is due 10h00 Monday February 28, 2005. Exercise Synopsis: Marks: 3% Writing CGI scripts - executable web pages with dynamic content. The "nc" command - a TCP/IP tool for connecting to TCP/IP servers. Where to work: Do your Unix command line work on the Course Linux Server. Exercise Preparation: See notes and readings in: week05notes.txt, week06notes.txt, CGI_script.txt, script_style.txt --------------------------------------------- Exercise Details (on the Course Linux Server) --------------------------------------------- Have you done all the preparation steps? If not, go back and do them. Finish your Notes Readings (see the weekly Notes files). Any questions? See me in a lab or post questions to the Discussion news group (on the top left of the Course Home Page). You may find it useful to create separate directories in your account to store the files for each exercise. ----------------- Part I - date.cgi ----------------- 0. Follow the preparation directions in Notes file: CGI_script.txt 1. Using VI/VIM, create in your account the executable CGI script date.cgi found at the end of the CGI_script.txt Notes file. 2. Add headers, labels, and comments to the .cgi script to bring it up to the standards expected in NET2003. (See the 10-part format described in Notes file script_style.txt .) 3. Demonstrate in your lab on February 28 the execution of this script in a web browser, with and without a passed argument at the end of the URL. (No late demonstrations accepted.) ----------------------- Part II - weather6a.cgi ----------------------- 4. Copy the above date.cgi script to weather6a.cgi in the same directory. Make sure the new file also has read and execute permissions. Test the copy to make sure it also works properly from the command line and from a web browser. 5. Modify the end of weather6a.cgi by deleting everything after the "date" command and replacing those lines with these four lines: echo "The Temperature in Ottawa" URL='http://text.weatheroffice.ec.gc.ca/forecast/city_e.html?on-118' lynx -dump "$URL" | grep -A 1 'Temperature:' | tr ' \n' ' ' echo "" 6. Test the weather6a.cgi script from the Unix command line, as you did when testing the date.cgi script: $ ./weather6a.cgi Content-Type: text/plain Mon Feb 14 10:14:51 EST 2005 The Temperature in Ottawa Temperature: -14°C 7. Execute your new weather6a.cgi script in your browser: http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6a.cgi Your browser window should display the Ottawa temperature. If it doesn't work, go back and test your CGI script from the Unix command line. 8. Add headers, labels, and comments to the script to bring it up to the standards expected in NET2003. (See the 10-part format described in Notes file script_style.txt .) 9. Demonstrate in your lab on February 28 the execution of this script in a web browser. (No late demonstrations accepted.) ------------------------ Part III - weather6b.cgi ------------------------ Using CGI argument passing (described in Notes file CGI_script.txt), write a CGI script named weather6b.cgi that shows the weather for the city code given as the word at the end of the URL, e.g. http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6b.cgi?on-118 If no city code is given (no argument), give the weather for Ottawa (city code on-118): $ ./weather6b.cgi Content-Type: text/plain DEBUG: Using default city code on-118 ./weather6b.cgi: The current temperature in on-118 is: Temperature: -14°C $ ./weather6b.cgi qc-147 Content-Type: text/plain DEBUG: Using argument city code qc-147 ./weather6b.cgi: The current temperature in qc-147 is: Temperature: -11°C The DEBUG messages are optional. Your script should not display any DEBUG messages when you hand it in. If no weather was found (probably due to an invalid city code, or the web site being down), display only an error message at the end of the script: $ ./weather6b.cgi XXX Content-Type: text/plain ./weather6b.cgi: Could not find temperature for city code 'XXX' Now try it in your browser: http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6b.cgi http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6b.cgi?on-118 http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6b.cgi?qc-147 http://net2003.idallen.ca:8888/~abcd0001/cgi-bin/weather6b.cgi?XXX Make sure you can see the error message in your web browser when you give a bad city code. BONUS: If the standard input to the script is a keyboard/terminal, the script is being run from the command line and it should not output the prefix "Content-type" line or the blank line that follows it. Fix the script to omit these two lines if standard input is a terminal. $ ./weather6b.cgi ./weather6b.cgi: The current temperature in on-118 is: Temperature: -14°C The script must output the two prefix lines when called as a CGI script. (A CGI script does not have terminal/keyboard on standard input.) Hint: Notes file shell_read.txt talks about reading from a keyboard ------------------------- Part IV - weatherout6.txt ------------------------- A. From the command line, test your script with these city code arguments (in the given order) and append all the output to file weatherout6.txt : on-118 nu-21 qc-133 nt-24 B. Start a script session log (reference Notes file miscellaneous.txt). (See also "man script", especially the "BUGS" section.) C. Following the session log below, use the "nc" command to connect to your CGI script and fetch your raw date.cgi web page. The lines you type are indicated by "*" in the sample session below. (There is one empty line you must type.) The rest is sample output from the web server: Script started, file is out.txt * $ nc -v net2003.idallen.ca 8888 DNS fwd/rev mismatch: home.idallen.ca != cpu1808.adsl.bellglobal.com home.idallen.ca [206.47.37.39] 8888 (?) open * GET /~abcd0001/cgi-bin/date.cgi?hello HTTP/1.0 * HTTP/1.1 200 OK Date: Mon, 14 Feb 2005 15:41:28 GMT Server: Apache-AdvancedExtranetServer/2.0.47 (Mandrake Linux/1.6.91mdk) mod_perl/1.99_08 Perl/v5.8.0 mod_ssl/2.0.47 OpenSSL/0.9.7a PHP/4.3.1 Connection: close Content-Type: text/plain; charset=ISO-8859-1 Mon Feb 14 10:41:29 EST 2005 The argument count is 1 The argument is 'hello' * $ exit Script done, file is out.txt D. As mentioned in the miscellaneous.txt file, use the "col -b" command to filter out backspaces and return characters from the script session log file. ("man col") Append the result to file weatherout6.txt . E. Add your assignment label to the top of the weatherout6.txt file. ------------------------------- Part V - internal documentation ------------------------------- Your scripts must meet the standards for NET2003 scripts, as described in the Notes file script_style.txt . Scripts without added comment lines will not be marked. ---------- Submission ---------- Submit the files for marking: $ datsubmit 06 date.cgi weather6a.cgi weather6b.cgi weatherout6.txt Always submit all files for marking at the same time.