0% found this document useful (0 votes)
95 views29 pages

Aait-Itsc 1071 (Fundamentals of It) : Lecture 16 - Python For The Web

The document discusses dynamic web pages created using Python CGI scripts, which allow web servers to interpret and execute code to generate customized HTML responses based on requests, with examples provided of basic "Hello World" CGI scripts in Python and how to configure an HTTP server in Python to run CGI scripts located in a cgi-bin directory to build dynamic web applications.

Uploaded by

yeabsera mulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
95 views29 pages

Aait-Itsc 1071 (Fundamentals of It) : Lecture 16 - Python For The Web

The document discusses dynamic web pages created using Python CGI scripts, which allow web servers to interpret and execute code to generate customized HTML responses based on requests, with examples provided of basic "Hello World" CGI scripts in Python and how to configure an HTTP server in Python to run CGI scripts located in a cgi-bin directory to build dynamic web applications.

Uploaded by

yeabsera mulu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 29

AAiT–ITSC 1071 (Fundamentals of IT)

Lecture 16 – Python for the web


Dynamic Documents
• Static documents created using HTML show
“the same” content all the time
– They cannot change
• Dynamic Documents can provide:
– automation of web site maintenance
– customized advertising
– database access
– shopping carts
– date and time service
–…
2
Smart Web Server
• Take a general purpose Web server (that can
handle static documents) and
– have it process requested documents as it send
HTML pages to the client

• The documents could contain commands that


the server understands
– the server includes some kind of interpreter

3
Example Smart Server
• Have the server read each HTML file as it
sends it to the client
• The server could look for this:
<SERVERCODE> some command </SERVERCODE>

• The server doesn’t send this part to the client,


instead it interprets the command and sends
the result to the client
• Everything else is sent normally

4
Server Side Includes
• Server Side Includes (SSI) provides a set of
commands that a server will interpret
• Typically the server is configured to look for
commands only in specially marked
documents
– so normal documents aren’t slowed down
• SSI commands are called directives
– Directives are embedded in HTML comments
– Java Server Pages, PHP, ASP (Microsoft),…
– Other courses (Web I, II) will cover this!!!
5
External Programs
• Another approach is to provide a standard
interface between external programs and
web servers
– We can run the same program from any web
server
– The web server handles all the http,
• In Fundamentals we focus on the special service only
– It doesn’t matter what language we use to write
the external program
• Python, C, Perl,…

6
Common Gateway Interface
• CGI is a standard interface to external
programs supported by most (if not all) web
servers
– CGI programs are often written in scripting
languages (python, perl, tcl, etc.),

• The interface that is defined by CGI includes:


– Identification of the service (i.e.,external program)
– Mechanism for passing the request to the external
program
7
CGI Programming
se
t
fo env
rk (
() ),
e s t , d
ex up(
re qu HTTP ec
() ,
)
ht tp ,
SERVER ..
.

CLIENT CGI Program


http response

8
First CGI Program Python 2.7
#!/usr/bin/python
Print "Content-type: text/html")
print
print "<html>"
print "<head>"
print "<title>Hello World - First CGI Program</title>"
print "</head>"
print "<body>"
print "<h2>Hello World! This is my first CGI program</h2>"
print "</body>"
print "</html>"

This line The line Content-type:text/html\r\n\r\n is part of HTTP


header is sent back to the browser and specify the content type to be
displayed on the browser screen.
9
First CGI Program Python 3.x
#!/usr/bin/python
print ("Content-type: text/html")
print ()
print ("<html>")
print ("<head>")
print ("<title>Hello World - First CGI Program</title>")
print ("</head>")
print ("<body>")
print ("<h2>Hello World! This is my first CGI program</h2>")
print ("</body>")
print ("</html>")

This line The line Content-type:text/html\r\n\r\n is part of HTTP


header is sent back to the browser and specify the content type to be
displayed on the browser screen.
10
CGI Addresses (A.K.A URLs)
• There is mapping between URLs and CGI
programs provided by a web sever
– The exact mapping is not standardized
• web server admin can set it up

• Typically:
– requests that start with /CGI-BIN/ , /cgi-bin/ or
/cgi/, etc.
• not to static documents
• If you are running on Linux or Mac you need to make
the scripts executable
11
Configure Python on Windows
• Open command prompt and type python and
press enter.
– If you get an “ ’python’ not recognized as an
internal or external command …” response, you
need to do the configuration on this slide.

• Configuration: Add python executables to the


