web.py lightweight development framework - python's ultra-lightweight server
Operating environment:
OS: Ubuntu 12.04 STL
python version: 2.7.5
1. Install web.py
to open the terminal and execute the command:
sudo easy_install web.py
2. Install
lpthw.web The purpose of installing lpthw.web is purely to print the log, which is convenient for background viewing.
Still in the terminal, run the command:
sudo pip install lpthw.web
OK, now start writing a simple program to try:
execute the following command:
mkdir karlweb cd karlweb mkdir bin gothonweb tests docs templates touch bin/app.py touch templates/index.html
At this point, a web.py project is created, then start editing and
execute the following code:
vi bin / app . py
Then enter the following code:
import web urls = ( '/' , 'Index' , # mapping of url addresses '/book' , 'Book' ) app = web.application(urls, globals()) render = web . template . render ( 'templates/' ) class Index ( object ): # this class maps with '/' address def GET ( self ): greeting = "Hello World" return render . index ( greeting = greeting ) class Book ( object ): # This class is mapped with '/book ' address def GET ( self ) : return render .book ( )if __name__ =="__main__": app.run()
Save and exit, execute the following command:
touch templates/index.html vi templates/index.html
Enter the following code:
$def with(greeting)<html><head><title>Web.py test</title></head><body> $if greeting: I just wanted to say <em style="color: green; font-size: 2em;">$greeting</em>. $else:<em>Hello</em>, world!</body></html>
At this point, run the following code:
python bin/app.py
Then visit localhost:8080/ and the following effect will appear:
I just wanted to say Hello World .
At this time, we will execute the following command:
touch templates/book.html vi templates/book.html
Enter the following code:
<html><head><title>BOOK</title></head><body><center>BOOK</center></body></html>
Then we visit localhost:8080/book and we will see BOOK displayed in the browser.
From the above process, it can be seen that webpy integrates the mvc design pattern well, realizes a multi-layer architecture,
and webpy is very lightweight, which is worth learning by program developers.