CS1116/CS5018

Web Development 2

Dr Derek Bridge

School of Computer Science & Information Technology

University College Cork

A web page that contains a form

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8" />
        <title>Challenge</title>
    </head>
    <body>
        <p>
            Halt! Who goes there?
        </p>
        <form action="response.py" method="get">
            <input type="text" name="firstname" />
            <input type="text" name="surname" />
            <input type="submit" />
        </form>
    </body>
</html>

Handling a form

The user requests a form, fills it in and presses Submit. The browser sends the data to the server. The server executes the program using the data and sends back the program's output.

When the user presses submit…

…and behind the scenes

And then…

The server-side program, response.py

#!/usr/local/bin/python3

from cgi import FieldStorage

print('Content-Type: text/html')
print()

form_data = FieldStorage()
fname = form_data.getfirst('firstname')
sname = form_data.getfirst('surname')

print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Response</title>
        </head>
        <body>
            <p>
                Hello, %s %s. You may go on your way.
            </p>
        </body>
    </html>""" % (fname, sname))

Class exercise

Incomplete solution

#!/usr/local/bin/python3

from cgi import FieldStorage

print('Content-Type: text/html')
print()












print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Your message in Morse code</title>
        </head>
        <body>
            <table>
                <tr>
                    <th>Original: </th><td>%s</td>
                </tr>
                <tr>
                    <th>Morse: </th><td>%s</td>
                </tr>
            </table>
        </body>
    </html>""" % (message, morse))

Complete solution

#!/usr/local/bin/python3

from cgi import FieldStorage

print('Content-Type: text/html')
print()

morse_dict = {
        'A': '.-',    'B': '-...',  'C': '-.-.',  'D': '-..',   'E': '.',
        'F': '..-.',  'G': '--.',   'H': '....',  'I': '..',    'J': '.---',
        'K': '-.-',   'L': '.-..',  'M': '--',    'N': '-.',    'O': '---',
        'P': '.--.',  'Q': '--.-',  'R': '.-.',   'S': '...',   'T': '-',      
        'U': '..-',   'V': '...-',  'W': '.--',   'X': '-..-',  'Y': '-.--',   
        'Z': '--..'
}

form_data = FieldStorage()
message = form_data.getfirst('message')

morse_list = [morse_dict[char.upper()] for char in message]
morse = ' '.join(morse_list)

print("""
    <!DOCTYPE html>
    <html lang="en">
        <head>
            <meta charset="utf-8" />
            <title>Your message in Morse code</title>
        </head>
        <body>
            <table>
                <tr>
                    <th>Original: </th><td>%s</td>
                </tr>
                <tr>
                    <th>Morse: </th><td>%s</td>
                </tr>
            </table>
        </body>
    </html>""" % (message, morse))