說明:藍色=命令名稱python
淺綠=命令參數mysql
淺藍=選項git
紫色=目錄github
系統環境:CentOS 5.5 x86_64web
學web.py框架時寫的一個很小很簡陋的小實例,效果如圖:sql
運行環境 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.pyide
- #!/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='')
根據本身狀況自行定義。ui
配置好環境 運行 run.py 而後經過8080端口就能夠訪問了。