一、简单调用
1.安装ActivePython-2.7.2.5-win32-x86.msi
2.http://webpy.org下载web.py-0.36.tar.gz,解压
3.转到解压目录web.py-0.36执行python setup.py install
4.测试代码
code.py
import web
urls = (
'/', 'index'
)
class index:
def GET(self):
return "Hello, world!"
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
5.运行程序
E:\python\webpy>python code.py
http://0.0.0.0:8080/
6.IE访问
http://localhost:8080,提示“Hello,world!”
二、使用模板
1.ct1.py
import web
render = web.template.render('templates/')
urls = (
'/(.*)', 'index'
)
class index:
def GET(self,name):
return render.index(name)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
2.模板html文件
程序ct1.py目录下建立templates目录,进入templates目录建立index.html文件,内容如下:
$def with (name)
$if name:
I just wanted to say <em>hello</em> to $name.
$else:
<em>Hello</em>, world!
3.执行测试
1).进入命令行界面
转入程序ct1.py目录下,执行python ct1.py
E:\python\webpy>python ct1.py
http://0.0.0.0:8080/
2).打开IE界面,输入“http://localhost:8080/Hellen”
IE输出"I just wanted to say hello to Hellen."
三、使用数据库
1.安装cx_Oracle模块
首先安装cx_Oracle模块,确认可以成功访问数据库
2.命令行测试
>>> import web
>>> db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns<远程服务名tnsnames.ora等号左边名字>')
>>> rows = db.select('test')
0.0 (3): SELECT * FROM test
>>> for row in rows:
... print row
...
<Storage {'ID': 1, 'IP': '192.168.1.11 '}>
<Storage {'ID': 2, 'IP': '192.168.1.12 '}>
<Storage {'ID': 3, 'IP': '192.168.1.13 '}>
<Storage {'ID': 4, 'IP': '192.168.1.14 '}>
<Storage {'ID': 5, 'IP': '192.168.1.15 '}>
<Storage {'ID': 6, 'IP': '192.168.1.16 '}>
>>>
3.准备工作
1)创建表
CREATE TABLE test (
id number(20) primary key not null,
title varchar2(20),
created timestamp default current_timestamp, --默认值为当前时间戳
done char(1) check(done in ('N','Y'))
);
2)修改done列默认值为‘Y’
alter table test MODIFY DONE default 'Y'; -- done列默认值为‘Y’
3)创建序列
CREATE SEQUENCE test_sequence
INCREMENT BY 1
START WITH 1
NOMAXvalue
NOCYCLE
CACHE 10;
3)建立触发器
保证test表id字段插入新记录时自动增加 -- id列默认值为‘TEST_SEQUENCE.Nextval’
create or replace trigger tr_user
before insert on test
for each row
begin
select TEST_SEQUENCE.Nextval into :new.id from dual;
end;
结果: 只保留title一个字段无默认值,用户插入时允许只指定一个title字段,其它为默认
4)插入记录
INSERT INTO test(id,title,done) VALUES (TEST_SEQUENCE.Nextval,'Learn web.py','Y');
INSERT INTO test(id,title,done) VALUES (TEST_SEQUENCE.Nextval,'Python web.py','Y');
insert into test (title) values('Python Web 框架')
5)命令行测试连接
>>> import web
>>> db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns')
>>> rows = db.select('test')
0.0 (1): SELECT * FROM test
>>> for row in rows:
... print row
...
<Storage {'TITLE': 'Learn web.py', 'DONE': 'Y', 'ID': 2, 'CREATED': datetime.dat
etime(2013, 4, 8, 13, 1, 52, 685605)}>
>>>
>>> db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns')
>>> rows = db.query('select * from test')
0.0 (1): select * from test
>>> for row in rows:
... print row['ID'],row['TITLE']
...
2 Learn web.py
3 Python web.py
4 Python Web 框架
5 Web.py 框架
>>>
4.服务端程序
cd1.py
import web
db = web.database(dbn='oracle',user = 'test' ,pw = 'test',db='testtns')
render = web.template.render('templates/')
urls = (
'/', 'index'
)
class index:
def GET(self):
rows = db.select('test')
return render.index(rows)
if __name__ == "__main__":
app = web.application(urls, globals())
app.run()
5.index.html
程序cd1.py目录下建立templates目录,进入templates目录建立index.html文件,内容如下:
$def with (rows)
<ul>
$for row in rows:
<li> $row['TITLE'] </li>
</ul>
打开IE访问“http://localhost:8080/”
- Learn web.py
- Python web.py
- Python Web 框架
- Web.py 框架