path.
– Look at the solution at
http://pythoncentral.io/add-python-to-path-python-is
-not-recognized-as-an-internal-or-external-command/
12
HTTP Server (Python 2.7)
• Python comes with a mini-webserver in the
CGIHttpServer module
• To set it up:
– 1. Create an HTML directory. E.g call it myserver
– 2. Make a cgi-bin folder under myserver.
– 3. Copy the file <Python Folder> \Lib\
CGIHttpServer.py to myserver folder.
– Put your cgi scripts (hello.py) in cgi-bin.
– 5. Run CGIHttpServer.py, as python CGIHttpServer.py
– 6. Point your browser at
http://localhost:8000/cgi-bin/home.py
13
HTTP Server (Python 3.x)
• Python comes with a mini-webserver in the
http.server module
• To set it up:
– 1. Create an HTML directory. E.g call it myserver
– 2. Make a cgi-bin folder under myserver.
– Put your cgi scripts (hello.py) in cgi-bin.
– 5. Run http.ser, as python –m http.server
8000 --cgi
– 6. Point your browser at
http://localhost:8000/cgi-bin/home.py
• Replace Print “hello” to print (“Hello”)
14
Forms
• How can I send data to my CGI scripts?
– HTML forms are used to collect user input
– Form is sent to CGI script when submit button
is pressed
– Data sent via HTTP request
• Server launches CGI script to process data
– Passes the data received from the form
<form method=POST
action=http://localhost:8000/cgi-bin/search.py>
Enter your query: <input type=text name=Search>
<input type=submit>
</form>
15
Request Method: Get
• GET requests can include a query string as part of
the URL:

Delimiter

GET /cgi-bin/search.py?Search=CGI

Request
Resource
Method Query
Name
String

16
HTTP Method: POST
• GET method delivers data as part of URI
• POST method delivers data as the content of a
request
<FORM METHOD=POST ACTION=…>

17
GET vs. POST
• When using forms it’s generally better to use
POST:
– there are limits on the maximum size of a GET
query string
• environment variable
– a post query string doesn’t show up in the
browser as part of the current URL

18
A typical HTML form

<form method=”GET" action="http://localhost:8000/cgi-bin/test.py">


<p>Your first name: <input type="text" name="firstname">
<p>Your last name: <input type="text" name="lastname">
<p>Click here to submit form: <input type="submit" value="Yeah!">
<input type="hidden" name="session" value="1f9a2">
</form>

19
A typical CGI script Python 2.7
#!/usr/local/bin/python
import cgi

def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage() # parse query
if form.has_key("firstname") and form["firstname"].value != "":
print "<h1>Hello", form["firstname"].value, "</h1>"
else:
print "<h1>Error! Please enter first name.</h1>"

main()

20
A typical CGI script Python 3.x
has_key() is deprecated in 3.x use “var in dict”
instead
#!/usr/local/bin/python
import cgi

def main():
print "Content-type: text/html\n"
form = cgi.FieldStorage() # parse query
if "firstname" in form and form["firstname"].value != "":
print "<h1>Hello", form["firstname"].value, "</h1>"
else:
print "<h1>Error! Please enter first name.</h1>"

main()
21
CGI script structure
• Check form fields
– use cgi.FieldStorage class to parse query
• takes care of decoding, handles GET and POST
• "foo=ab+cd%21ef&bar=spam" -->
{'foo': 'ab cd!ef', 'bar': 'spam'} # (well, actually, ...)
• Perform action
– this is up to you!
– database interfaces available
• Generate HTTP + HTML output
– print statements are simplest
– template solutions available

22
Structure refinement
form = cgi.FieldStorage()
if not form:
...display blank form...
elif ...valid form...:
...perform action, display results (or next form)...
else:
...display error message (maybe repeating form)...

23
FieldStorage details
• Behaves like a dictionary:
– .keys(), .has_key() # but not others!
– dictionary-like object ("mapping")
• Items
– values are MiniFieldStorage instances
• .value gives field value!
– if multiple values: list of MiniFieldStorage instances
• if type(...) == types.ListType: ...
– may also be FieldStorage instances
• used for file upload (test .file attribute)
24
Other CGI niceties
• cgi.escape(s)
– translate "<", "&", ">" to "&lt;", "&amp;", "&gt"
• cgi.parse_qs(string, keep_blank_values=0)
– parse query string to dictionary {"foo": ["bar"], ...}
• cgi.parse([file], ...)
– ditto, takes query string from default locations
• urllib.quote(s), urllib.unquote(s)
– convert between "~" and "%7e" (etc.)
• urllib.urlencode(dict)
– convert dictionary {"foo": "bar", ...} to query string
"foo=bar&..." # note asymmetry with parse_qs() above
25
Dealing with bugs
• Things go wrong, you get a traceback...
• By default, tracebacks usually go to the
server's error_log file...
• Printing a traceback to stdout is tricky
– could happen before "Content-type" is printed
– could happen in the middle of HTML markup
– could contain markup itself

26
Using persistent data
• Store/update data on the server:
– In plain files (simplest)
• FAQ wizard uses this
– In a (g)dbm file (better performance)
• string keys, string values
– In a "shelf" (stores objects)
• avoids parsing/unparsing the values
– In a real database (if you must)
• 3rd party database extensions available

27
11/12/1999
Plain files
key = ...username, or session key, or whatever...
try:
f = open(key, "r")
data = f.read() # read previous data
f.close()
except IOError:
data = "" # no file yet: provide initial data
data = update(data, form) # do whatever must be done
f = open(key, "w")
f.write(data) # write new data
f.close()
# (could delete the file instead if updated data is empty)

28
CGI performance
• What causes slow response?
– One process per CGI invocation
• process creation (fork+exec)
• Python interpreter startup time
• importing library modules (somewhat fixable)

– Connecting to a database!
• this can be the killer if you use a real database

– Your code?
• probably not the bottleneck!
29

You might also like