web.py需要使用python2.X,所以安装python版本2.7.9

web.py 是一个轻量级Python web框架,它简单而且功能强大

web.py安装

安装python

(1)使用pip

pip install web.py

安装的目录Python27\Lib\site-packages

(2) https://github.com/webpy/webpy下载release版本的web.py

下载下来之后,解压,打开cmd,cd到解压目录下,输入

python setup.py install

查看安装是否成功,pip list

web.py 测试

新建hello.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()

进入保存的目录

python hello.py

在浏览器输入http://127.0.0.1:8080/

命令窗口显示

第一部分(‘/’)是一个匹配URL 的正则表达式;第二部分(‘index’)是一个类名,匹配的请求将会被发送过去

若要制定另外的端口使用python code.py 后面添加IP 地址/端口

如:http://192.168.5.239:8080/aa

web.py的输出html页面

import web

urls = (
'/(.*)', 'hello'
)
app = web.application(urls, globals()) class hello:
def GET(self, name):
return open(r'aa.html','r').read() if __name__ == "__main__":
app.run()

aa.html页面是已有的html页面

web.py学习

1.URL映射

  完全匹配

  模糊匹配

  带组匹配

  

import web

urls = (
'/index','index',
'/blog/\d+','blog',
'/(.*)', 'hello'
)
app = web.application(urls, globals()) class index:
def GET(self):
return 'index method' class blog:
def GET(self):
return 'blog method' class hello:
def GET(self, name):
if not name:
name = 'World'
return 'Hello, ' + name + '!' if __name__ == "__main__":
app.run()

 

注:

  范围大的要放在后面 

2.请求处理

  请求参数获取

    web.input()

  请求头获取

    web.ctx.env

hello.py

import web

urls = (
'/index','index',
'/blog/\d+','blog',
'/(.*)', 'hello'
)
app = web.application(urls, globals()) class index:
def GET(self):
query = web.input()
return query class blog:
def POST(self):
data = web.input()
return data class hello:
def GET(self, name):
return open(r'hello.html').read() if __name__ == "__main__":
app.run()

 hello.html

<html>
<head>
<title>hello</title>
<head>
<body>
<form action="/blog/123" method="POST">
<input type="text" name="id" value="" />
<input type="text" name="name" value="" />
<input type="submit" value="submit">
</form>
</body>
</html>

表单提交后

修改代码获取请求头

import web

urls = (
'/index','index',
'/blog/\d+','blog',
'/(.*)', 'hello'
)
app = web.application(urls, globals()) class index:
def GET(self):
query = web.input()
return query class blog:
def POST(self):
data = web.input()
return data
def GET(self):
data1 = web.ctx.env
return data1 class hello:
def GET(self, name):
return open(r'hello.html').read() if __name__ == "__main__":
app.run()

  

3.相应处理

  (1)模板文件读取

    render.index("参数")

hello.py    

import web

render = web.template.render("templates")
urls = (
'/index','index',
'/blog/\d+','blog',
  '/(.*)', 'hello'
)
app = web.application(urls, globals()) class index:
def GET(self):
query = web.input()
return query class blog:
def POST(self):
data = web.input()
return data
def GET(self):
data1 = web.ctx.env
return data1 class hello:
def GET(self, name):
return render.hello1(name) if __name__ == "__main__":
app.run()

在hello.py同级目录下,存在templates/hello1.html

hello1.html

$def with(name)
<html>
<head>
<title>hello1</title>
<head>
<body>
<h1>hello1,$name</h1>
<form action="/blog/123" method="POST">
<input type="text" name="id" value="" />
<input type="text" name="name" value="" />
<input type="submit" value="submit">
</form>
</body>
</html>

  

  (2)结果数据处理

    model.select("sql)

安装已编译版本 下载MySQldb ,安装已编译版本

goods.py

import web
import MySQLdb
import MySQLdb.cursors
render = web.template.render("templates")
urls = (
'/goods','goods'
)
app = web.application(urls, globals()) class goods:
def GET(self):
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='mshop_dev',port=3306,charset="utf8",cursorclass=MySQLdb.cursors.DictCursor)
cur=conn.cursor()
cur.execute("select * from mshop_goods limit 5")
r=cur.fetchall()
cur.close()
conn.close()
print r
return render.goods(r) if __name__ == "__main__":
app.run()

goods.html

$def with(r)
<html>
<head>
<meta charset="utf-8" />
<title>goods</title>
<head>
<body>
<h1>商品列表</h1>
<ul>
$for l in r:
<li>$l.get('goods_id'),$l.get('goods_name')=>$l.get('store_name')<li>
</ul>
</body>
</html>

  

  (3)URL跳转

    web.seeother("/")

import web
import MySQLdb
import MySQLdb.cursors
render = web.template.render("templates")
urls = (
'/index','index',
'/goods','goods',
'/(.*)', 'hello'
)
app = web.application(urls, globals()) class index:
def GET(self):
return web.seeother("/goods") class goods:
def GET(self):
conn = MySQLdb.connect(host='127.0.0.1',user='root',passwd='root',db='mshop_dev',port=3306,charset="utf8",cursorclass=MySQLdb.cursors.DictCursor)
cur=conn.cursor()
cur.execute("select * from mshop_goods limit 5")
r=cur.fetchall()
cur.close()
conn.close()
print r
return render.goods(r) class hello:
def GET(self, name):
return web.seeother("http://baidu.com") if __name__ == "__main__":
app.run()

  输入http://127.0.0.1/index跳转到http://127.0.0.1:8080/goods

  输入http://127.0.0.1/hello跳转到https://www.baidu.com/

