Web.py – some quick references

Writing POST request
We use POST method when we have to submit some data that will make some changes in webserver. Inorder to invoke a POST request,specify the method attribute of html form as POST. Upon submission of form POST method will be called.

The above html form contain a textbox to read name. Now let’s look how to read the name entered into the textbox from inside a POST method.

def POST(self):
        name =  web.input()
        return 'Entered name is '+name.user

Note that the ‘user’ is the name attribute of textbox.
Usually we pass our values through ‘web.websafe()’ function due to security reasons. ( web.websafe(name.user))

Redirect to a new page after POST

def POST(self):
        name =  web.input()
        f = open("file.txt","a")
        raise web.seeother('/names')

The above function describe two attributes. First one is, how to write the input value into a file, and the second is how to redirect to a new page from a POST method.
Inorder to write into a file just open file in append mode.This is to write all names in each submission. Then write into file using write attribute of file.
Inorder to redirect to a new page we use web.seeother() function. Pass the url of page as arguement inside the function call. Here I have given the url ‘/names’ which contain all the names entered through textbox.

web.py – Introduction

Web.py is a web framework for python. You can install it in your Linux by running ‘sudo easy_install web.py’ in your terminal.
Here I would like to share a simple problem and solution that would introduce you into web.py. The problem is ‘ make a root webpage with an option to read name from a text box and a submit button to submit it. Upon submission the name should be saved on a text file and this file should be displayed on another webpage.’

Here is the solution with explanation:

import web

# '/' indicate root,for the root page request home class will be called.
# request for the url '/names' will call hello class

urls = (
  '/names', 'hello','/','home') 

app = web.application(urls, globals(), True)

# upon the GET request for the url '/names', the hello class return the 
#text file which contain the names

class hello:
    def GET(self):
        text = f.read()
        return text

#the home class has two functions to handle GET and POST request from web
#browser. Upon the GET request it return a html webpage.
#The POST function is called when we submit the html form and it write 
# the names to a text file.

class home:
    def GET(self):

        <form name="form" method="POST">
        Name: <input type="text" name="user" />
        <input type="submit" value="Submit" />

    def POST(self):
        form_name =  web.input()
        f = open("file.txt","a")
        raise web.seeother('/names')

if __name__ == "__main__":

How to run this program :
1.Save this program with an extension .py (say code1.py).
2.On the terminal type ‘python sample.py’ . Then on terminal you can see

Terminal Display

3. Now go to your browser and type ‘http://0.0.0:8080&#8217; .A simple page will be displayed.