Webpy Getting Started
The work environment needs to produce and test the server frequently, the room has been very chaotic, so the development of a simple and convenient server management system (said a good tall, in fact, is a small Web application can obtain server information). The reason for choosing Webpy, formally because it is simple enough, especially for me this new python. It is a lightweight Python web development framework that works well for personal development applications.
Webpy Install
下载:wget http://webpy.org/static/web.py-0.37.tar.gz
安装:python setup.py install
Webpy ' Hello World '
Refer to Webpy's official documentation: Http://webpy.org/docs/0.3/tutorial
Hello, world as follows:
ImportWeburls= ( '/','Index')classIndex:defGET (self):return "Hello, world!."if __name__=="__main__": App=web.application (URLs, Globals ()) App.run ()
In webpy, the URL request is mapped in the URLs tuple, such as in Get ip:port/, which calls the Get method of the index class directly, returning the string ' Hello, world! ' ;
Class index contains a get method that handles the GET request for the URL corresponding to index;
In the main function, just create a Application object, run to open a simple Web application, the default address is: 127.0.0.1:8080
GET && POST
The web consists of two methods: Get and post
For GET, you can use:
class Index: def GET (self): return " Hello, world!. "
And, for post, use:
class Index: def POST (self): = Web.input (name=None) return"" "! "
HTML templates
In webpy, templates are generally used to store HTML page files. The approximate method of access is as follows:
URLs = ( '/img'image'= Web.template.render ('templates')class Image: def GET (self): return render.image ()
URL mappings are defined in URLs, and access to ip:port/img is handled directly by using the class image;
Web.template.render (path) is used to specify the directory for HTML, which specifies that the specified location of the HTML is located under the templates file under the current folder;
The returned Render.image () indicates that the image.html file is looked for in the directory specified by render and is returned as a result.
class Show: def GET (self): return render.show (' Hello world! ')
$def with (str) < HTML > < Body > $for i in range (5): <H1>$str</H1 > <body></ HTML >
The show class is used to show the string ' Hello world! ', the following HTML for the Show.html,webpy Support Template, which supports parameters to start with the $def with () as a function;
You can use Python statements in HTML, but you need to add $ before the statement, and STR will print 5 times on the page in the HTML above.
static files
In Webpy, provides a way to access the default static files
- Webpy as a server, the static directory is established under the current directory, and Webpy will automatically look for the files in that directory.
- In Apache, you can use the ALIAS directive to map requests to the specified directory before processing web.py.
Webpy DB
In the WEBPY provides a database access API, in fact, from the source can be seen in the MYSQLDB package, but for the sake of convenience is still possible.
db = Web.database (dbn='MySQL', db='Test', user='Root', pw='123123')defNew_post (title, content): Db.insert ('News', Title=title, Content=content, posted_on=Datetime.datetime.utcnow ())defget_post (ID):Try: returnDb.select ('News', where='id= $id', vars=locals ()) [0]exceptIndexerror:returnNonedefget_posts ():returnDb.select ('News', order ='ID DESC')defdel_post (ID): Db.delete ('News', where ='id = $id', VARs =locals ())defupdate_post (ID, title, content): Db.update ('News', where='id = $id', Vars=locals (), Title=title, content=content)
Webpy also supports transactions:
ImportWebDB= Web.database (dbn="Postgres", db="webpy", user="Foo", pw="") T=db.transaction ()Try: Db.insert (' Person', name='Foo') Db.insert (' Person', name='Bar')except: T.rollback ()RaiseElse: T.commit ()
This work is licensed under the Creative Commons Attribution-NonCommercial use-Share 3.0 non-localized version license agreement in the same way. Welcome reprint, please specify the source:
Reprinted from: Cococo Point http://www.cnblogs.com/coder2012
Webpy using Notes