python-web.py 入门介绍
内容来源:webpy.org
介绍:
1、python中web.py 是一个轻量级Python web框架,它简单而且功能强大。web.py是一个开源项目。
2、安装很简单:pip install web.py
3、URL处理
例:hello word
import web
#模糊匹配
urls = ("/.*", "hello")
app = web.application(urls, globals())
class hello:
def GET(self):
#给页面返回值(响应结果)
return 'Hello, world!'
if __name__ == "__main__":
app.run()
#测试:
请求地址:
http://localhost:8080/
请求方式GET
总结:以上是一个最简单的应用web.py的例子,介绍了一种URL处理,并且返回值直接是return 一个字符串的简单形式,下面详细进行说明web.py的强大之处:
1、URL处理支持三种形式:
urls = (
#精确匹配
'/selectDB', 'selectDb',
# 精确匹配
'/index', 'index',
# 模糊的不带组的
'/blog/\d+', 'blog',
# 带组的模糊匹配
'/(.*)', 'hello'
) 这个类名为上面的URL:'/blog/\d+', 'blog',相关联的
class blog(object):
def GET(self):
print 'GET'
query = web.input()
return query def POST(self):
print "Post"
query = web.input()
print '用户名:', query['username'], '密码:', query['password']
return query
上面的请求支持GET和POST
例:模拟POST请求:需要写一个form表单提交
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>hello</title>
</head>
<body>
<h1>POST</h1>
<form action="/blog/123" method="post">
用户名:<input type="text" id="username" name="username" value=""/>
密 码:<input type="password" id="password" name="password" value=""/><br>
<input type="submit" value="submit" />
</form>
</body>
</html>
总结:此部分重点需要了解web.input()的使用,用于接收请求的参数(POST/GET),
2、下面再继续学习从数据库查询出结果,返回给页面的例子:
import web
import MySQLdb
print "Web.py 练习"
urls = ( '/selectDB', 'selectDb', )
app = web.application(urls, globals())
# 响应使用模板的方式
render = web.template.render('templates')
class selectDb(object): def GET(self):
conn = MySQLdb.connect(
host='localhost',
port=3306,
user='root',
passwd='root',
db='cf_sjjy',
charset='utf8'
)
cursor = conn.cursor()
cursor.execute("select CertId,Name from zhengxin_hit_rules")
rs = cursor.fetchall()
cursor.close()
conn.close()
print rs
return render.article(rs)
if __name__ == "__main__": app.run() HTML页面如下:
$def with(rs)
<html lang="en">
<head>
<meta charset="UTF-8">
<title>数据库查询</title>
</head>
<body>
<h1>数据库查询</h1>
<ul>
$for v in rs:
<li>$v[0] => $v[1]</li>
</ul>
</body>
</html>
总结:这个例子说明了如何把结果list返回到界面展示,使用到了模板的应用,需要创建一个模板文件夹
templates里面创建一个article.html
内容如上,通过return render.article(rs)给页面传值,页面最上面$def with(rs)接收值,然后通过for循环取值。
$for v in rs:
<li>$v[0] => $v[1]</li>
可以掌握到的内容:
1、web.py大体架构和概念
2、web.py的url处理机制
3、发送请求POST/GET
4、数据库操作mysql
5、模板的使用(article.html)
5、响应值传递,解析和展示处理( return render.article(rs) $def with(rs)、
$for v in rs:
<li>$v[0] => $v[1]</li>
)主要是这三个的理解。
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】python web py入门-5-请求处理(下)
前面一篇,我们演示了如何获取GET和POST请求的参数信息,这篇我们介绍如何获取请求的头部信息,这个方法我们在前面一篇文章已经给出了.直接来看一个例子,首先,我们在hello.py文件新增一个方法,用 ...
- Python Web自动化测试入门与实战,从入门到入行
Python Web自动化测试入门与实战 购买地址 · 京东:https://item.jd.com/69239480564.html 天猫:https://detail.tmall.com/it ...
- Python 基于python操纵redis入门介绍
基于python操纵redis入门介绍 by:授客 QQ:1033553122 测试环境 redis-3.0.7 CentOS 6.5-x86_64 python 3.3.2 基于Python操作R ...
- web.py入门
官网介绍: web.py is a web framework for Python that is as simple as it is powerful. web.py is in the pub ...
- 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安装使用
官方首页:http://webpy.org/) 它的源代码非常整洁精干,学习它一方面可以让我们快速了解python语法(遇到看不懂的语法就去google),另一方面可以学习到python高级特性的使用 ...
- python web.py实现简单的get和post请求
使用web.py框架,实现简单的get和post请求: py文件名:mytest.py import web urls = ( '/', 'hello' ) app = web.application ...
随机推荐
- Java学习之J2EE
什么是J2EE 本文摘抄于其他博文. 什么是J2EE 一.准备篇 1 什么是J2EE?它和普通的Java有什么不同?答:J2EE全称为Java2 Platform Enterprise Editio ...
- Web前端的路该怎么走?很迷茫
“路漫漫其修远兮,吾将上下而求索”,这句出自<离骚>. 虽然端午已过,但是还是不影响一个程序员想表(zhuang)达(boy)自己此刻心情的冲动. 偶然路过同事旁边,不(tou)小(kan ...
- [原创] 利用前端+php批量生成html文件,传入新文本,输出新的html文件
本人因为要想自己写个小说网站练练手,在其中遇到的一些问题,将其解决方法总结出来,例如: 1:小说网站存储了大量的小说,每个小说主页都很相似,url不同,不是使用的history属性改写的,所以如果人工 ...
- Google云平台技术架构
Google Cloud 设计原理: 1.分布式文件系统: Google Distributed File System(GSF) 为了满足Google迅速增长的数据处理需求,我们设计并实现了G ...
- win7双系统安装openSUSE13.2解决【引导加载器安装期间出错】问题
原始日期:2015-08-17 14:16 昨晚不知道哪根筋不对,突然想装一个liunx系统,与win7形成双系统,最终选定openSUSE13.2,想想以前也安装过Ubuntu,应该差不多,所以直接 ...
- github用法小结
共享仓库 bare 裸仓库 生成裸仓库时必须以.git结尾. 仓库就相当于一个服务器 ### 创建远程仓库 1. 创建以.git结尾的目录mkdir repo.git 2 ...
- mysql 修改表结构的字段名
alter table domains change STATUS status tinyint(1) not null;
- 常用html标签的只读写法
<a href="baidu.com" onclick="event.returnValue=false;">百度</a> a链接的只读 ...
- json字符串转成数组
$hour_23 json_decode($hour_23,true);//(第二个参数为true的时候)
- 一张图告诉你 canvas 中的 miterLimit 代表着什么
一图胜千言, 图中有一条路径path, 沿着路径描了一条宽度为 width 的边, miterLimit 代表的是, 比例 ab/ac, 其中ac的长度为 1/2 width 来看 mdn 上的描述, ...