Python——Web.py詳解
ubuntu安裝Web.py
sudo pip install web.py
測試代碼:
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()
类似于flask模板,这里也可以使用文件读写返回html内容:
import web urls = (
'/(.*)','hello'
)
app = web.application(urls,globals()) class hello:
def GET(self,name):
return open(r'index.html','r').read() if __name__=='__main__':
app.run()
URL映射:
完全匹配: /index
模糊匹配 /post/\d+
带组匹配 /post/(\d+)
import web urls = (
'/blog/\d+','blog',
'/index','index',
'/(.*)','hello',
)
app = web.application(urls,globals()) class index:
def GET(self):
return 'index' class blog:
def GET(self):
return 'blog' class hello:
def GET(self,name):
return 'hello' if __name__=='__main__':
app.run()
请求处理:
请求参数获取 : web.input()
hello.html源码:
<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<title>User Login</title>
</head> <body>
<div>
<h6>User Login</h6>
</div>
<form action="/blog/123" method="POST">
<h6>用戶名:</h6>
<input type="text" name="username"><br>
<h6>密碼:</h6>
<input type="password" name="password"><br>
<input type="submit" name="submit"><br>
</form>
</body>
</html>
app.py源码:
import web urls = (
'/blog/\d+','blog',
'/index','index',
'/(.*)','hello',
)
app = web.application(urls,globals()) class index:
def GET(self):
r = web.input()
return r class blog:
def POST(self):
r = web.input()
return r class hello:
def GET(self,name):
return open(r'hello.html','r').read() if __name__=='__main__':
app.run()
运行结果:



请求头获取 : web.ctx.env
app.py代码如下,hello.html代码如上相同
import web urls = (
'/blog/\d+','blog',
'/index','index',
'/(.*)','hello',
)
app = web.application(urls,globals()) class index:
def GET(self):
return web.ctx.env class blog:
def POST(self):
r = web.input()
return r class hello:
def GET(self,name):
return open(r'hello.html','r').read() if __name__=='__main__':
app.run()
运行结果:
响应处理:
模板文件读取:render.index("参数")
在py文件同目录下要创建templates文件夹,存放模板。
index是指定模板的名称,参数是html中是所需参数。
import web
render = web.template.render("templates")
urls = (
'/blog/\d+','blog',
'/index','index',
'/(.*)','hello',
)
app = web.application(urls,globals())
class hello:
def GET(self,name):
return render.hello()
if __name__=='__main__':
app.run()
采用模板就可以不用之前所用的open(r'hello.html','r').read()了。
结果数据获取:model.select("sql")
参考我之前的文章http://www.cnblogs.com/LexMoon/p/Flask_6.html
URL跳转 :web.seeother("/")
import web urls = (
'/(.*)','hello',
)
app = web.application(urls,globals()) class hello:
def GET(self,name):
return web.seeother('http://www.cnblogs.com/LexMoon/') if __name__=='__main__':
app.run()
Python——Web.py詳解的更多相关文章
- 【Python】【web.py】python web py入门-4-请求处理(上)
python web py入门-4-请求处理(上) 2017年09月05日 23:07:24 Anthony_tester 阅读数:2907 标签: webpy入门请求处理 更多 个人分类: Pyth ...
- python web.py安装使用
官方首页:http://webpy.org/) 它的源代码非常整洁精干,学习它一方面可以让我们快速了解python语法(遇到看不懂的语法就去google),另一方面可以学习到python高级特性的使用 ...
- mac OS X 配置Python+Web.py+MySQLdb环境
MAC默认支持Python 2.7所以不用安装. 1.安装pip sudo easy_install pip 2.安装Web.py sudo pip install Web.py 3.安装MySQLd ...
- Python Web.py
安装Web.py root@bt:~# sudo pip install web.py Downloading/unpacking web.py Downloading web.py-0.37.tar ...
- python web.py实现简单的get和post请求
使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...
- python web py安装与简单使用
web.py是一个轻量级的python web框架,简单而且功能强大.相对flask和Django,web.py更适合初学者来学习和了解web开发的基础知识. 安装: pip install we ...
- 【Python】【Web.py】python web py入门-5-请求处理(下)
前面一篇,我们演示了如何获取GET和POST请求的参数信息,这篇我们介绍如何获取请求的头部信息,这个方法我们在前面一篇文章已经给出了.直接来看一个例子,首先,我们在hello.py文件新增一个方法,用 ...
- python web.py操作mysql数据库,实现对数据库的增删改查操作
使用web.py框架,实现对mysql数据库的增删改查操作: 该示例代码中连接的是本地数据库testdb,user表,表结构比较简单,只有两个字段:mobile和passwd,类型均为字符型 实际应用 ...
- 树莓派上搭建基于Python+web.py+fastcgi+lighttpd的网站
最近在网上淘了一个树莓派,什么是树莓派?这里是他的官方网站你可以去看看. 简单的说就是一块使用了ARM11的CPU,具有256MB或512MB内存的具有两个USB接口,一个RJ45接口,HDMI输出和 ...
随机推荐
- Java高级工程师——面试总结
面试技巧 1.背熟你的简历 原因:面试的第一个问题,一般都是让你简单介绍下你自己,或者介绍一下你最近的项目,而一个面试者,如果连自己的简历都无法熟知,对里面提到的项目.技术都无法描述清楚的话,我想没有 ...
- python_20_socket
什么是socket? -- 通过各种协议,发送和接收数据,实现网络通信 -- 在python3中,网络发送只能发二进制数据 OSI七层模型是什么? 应用 表示 会话 传输 网络 ...
- 企业级分布式存储应用与实战-mogilefs实现
Mogilefs是什么 MogileFS是一个开源的分布式文件存储系统,由LiveJournal旗下的Danga Interactive公司开发.Danga团队开发了包括 Memcached.Mogi ...
- 【转】城市CORS系统建设
随着GPS技术的飞速进步和应用普及,它在城市测量中的作用已越来越重要.当前,利用多基站网络RTK技术建立的连续运行卫星定位服务综合系统(Continuous Operational Reference ...
- WindowXp-Windows7-Windows运行命令(转)
Win7里面按 Win+R 呼出运行界面,一下是它的一些常用命令: 1.cleanmgr: 打开磁盘清理工具 2.compmgmt.msc: 计算机管理 3.conf: 启动系统配置实用程序 4.ch ...
- Div+Css画太极图源代码
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>D ...
- ABP 多租户 对应多数据库 租户启动报错
什么是多租户? “软件多租户是指一个软件体系结构,其中一个软件实例在一个服务器上运行,并为多个租户提供服务*租户是一组共享具有软件实例特定权限的公共访问权限的用户. 架构中,软件应用程序旨在为每个租户 ...
- XSD详解一 - 基本概念
本分类下的文章主要是对W3School的文档进行整理:http://www.w3school.com.cn/x.asp XML Schema 是基于 XML 的 DTD 替代者. XML Schema ...
- AI 学习新的开始
推荐入门学习 http://www.cnblogs.com/subconscious/p/6240151.html
- DataGrid 拖动 附加属性类
项目需要实现一个DataGrid拖动排序,于是参考网上一些资源然后,修改了下实现了一个附加属性类,如下 使用方法 <DataGrid x:Name="shareGrid" t ...