导航栏: 首页 评论列表

python环境搭建

默认分类 2013/12/26 00:16

python环境搭建

1.安装python(Ubuntu自带2.7.4,其实可以不用重装)

wget http://www.python.org/ftp/python/2.7.6/Python-2.7.6.tgz
tar -zxvf Python-2.7.6.tgz
cd Python-2.7.6
./configure
make 
sudo make install

然后使用 python -V 查看版本 >> Python 2.7.6

参考:http://lutaf.com/141.htm

2.安装setuptools (无需最新2.0,使用0.9.8即可)

wget https://pypi.python.org/packages/source/s/setuptools/setuptools-0.9.8.tar.gz#md5=243076241781935f7fcad370195a4291(这是setuptools-0.9.8压缩包下载地址)
tar -zxvf setuptools-0.9.8.tar.gz
cd setuptools-0.9.8
sudo python setup.py build
sudo python setup.py install

然后使用sudo easy_install 如果返回error: No urls, filenames, or requirements specified (see --help)表示安装成功

3.安装web.py

sudo easy_install web.py

然后,新建文件server.py(任意目录),并运行python server.py,然后打开浏览器输入http://localhost:8080/

import web

urls = (
    '/(.*)', 'hello'
)
app = web.application(urls, globals())

class hello:        
    def GET(self, name):
        if not name: 
            name = 'World'
        return 'Hello, ' + name + '!'

if __name__ == "__main__":
    app.run()

4.安装mysql

sudo apt-get install mysql-server 

安装完成之后输入

mysql -u root -p 

然后创建测试数据库test

mysql> show databases;
mysql> create database test;
mysql> use test;

创建测试数据表

CREATE TABLE post_list (    
    id INT AUTO_INCREMENT,    
    title TEXT,    
    content TEXT,      
    primary key (id)    
);   
insert into post_list (title,content) values ('hello','This is hello world');

5.简单博客API, app.py

#!/usr/local/bin/python
#coding=utf-8
import web
import json

# routes        
urls = (
    '/hello/(.*)', 'Hello',
    '/', 'List',
    '/view/(\d+)', 'View',
    '/new', 'New',
    '/delete/(\d+)', 'Delete',
    '/edit/(\d+)', 'Edit',
)

app = web.application(urls, globals())
# database instance
db = web.database(dbn = 'mysql', db = 'test', user = 'root', pw = 'password')

# Hello World  
class Hello:
    def GET(self, name = 'World'):
        return 'Hello, ' + name + '!'

#查看文章类  
class View:
    def GET(self, id):
        try:
            return json.dumps(db.select('post_list', where = 'id='+str(int(id)), vars = locals())[0])
        except IndexError:
            return None
#首页类  
class List:
    def GET(self):
        posts = db.select('post_list', order = 'id DESC')
        return json.dumps(posts.list())
#新建文章类  
class New:
    def GET(self):
        form = web.input()
        db.insert('post_list',
            title = form.title,
            content = form.content)

        raise web.seeother('/')
#删除文章类  
class Delete:
    def GET(self, id):
        db.delete('post_list', where = 'id = ' + str(int(id)), vars = locals())
        raise web.seeother('/')
#编辑文章类  
class Edit:
    def GET(self, id):
        form = web.input()
        db.update('post_list',
            where = ' id = ' + str(int(id)),
            vars = locals(),
            title = form.title,
            content = form.content)

        raise web.seeother('/')
#定义404错误显示内容  
def notfound():
    return web.notfound("Sorry, the page you were looking for was not found.")

app.notfound = notfound
#运行  
if __name__ == '__main__':
    app.run()

6.运行python app.py, 通过浏览器访问http://localhost:8080/就能看到了


>> 留言评论