Web. py: Installation and HelloWorld

Source: Internet
Author: User

1. Introduction to web. py
Web. py is a simple and powerful open-source Web framework based on Python. Because I always prefer lightweight things. Therefore, we chose web. py to learn Web development. At present, there is little information about web. py (even no Wikipedia entries ......), I will learn through tutorials and documents on the official website. I will summarize my learning experience in the future.

2. web. py Installation
Web. py is easy to install, and almost no configuration is required. First of all to install Python, and then download the web. py compressed package: wget http://webpy.org/static/web.py-0.36.tar.gz. After decompression, run Python setup. py install to complete the installation. Web. py has a built-in Web server, so it can be used directly after installation. To be more formal, install flup to provide the WSGI interface (WSGI is an interface between a Python application and a Web server, similar to a java Servlet ). Flup: http://www.saddi.com/software/flup/dist. For flup installation, execute Python setup. py install. If you need to use the database, you also need to install psycopg2 and Postgresql, psycopg can be downloaded here: http://initd.org/psycopg/ I encountered a lot of problems during the installation, the prompt can not find pg_config need to install postgresql-devel plug-in. If you cannot find python. h, you need to install python-devel. Postgresql can be downloaded here: http://www.postgresql.org/download/ I tried with yum, found that your machine has installed the latest version by default.
3. The first web. py Program
If you have Python basics, it is very easy to get started with web. py. As the web. py slogan statement written on the official website: "Think about the ideal way to write a web app. Write the code to make it happen ."
The Hello world Program is very simple. We can directly open the Python interactive interpreter. Then write this line:


>>> Import web
To introduce the web. py module.
Then the next line is:

>>> Urls = ('/', 'index ')
This line declares the url of the website. The first parameter is a regular expression used to match the url. The second parameter is the class of the url to be processed.
Next, use this url to create a program:

>>> App = web. application (urls, globals ())
Next, we will define the index class just mentioned:

>>> Class index:
Def GET (self ):
Return 'Hello World! '
Note that GET is defined to request a webpage. the Html code returned at the end is the content of the webpage. Another important function is POST, which is used to submit forms. In this simple program, it does not need to interact with users. Therefore, only GET functions are defined.
At last, you only need to add this program to run it:

>>> App. run () www.2cto.com
We will see http: // 0.0.0.0: 8080/. 8080 indicates the port number. Enter this address in the browser to view the Hello World page!
If you directly write a script, execute Python filename. py.
Two prompts are displayed:
127.0.0.1: 57720--[17/Sep/2011 22:46:28] "HTTP/1.1 GET/"-200 OK
127.0.0.1: 57720--[17/Sep/2011 22:46:28] "HTTP/1.1 GET/favicon. ico"-404 Not Found
The first "OK" indicates that the url is obtained successfully, and the second "favicon. ico" is not found. Ico is an icon file. It seems to be a website icon. It doesn't matter because we are not creating a formal website.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.