Web Programming with CherryPy
-CherryPy is a minimalist Python web framework that allows web developers to build
web apps in the same way as building a python program.
-Can be downloaded from www.cherrypy.org
-To install cherrypy:
1. Extract the downloaded file and copy the extracted file to C:\Python27 folder.
2. Open command prompt>Go to the cherrypy folder then type Python setup.py
install
-To test if the cherrypy framework is installed properly, type import cherrypy in the
python shell.
Simple Search Engine
<html>
<head>
<title>Simple Search Engine</title>
</head>
<body>
<h3>Simple Search Engine</h3>
<form action="http://www.google.com/search">
<input type="text" name="q">
<input type="submit">
</body>
</html>
Post Data to another Page
Import cherrypy
class Calculator:
def index(self):
fx=open("webcalc.html","r")
webform=fx.read()
returnwebform
index.exposed=True
defdoCalc(self,num1=None,num2=None,select1=None):
intNum1=int(num1)
intNum2=int(num2)
if(select1=="+"):
return "Sum: " + str(intNum1+intNum2)
if(select1=="-"):
return "Difference: " + str(intNum1-intNum2)
if(select1=="*"):
return "Product: " + str(intNum1*intNum2)
if(select1=="/"):
return "Quotient: " + str(intNum1/intNum2)
doCalc.exposed=True
cherrypy.quickstart(Calculator())
<html>
<head>
<title>Calculator</title>
</head>
<body>
<form action="doCalc" method="post">
First number:
<input type="text" name="num1"></input>
<select name="select1">
<option>+</option>
<option>-</option>
<option>*</option>
<option>/</option>
</select>
Second number:
<input type="text" name="num2"></input>
<input type="submit" value="Compute"></input>
</body>
</html>