web programming with apache and python
TRANSCRIPT
Web Programming with Apache and PythonWeb Programming with Apache and Python
Balamurugan SEmail: [email protected]: http://www.bitbala.org/
Agenda
● Introduction to Web
● Introduction to Apache
● Web Programming with Python and CGI
● Web Programming with Python and mod_python
Web Programming with Apache and Python
Introduction to Web
● Internet
● Need for dynamic content over Web
● Web servers
Web Programming with Apache and Python
Web servers
● Computer program which delivers the content to users
over Hypertext Transfer protocol (HTTP) over the World
Wide Web (WWW)
● Apache httpd, Apache Tomcat, Microsoft IIS, Google
GWS, lighthttpd
Web Programming with Apache and Python
How web server works?
Web Programming with Apache and Python
Web Clients - Browsers
Web Servers Applications1. Sends request
3. Sends response
2. Request External executions
Apache – Installation
● Debian based systems
apt-get install apache2
● RPM based systems
yum install apache2
● Can be downloaded and compiled :)
Web Programming with Apache and Python
Introduction to CGI
● Common Gateway Interface
● Written using high level languages
● Commonly used High level languages are
C/C++
Python
Perl
PHP
● CGI is no longer widely used
● CGI is good for small applications
Web Programming with Apache and Python
CGI
● How CGI scripts are different from normal scripts?
● What kind of Inputs it takes?
● How to generate the output?
Since it is sending the data to Web browsers, it
should send valid HTTP Data (MIME Headers +
HTML)
Web Programming with Apache and Python
Apache Configuration for CGI
Web Programming with Apache and Python
<Di r ect or y " / pat h/ t o/ cgi / scr i pt s">Al l owOver r i de NoneOpt i ons +ExecCGIAddHandl er cgi - scr i pt . pyOr der al l ow, denyAl l ow f r om al l
</ Di r ect or y>
<Di r ect or y " / pat h/ t o/ cgi / scr i pt s">Opt i ons Al l
</ Di r ect or y
● Change the content of /etc/apache2/sites-available
Hello World!!!
Web Programming with Apache and Python
1 #! / usr / bi n/ pyt hon
2 pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n"3 pr i nt ' <ht ml >'4 pr i nt ' <head>'5 pr i nt ' <t i t l e>Hel l o Wor d - Fi r st CGI Pr ogr am</ t i t l e>'6 pr i nt ' </ head>'7 pr i nt ' <body>'8 pr i nt ' <h2>Hel l o Wor d! Thi s i s my f i r st CGI pr ogr am</ h2>'9 pr i nt ' </ body>'10 pr i nt ' </ ht ml >'
● Helloworld.py
Line 2 – Sends the HTTP Header to browser.
CGI Environment Variables
Web Programming with Apache and Python
1 #! / usr / bi n/ pyt hon
2 i mpor t os
3 pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n" ;4 pr i nt "<f ont si ze=+1>Envi r onment </ f ont ><\ br >" ;5 f or par am i n os. envi r on. keys( ) :6 pr i nt "<b>%s</ b>: %s<\ br >" % ( par am, os. envi r on[ par am] )
● CONTENT_TYPE, CONTENT_LENGTH, HTTP_COOKIE, HTTP_USER_AGENT, PATH_INFO, QUERY_STRING, REMOTE_ADDR, REMOTE_HOST, REQUEST_METHOD, SCRIPT_FILENAME, SCRIPT_NAME, SERVER_NAME, SERVER_SOFTWARE
To print the CGI Environment Variables
cgi module
Web Programming with Apache and Python
● Primary module to be used in CGI programming in
python
● FieldStorage is the primary class that will be used
● A object has to be created for FieldStorage when
python CGI scripts begins
● The object will read all the pertinent user info sent
by web client in a dictionary
● Name given in the form will be keys and Values of
the form elements will be values in dictionaries
Sending data over Forms - GET
Web Programming with Apache and Python
<f or m act i on="user _get . py" met hod="get ">Fi r st Name: <i nput t ype="t ext " name="f i r st _name"> <br / >
Last Name: <i nput t ype="t ext " name=" l ast _name" / ><i nput t ype="submi t " val ue="Submi t " / ></ f or m>
userform_get.html
1 #! / usr / bi n/ pyt hon2 i mpor t cgi , cgi t b3 user _dat a = cgi . Fi el dSt or age( ) 4 f i r st _name = user _dat a. get val ue( ' f i r st _name' )5 l ast _name = user _dat a. get val ue( ' l ast _name' )6 pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n"7 pr i nt "<ht ml >"8 pr i nt "<head>"9 pr i nt "<t i t l e>Sendi ng Dat a - GET</ t i t l e>"10 pr i nt "</ head>"11 pr i nt "<body>"12 pr i nt "<h2>Hel l o %s %s</ h2>" % ( f i r st _name, l ast _name)13 pr i nt "</ body>"14 pr i nt "</ ht ml >"
user_get.py
Sending data over Forms - POST
Web Programming with Apache and Python
<f or m act i on="user _post . py" met hod="post ">Fi r st Name: <i nput t ype="t ext " name="f i r st _name"> <br / >
Last Name: <i nput t ype="t ext " name=" l ast _name" / ><i nput t ype="submi t " val ue="Submi t " / ></ f or m>
userform_post.html
1 #! / usr / bi n/ pyt hon2 i mpor t cgi , cgi t b3 user _dat a = cgi . Fi el dSt or age( ) 4 f i r st _name = user _dat a. get val ue( ' f i r st _name' )5 l ast _name = user _dat a. get val ue( ' l ast _name' )6 pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n"7 pr i nt "<ht ml >"8 pr i nt "<head>"9 pr i nt "<t i t l e>Sendi ng Dat a - GET</ t i t l e>"10 pr i nt "</ head>"11 pr i nt "<body>"12 pr i nt "<h2>Hel l o %s %s</ h2>" % ( f i r st _name, l ast _name)13 pr i nt "</ body>"14 pr i nt "</ ht ml >"
user_post.py
cgitb module
Web Programming with Apache and Python
● This module provides a special exception handler for
Python scripts
● This will help you in debugging the CGI scripts
● After activating this module, if an uncaught exception
occurs, a detailed, formatted report will be displayed.
● To use the module you have to
● i mport cgi tb
● To enable the module
● cgi tb. enabl e()
Passing check box
Web Programming with Apache and Python
<f or m act i on="user _checkbox. py" met hod="POST" t ar get ="_bl ank"><i nput t ype="checkbox" name="mat hs" val ue="on" / > Mat hs<i nput t ype="checkbox" name="physi cs" val ue="on" / > Physi cs<i nput t ype="submi t " val ue="Sel ect Subj ect " / ></ f or m>
userform_checkbox.html
1 #! / usr / bi n/ pyt hon2 i mpor t cgi , cgi t b 3 f or m = cgi . Fi el dSt or age( ) 4 i f f or m. get val ue( ' mat hs' ) :5 mat h_f l ag = "ON"6 el se:7 mat h_f l ag = "OFF"8 i f f or m. get val ue( ' physi cs' ) :9 physi cs_f l ag = "ON"10 el se:11 physi cs_f l ag = "OFF"12 pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n"13 pr i nt "<ht ml ><head><t i t l e>Checkbox</ t i t l e>"14 pr i nt "</ head><body>"15 pr i nt "<h2> CheckBox Mat hs i s : %s</ h2>" % mat h_f l ag16 pr i nt "<h2> CheckBox Physi cs i s : %s</ h2>" % physi cs_f l ag17 pr i nt "</ body></ ht ml >"
user_checkbox.py
Passing Radio buttons
Web Programming with Apache and Python
<f or m act i on="r adi obut t on. py" met hod="post " t ar get ="_bl ank"><i nput t ype="r adi o" name="subj ect " val ue="mat hs" / > Mat hs<i nput t ype="r adi o" name="subj ect " val ue="physi cs" / > Physi cs<i nput t ype="submi t " val ue="Sel ect Subj ect " / ></ f or m>
userform_radiobutton.html
#! / usr / bi n/ pyt hon
i mpor t cgi , cgi t b
f or m = cgi . Fi el dSt or age( )
i f f or m. get val ue( ' subj ect ' ) : subj ect = f or m. get val ue( ' subj ect ' )el se: subj ect = "Not set "
pr i nt "Cont ent - t ype: t ext / ht ml \ r \ n\ r \ n"pr i nt "<ht ml ><head><t i t l e>Radi o</ t i t l e>"pr i nt "</ head><body>"pr i nt "<h2> Sel ect ed Subj ect i s %s</ h2>" % subj ectpr i nt "</ body></ ht ml >"
radiobutton.py
File Upload
Web Programming with Apache and Python
<ht ml ><body> <f or m enct ype="mul t i par t / f or m- dat a" act i on="save_f i l e. py" met hod="post "> <p>Fi l e: <i nput t ype="f i l e" name="f i l ename" / ></ p> <p><i nput t ype="submi t " val ue="Upl oad" / ></ p> </ f or m></ body></ ht ml >
fileupload.html
#! / usr / bi n/ pyt hon
i mpor t cgi , osi mpor t cgi t b; cgi t b. enabl e( )
f or m = cgi . Fi el dSt or age( )f i l ei t em = f or m[ ' f i l ename' ]
i f f i l ei t em. f i l ename: f n = os. pat h. basename( f i l ei t em. f i l ename) open( ' / t mp/ ' + f n, ' wb' ) . wr i t e( f i l ei t em. f i l e. r ead( ) ) message = ' The f i l e " ' + f n + ' " was upl oaded successf ul l y' el se: message = ' No f i l e was upl oaded'pr i nt " " "Cont ent - Type: t ext / ht ml \ n<ht ml ><body><p>%s</ p></ body></ ht ml >""" % ( message, )
fileupload.py
mod_python
● A module to connect Python and Apache
● Main advantages of mod_python over traditional CGI
is performance
● Sample performance data St andar d CGI : 23 r equest s/ sMod_pyt hon cgi handl er : 385 r equest s/ sMod_pyt hon publ i sher : 476 r equest s/ s
Tests are done wi th 1. 2GHz Penti num machi ne sendi ng 1000 requests wi th f requency of 1
Web Programming with Apache and Python
mod_python - Installation
● Debian based Systems● apt- get i nstal l l i bapache2- mod- python
● RPM based systems● apt- get i nstal l l i bapache2- mod- python
Web Programming with Apache and Python
mod_python – Configuring Apache
Web Programming with Apache and Python
<Di r ect or y " / pat h/ t o/ scr i pt s"> Set Handl er mod_pyt hon . py Pyt honHandl er mod_pyt hon. publ i sher
Pyt honDebug On Pyt honAut oRel oad On Pyt honPat h "sys. pat h + [ ' / pat h/ t o/ modul es' ] "
</ Di r ect or y>
● Change the content of /etc/apache2/sites-available
Hello World!!!
Web Programming with Apache and Python
1 def i ndex( ) :2 s = "" " \3 <ht ml >4 <body>5 <h2>Hel l o Wor l d! </ h2>6 </ body>7 </ ht ml >8 " ""9 r et ur n s
● Helloworld.py
● URL to access this - http://localhost/hello.py/index or http://localhost/hello.py
Hello World!!! - one more time
Web Programming with Apache and Python
s = " " " \<ht ml ><body><h2>Hel l o %s! </ h2></ body></ ht ml >"""
def i ndex( ) : r et ur n s % ' Wor l d' def ever ybody( ) : r et ur n s % ' ever ybody'
def _hi det hi sf unct i on r et ur n “You cannot access t hi s f unct i on”
● Helloworld_new.py
● URL to access everybody function http://localhost/helloworld_new.py/everybody
● Function starting with _ will be hidden to the web
Sending form data
Web Programming with Apache and Python
● Request object will hold the user data sent over Web
<ht ml ><body><f or m act i on="show. py/ out put " met hod="post "><p>Type Your name: <i nput t ype="t ext " name="user name"><i nput t ype="submi t " val ue="Submi t "></ p></ f or m></ body></ ht ml >
user_form.html
def out put ( r eq) : name = r eq. f or m[ ' user name' ] s = " " " \<ht ml ><body><p>The submi t t ed wor d was "%s"</ p><p><a hr ef ="user _f or m. ht ml ">Submi t anot her wor d! </ a></ p></ body></ ht ml >""" r et ur n s % name
●show.py
File Upload
Web Programming with Apache and Python
<ht ml ><body> <f or m enct ype="mul t i par t / f or m- dat a" act i on="upl oad. py/ upl oad" met hod="post "> <p>Fi l e: <i nput t ype="f i l e" name="f i l ename" / ></ p> <p><i nput t ype="submi t " val ue="Upl oad" / ></ p> </ f or m></ body></ ht ml >
fileupload.html
def upl oad( r eq) :
f i l ename = r eq. f or m[ ' f i l ename' ] . f i l ename f p = " / t mp/ "+f i l ename f = f i l e( f p, ' wb' ) f i l e_cont ent s = r eq. f or m[ ' f i l ename' ] . f i l e dat a = f i l e_cont ent s. r ead( ) f . wr i t e( dat a) f . cl ose r et ur n "Fi l e upl oad successf ul ”
upload.py