说明:蓝色=命令名称
浅绿=命令参数
浅蓝=选项
紫色=目录
系统环境:CentOS 5.5 x86_64
学web.py框架时写的一个很小很简陋的小实例,效果如图:
运行环境 python 2.6以上,mysql数据库。
建库脚本:createdb.py
- #!/usr/bin/env python27
- #-*- coding:utf-8 -*-
- import MySQLdb,sys
- conn = MySQLdb.connect(host='localhost',user='root',passwd='')
- cursor = conn.cursor()
- cursor.execute("create database if not exists notes")
- conn.select_db('notes')
- cursor.execute("create table if not exists notes(nid int(10) not null primary key auto_increment,date date,time time, text text default null ,comment int(10) default 0)")
- cursor.execute("create table if not exists user(uid int(5) not null primary key auto_increment,user varchar(10) not null,passwd varchar(20) not null ,perm int(2) default 1,datetime datetime)")
- cursor.execute("create table if not exists comment(nid int(10) not null primary key auto_increment,cid int(10) not null,datetime datetime,name varchar(20) default '匿名',mail varchar(20) default '保密', comment text )")
- cursor.execute("insert into user(user, passwd) values('admin', password('123456'))")
- cursor.execute("insert into notes(date,time,text)values(now(),now(),'hello world')")
- cursor.close()
- conn.close()
配置脚本:setting.py
- #!/usr/bin/env python27
- #-*- coding:utf-8 -*-
- import web
- #import sae.const
- urls = ('', 'reindex',
- '/','index',
- '/add','add',
- '/login','login',
- '/about','about',
- '/logout','logout',
- '/alter','alter',
- '/new', 'new',
- '/search','search',
- '/comment','comment'
- )
- render = web.template.render("templates/")
- #db = web.database(dbn='mysql', db = sae.const.MYSQL_DB, host = sae.const.MYSQL_HOST, port = int(sae.const.MYSQL_PORT), user = sae.const.MYSQL_USER, pw = sae.const.MYSQL_PASS)
- db = web.database(dbn='mysql', db='notes', host='localhost', port=3306, user='root', pw='')
根据自己情况自行定义。
配置好环境 运行 run.py 然后通过8080端口就可以访问了。
源码下载:https://github.com/zuopucuen/notes