Python Scripts in Browsers: Programming Tools (MCS 275) Introduction To CGI
Python Scripts in Browsers: Programming Tools (MCS 275) Introduction To CGI
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
$ httpd -v
Server version: Apache/2.4.23 (Unix)
Server built: Aug 8 2016 16:31:34
$
apachectl configtest
/etc/apache2/extra/httpd-userdir.conf
Include /private/etc/apache2/users/*.conf
<Directory "/Users/<user>/Sites/">
Options Indexes MultiViews
AllowOverride None
Require all granted
</Directory>
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
#!/usr/bin/python
"""
Place this in /Library/WebServer/CGI-Executables.
"""
print("Content-Type: text/plain\n\n")
print("Python works")
#!/usr/bin/python
"""
Displays the current time in a browser window.
"""
import time
print("Content-Type: text/plain\n")
print(time.ctime(time.time()))
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
We distinguish between
Client-Side Scripting: processed by browser
advantage: saves time of the server
Server-Side Scripting: processed by server
needed for synchronization, modification of data
server time is then the time
Python is a powerful server-side scripting language.
The cgi module has to be imported,
in order to communicate the data from client.
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
<HTML>
<HEAD>
<TITLE> MCS 275 Lec 21: give a number </TITLE>
</HEAD>
<BODY>
<FORM action="http://localhost/cgi-bin/give_number.py">
give a number:
<INPUT type="submit">
</FORM>
</BODY>
</HTML>
#!/usr/bin/python
"""
Accepts a number from a form.
"""
import cgi
form = cgi.FieldStorage()
print("Content-Type: text/plain\n")
try:
n = form[’number’].value
print("your number is " + n)
except KeyError:
print("please enter a number")
2 Internet Basics
HTTP: GET and POST methods
FORM to accept user data
server-side scripting
<HTML>
<HEAD>
<TITLE> MCS 275 Lec 21: web gcd </TITLE>
</HEAD>
<BODY>
<FORM action="http://localhost/cgi-bin/web_gcd.py">
<P> give first number:
<input type="text" name="A" size ="40"> </P>
<P>give second number:
<input type="text" name="B" size ="40"> </P>
<P> <input type="submit"> </P>
</FORM>
</BODY>
</HTML>
ix = int(x)
iy = int(y)
print("gcd(%d,%d) = %d" % (ix, iy, gcd(ix, iy)))
Exercises:
1 Make your own web page, using people.uic.edu.
2 Verify if Apache is installed on your computer.
Install Apache if necessary.
3 Design a web interface to convert pounds into kilograms and
kilograms into pounds.
4 Take the script facnum.py of Lecture 1
and write a web interface for it.