注:

  web.py的静态文件必须放在static文件夹下面

  web.py并不具备部署网站的能力,因此对于web.py程序只能在本地访问,如果要进行部署必须要使用apache、nginx、lighttped

  通过FastCGI结合lighttpd是web.py,通过该方法可以处理百万次的点击

web.py开发的更多相关文章

  1. python使用web.py开发httpserver,解决post请求跨域问题

    使用web.py做http server开发时,遇到postman能够正常请求到数据,但是浏览器无法请求到数据,查原因之后发现是跨域请求的问题. 跨域请求,就是在浏览器窗口中,和某个服务端通过某个 “ ...

  2. windows下如何快速搭建web.py开发框架

    在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方 ...

  3. 在windows下如何快速搭建web.py开发框架

    在windows下如何快速搭建web.py开发框架 用Python进行web开发的话有很多框架供选择,比如最出名的Django,tornado等,除了这些框架之外,有一个轻量级的框架使用起来也是非常方 ...

  4. 【Python】【web.py】python web py入门-4-请求处理(上)

    python web py入门-4-请求处理(上) 2017年09月05日 23:07:24 Anthony_tester 阅读数:2907 标签: webpy入门请求处理 更多 个人分类: Pyth ...

  5. web.py 安装

    安装 安装web.py, 请先下载: http://webpy.org/static/web.py-0.37.tar.gz 或者获取最新的开发版: https://github.com/webpy/w ...

  6. Python开发WebService:REST,web.py,eurasia,Django

    Python开发WebService:REST,web.py,eurasia,Django 博客分类: Python PythonRESTWebWebServiceDjango  对于今天的WebSe ...

  7. web.py学习心得

    1.注意判断数字时,如果是get传递的参数,一定要用int转换.不然出错. 2.$var 定义时,冒号后的内容不是python内容,需加上$符号.如$var naviId:$naviId. 3.各个模 ...

  8. web.py simpletodo 例子

    一个很好的例子: 许多新手,特别是从 ASP/PHP/JSP 转过来的同学,经常问下面这几个问题: 所有东西都放在一个 code.py 中呀?我有好多东西该如何部署我的代码? 是不是 /index 对 ...

  9. python web.py安装使用

    官方首页:http://webpy.org/) 它的源代码非常整洁精干,学习它一方面可以让我们快速了解python语法(遇到看不懂的语法就去google),另一方面可以学习到python高级特性的使用 ...

随机推荐

  1. 在SQL Server中创建用户角色及授权

    参考文献 http://database.51cto.com/art/201009/224075.htm 正文 要想成功访问 SQL Server 数据库中的数据, 我们需要两个方面的授权: 获得准许 ...

  2. mysql的导入导出工具mysqldump命令详解

    导出要用到MySQL的mysqldump工具,基本用法是: shell> mysqldump [OPTIONS] database [tables] 如果你不给定任何表,整个数据库将被导出. 通 ...

  3. ActiveMQ队列特性:删除不活动的队列(Delete Inactive Destinations)

    方法一 通过 ActiveMQ Web 控制台删除. 方法二 通过 Java 代码删除. ActiveMQConnection.destroyDestination(ActiveMQDestinati ...

  4. C语言 · 成绩查询系统

    抱歉,昨天忘了往博客上更新,今天补上. 成绩查询系统 分值: 21 数学老师小y 想写一个成绩查询系统,包含如下指令: insert [name] [score],向系统中插入一条信息,表示名字为na ...

  5. Flexbox的布局

    http://segmentfault.com/blog/gitcafe/1190000002490633 https://css-tricks.com/snippets/css/a-guide-to ...

  6. linux 获取网卡信息

    sar -n DEV 2 10:41:37 AM IFACE rxpck/s txpck/s rxkB/s txkB/s rxcmp/s txcmp/s rxmcst/s10:41:39 AM eth ...

  7. Web API(六):使用Autofac实现依赖注入

    在这一篇文章将会讲解如何在Web API2中使用Autofac实现依赖注入. 一.创建实体类库 1.创建单独实体类 创建DI.Entity类库,用来存放所有的实体类,新建用户实体类,其结构如下: us ...

  8. 《开发专家 Visual C 开发入行真功夫》笔记

    智能感知的功能,输入 is 后,同时按下Alt + →这两个键就出现了供选择变量.方法.宏等的列表,继续输入 in 后,isInit就出来了. stdafx.h预编译头文件,.h应用程序主头文件,do ...

  9. JSP字符集编码集合

    在这里,我们先说说JSP/Servlet中的几个编码的作用. 在JSP/Servlet 中主要有以下几个地方可以设置编码,pageEncoding="UTF-8".contentT ...

  10. KindEditor使用初步

    KindEditor是一套开源的HTML可视化编辑器,非常适合在CMS.商城.论坛.博客.Wiki.电子邮件等互联网应用上使用,目前在国内已经成为最受欢迎的编辑器之一.目前最新版本为4.1.9,详见h ...