diff --git a/cgi-bin/cgi_1.py b/cgi-bin/cgi_1.py index baa5c3e..60fa9bc 100644 --- a/cgi-bin/cgi_1.py +++ b/cgi-bin/cgi_1.py @@ -1,5 +1,8 @@ -#!/usr/bin/env python -import cgi - - -cgi.test() +#!/usr/bin/env python +import cgitb +import cgi +cgitb.enable() + + + +cgi.test() diff --git a/cgi-bin/cgi_2.py b/cgi-bin/cgi_2.py index ae4ed0d..75f560c 100644 --- a/cgi-bin/cgi_2.py +++ b/cgi-bin/cgi_2.py @@ -1,32 +1,32 @@ -#!/usr/bin/env python -import cgi -import cgitb -cgitb.enable() -import os -import datetime - - -default = "No Value Present" - - -print("Content-Type: text/html") -print("") - -body = """ - -Lab 1 - CGI experiments - - -

Hey there, this page has been generated by {software}, running {script}

-

Today is {month} {date}, {year}.

-

This page was requested by IP Address {client_ip}

- -""".format( - software=os.environ.get('SERVER_SOFTWARE', default), - script='aaaa', - month='bbbb', - date='cccc', - year='dddd', - client_ip='eeee' -) -print(body) +#!/usr/bin/env python +import cgi +import cgitb +cgitb.enable() +import os +import datetime + + +default = "No Value Present" + + +print("Content-Type: text/html") +print("") + +body = """ + +Lab 1 - CGI experiments + + +

Hey there, this page has been generated by {software}, running {script}

+

Today is {month} {date}, {year}.

+

This page was requested by IP Address {client_ip}

+ +""".format( + software=os.environ.get('SERVER_SOFTWARE', default), + script=os.environ.get('SCRIPT_NAME', default), + month=datetime.datetime.now().strftime('%B'), + date=datetime.datetime.now().day, + year=datetime.datetime.now().year, + client_ip=os.environ.get('REMOTE_ADDR', default), +) +print(body) \ No newline at end of file diff --git a/cgi-bin/cgi_sums.py b/cgi-bin/cgi_sums.py index feed2bc..8b1d67e 100644 --- a/cgi-bin/cgi_sums.py +++ b/cgi-bin/cgi_sums.py @@ -1,9 +1,18 @@ -#!/usr/bin/env python -import cgi -import cgitb - -cgitb.enable() - -print("Content-type: text/plain") -print() -print("Your job is to make this work") +#!/usr/bin/env python +import cgi +import cgitb + +cgitb.enable() + +form = cgi.FieldStorage() +operands = form.getlist('operand') + +try: + total = sum(map(int, operands)) + body = "Your total is: {}".format(total) +except (ValueError, TypeError): + body = "Unable to calculate a sum: please provide integer operands." + +print("Content-type: text/plain") +print() +print(body) \ No newline at end of file diff --git a/cgi-bin/hello_world.py b/cgi-bin/hello_world.py index 94f9a2d..db7ca0b 100644 --- a/cgi-bin/hello_world.py +++ b/cgi-bin/hello_world.py @@ -1,7 +1,7 @@ -#!/usr/bin/env python - -print("Content-Type: text/plain") -print("") - -print("Hello, world!") - +#!/usr/bin/env python + +print("Content-Type: text/plain") +print("") + +print("Hello, world!") +