Cgi Programing

12
CGI Programming Python Advanced #7

Transcript of Cgi Programing

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 1/12

CGI Programming

Python Advanced #7

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 2/12

Preamble

 This tutorial is about web interfaHaving a basic understanding oand how it wor"s will be very he

If you have done any PHP beforwill be familiar however in yth

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 3/12

%hat is CGI&

CGI stands for Common Gateway In• CGI is the standard for rograms to

interface with HTTP servers$ '(omeservers too)

•  This means we can ta"e informatiothings li"e *orms$

• CGI scrits generally go in a web se

cgibin directory$

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 4/12

%hat is CGI Programmin

CGI rogramming is writingdynamically generating %ebPagresond to user inut or %ebPathat interact with software on thserver$

•  Things li"e -robo. use ythonweb interface$

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 5/12

(etu

 To be able to get our CGI to worneed a web server we can lay

• If you have your own web serve

want to use that go ahead$• If you don+t lets /uic"ly set u a

aache0 web server on our 1bu

bo.$

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 6/12

(etu

 To install Aache0 'which should be astandard ac"age in almost all linu. dwe use2sudo atget install aache0

•  Then we must set the web server erto allow cgi rograms to e.ecute$*or simlicity we will allow cgi to e.ecthe root directory$

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 7/12

3asic CGI ython scrit

!ets create a basic scrit that 4ust that it wor"ed and rint hello world

• !ets call it hello$y

• ust have a line telling the web serv

where ython is installed eg$#!/usr/bin/python

• %e also have to change the 6le erm

so it can e.ecute$ chmod 755 hello.p

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 8/12

 The cgi module

 The cgi module contains a lot of declaand does a lot of initialiing in the bac

• 3ecause of this 8ever use2from cgi imort 9

• Provides us with methods to get P:(Tre/uests from the web server$

• As well as a few other methods for ardata$

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 9/12

1sing *orm data•  The ython cgi module handles P:(T and G;

same method$

• cgi$*ield(torage') this method will return a *iob4ect that allows us to get data from a subm

• %e can user the *ield(torage ob4ect li"e a y

dictionary or we can use the getvalue') methsuggest not using this method as it will crashmore than one 6eld with the same name$ buit+s 6ne)

Alternative methods are get6rst') and getlist

i l h ll i

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 10/12

(imle hello <insert namrogram•

!et+s create a simle scrit thatthe entered name from P:(T anrints out hello to that name$

!et+s 4ust modify our hello$y 6l

•  Then lets add a chec" bo. to se

they are hay or sad or both,

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 11/12

8otes• %hen debugging you code you can use the

module to outut errors to the age$

• Imort cgitbcgitb$enable')

•  The cgitb$enable') can be modi6ed so that t

messages are saved to a 6le rather than ouusers of your scrit$ This can be done with2cgitb$enable'dislay=>, logdir=?@ath@)

• cgi$escae') Try to always escae user inut

lan to use itB

8/9/2019 Cgi Programing

http://slidepdf.com/reader/full/cgi-programing 12/12

8e.t

-atabase Interaction

• *eel free to leave any /uestions

the comments$