python-web.py开发入门(推荐)

浅显易懂,推荐 

课程地址:https://www.imooc.com/learn/753

 一、课程介绍

26ec9d9991dc3a124fd50b83589b4723f20.jpg

9d9931ac8d58c0233829957c4595059f4d9.jpg

98074f8e726f4605d9c55fb46c0dc99c36f.jpg

e87b0490d91f6377e38e10fac16c2787983.jpg

web.py官网:http://webpy.org

161ae0d336b94eb65f62b1f34df151f49d6.jpg

版本基本不会更新,作者去世

pip install web.py #在python2.7环境下
pip3 install web.py--0.40-dev1 #在python3环境下

安装webpy

4365435f4900c4db04113debb22ec28e15f.jpg

9cdc8588ab4513c9dce97080cca6d693254.jpg

f1c50fce313fb1fb5fdb2005168cb65763f.jpg

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()

新建hello.py

python hello.py

8a139934b6014af85cb6455ca3d920e2315.jpg

输入运行文件命令。(我这里pycharm2018.2不晓得为嘛terminal调整不了字间距,而且文件路径的/都变了,文字颜色也不晓得哪里能改,其他地方的显示都正常也能修改,这里除了文字大小能调整外,别的都不起作用。TVT)

返回0.0.0.0:8080

f02ebcdb7a91011917fcb83aaf9f24a5a3c.jpg

浏览器访问127.0.0.1:8080

——————————————————————————————————————

PS:0.0.0.0与127.0.0.1区别:https://blog.csdn.net/searchwang/article/details/33804265

0.0.0.0

严格说来,0.0.0.0已经不是一个真正意义上的IP地址了。它表示的是这样一个集合:所有不清楚的主机和目的网络。这里的“不清楚”是指在本机的路由表里没有特定条目指明如何到达。对本机来说,它就是一个“收容所”,所有不认识的“三无”人员,一律送进去。如果你在网络设置中设置了缺省网关,那么Windows系统会自动产生一个目的地址为0.0.0.0的缺省路由。

127.0.0.1

本机地址,主要用于测试。用汉语表示,就是“我自己”。在Windows系统中,这个地址有一个别名“Localhost”。寻址这样一个地址,是不能把它发到网络接口的。除非出错,否则在传输介质上永远不应该出现目的地址为“127.0.0.1”的数据包。

127.0.0.1与本地IP:127.0.0.1是自环地址,也就是回路地址,PING通了说明网卡没有问题,因此发往127的消息不会出网卡。

————————————————————————————————————————

二、web.py开发

1.demo

46305c4a6f5b8fb542aefd6179ab0bcfb4a.jpg

16be3afd3de9948a8aa5fc379dcc7233e9e.jpg

name

74a976d4bd75e651fa3156cea7f570002d4.jpg

e5db46431ec70bdc177a0a99ee5d1d15353.jpg

一些前课程前端代码

39759ac0ad912a6d1d1271971e5da4a4c67.jpg

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>hello</title>
    <style>
        div p{color: #f00;}
        .py{font-size: 40px}
        #l1{width: 200px;font-size: 40px}
    </style>
</head>
<body>
    <h1>hello</h1>
    <div>World</div>
    <p class="py">python</p>
    <label id ='l1'>test</label>
    <div>
        <a href="javascript:void(0)" onclick="javascript:show_text('l1','my frist js')">my frist js</a>
        <a href="javascript:void(0)" onclick="javascript:show_text('l1','hello python')">hello python</a>
        <a href="javascript:void(0)" onclick="javascript:show_color('l1','#f00')">red</a>
        <a href="javascript:void(0)" onclick="javascript:show_color('l1','#0f0')">green</a>
    </div>
    <script>
        function show_text(id, text) {
            document.getElementById((id)).innerHTML=text
        }
        function show_color(id, color) {
            document.getElementById(id).style.color=color
        }
    </script>
</body>
</html>

新建1.html文件

9901176b6049fbb87afeb334bb8b5e4265f.jpg

测试返回1.html内容

48af9d59fa0ba2362db89a49f1c3114e3be.jpg

2.web.py学习

d2b7d807cdcc4e1d4688bd1f718318e89fc.jpg

(1)url映射

7fb2d733b126830511738aaeb2ecc636233.jpg

d20fbc86ef73f9aaa991251d02025a732cb.jpg

ccee8cce12d5793b72cc8cc2f723c57d6d6.jpg

import web
urls = (
    '/index', 'index', #精确匹配
    '/blog/\d+','blog', #模糊匹配-带组
    '/(.*)','hello' #模糊匹配-不带组
) # 注意:url里有多个使用模糊匹配,模糊匹配范围大的要放在小的后面
app = web.application(urls, globals())
class index:
    def GET(self):
        return 'index method'
class blog:
    def GET(self):
        return 'blog method'
    def POST(self):
        return 'blog post method'
class hello:
    def GET(self, name):
        return 'hello' + name
if __name__ == "__main__":
    app.run()

hello.py获取路径

(2)请求处理

04fc03691831eafc18e2cb773ede9f89bb3.jpg

0549174a52d0c1e50d912c4fbf4ee323c25.jpg

A.请求参数获取

731963ee9645deca189f106fcf2057fd162.jpg

添加参数获取

665df789acaad4aee212d3b36af9459e947.jpg

<!DOCTYPE html>
<html lang="en">
<head>
    <title>hello</title>
</head>
<body>
    <h1>POST</h1>
    <form action="/blog/123" method="POST">
        <input type="username" name="username" value="">
        <input type="password" name="password" value="">
        <input type="submit" value="submit">
    </form>
</body>
</html>

新建2.html

import web
urls = (
    '/index', 'index', #精确匹配
    '/blog/\d+','blog', #模糊匹配-带组
    '/(.*)','hello' #模糊匹配-不带组
) # 注意:url里有多个使用模糊匹配,模糊匹配范围大的要放在小的后面
app = web.application(urls, globals())
class index:
    def GET(self):
        query = web.input()
        return query
class blog:
    def GET(self):
        query = web.input()
        return query
    def POST(self):
        data = web.input()
        return data
class hello:
    def GET(self, name):
        return open(r'2.html').read()
if __name__ == "__main__":
    app.run()

hello.py

e985872be4ffb905260b98028cf776069d4.jpg

77ad25b1cba851baf049da9af4a1b720966.jpg

8a0422605603d65aca63cf890dd0fce2f53.jpg

3d5c6f42f90ff17cb0412393f08ac9aa35e.jpg

B.请求头获取

d4cd18d78a0d7da9d8bfbef40bbaf68eebe.jpg

5e2c99832839cd19c438365b47bccff1e36.jpg

c727ec332a528b20a138aa6fbc1a1d98a3f.jpg

import web
urls = (
    '/index', 'index', #精确匹配
    '/blog/\d+','blog', #模糊匹配-带组
    '/(.*)','hello' #模糊匹配-不带组
) # 注意:url里有多个使用模糊匹配,模糊匹配范围大的要放在小的后面
app = web.application(urls, globals())
class index:
    def GET(self):
        query = web.input()
        return query
class blog:
    # def GET(self):
    #     query = web.input()
    #     return query
    def GET(self):
        return web.ctx.env
    def POST(self):
        data = web.input()
        return data
class hello:
    def GET(self, name):
        return open(r'2.html').read()
if __name__ == "__main__":
    app.run()

(3)相应处理

bca3cc3871e98476be30a4093012bb1dada.jpg

A.模板文件读取
pip install pymysql #python3

6c4adfa856714cad4bdacc9c6613cbddb24.jpg

定义模板主目录

4a53248234a22c3c17b01e9c8f77354b649.jpg

添加文章路径

8e0c69003c6aaea11b237b556c69f3f0a3d.jpg

无参数render

1da3832e357b85ca3e700f4c2f8f3a2550c.jpg

d40c5ab579fbbb74712b1cb6123bcd3ed55.jpg

84ffe0aaea71d20a5e7d0e19cfeba1f0c02.jpg

bd8a62ae52109860f1b83b7d6f10cfc6d90.jpg

B.获取数据结果

48b12eaee4f24d02bce6ea0f98c1e4f1279.jpg

建立数据库信息

c3b3ffb75eb2747d3928783a65006d1dc80.jpg

868c02f0a592bed2cdfc12e0d394dfae3f6.jpg

23cae881dac64dce4754fe57a5c2fbc328b.jpg

3154ff15c33bac02e1a52dce6829b4cacdb.jpg

$def with(r)
<html lang="en">
<head>
    <title>article</title>
</head>
<body>
    <h1>文章列表</h1>
    <ul>
        $for l in r:
            <li>$l.get('aid') => $l.get('title')</li>
    </ul>
</body>
</html>

新建article.html

import pymysql #python3
pymysql.install_as_MySQLdb()
class article:
    def GET(self):
        conn =pymysql.connect(host='localhost',user='root',passwd='root',db='py_webpy',cursorclass=pymysql.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute('select * from articles')
        r = cur.fetchall()
        cur.close()
        conn.close()
        print(r)
        return  render.article(r)

e2f3ae69993c91fafe9332e26985a09dcf6.jpg

首页跳转

d20f3830d805c5b6286e87863d6eb9c9896.jpg

import web
# import MySQLdb #python2
# import MySQLdb.cursors
import pymysql #python3
pymysql.install_as_MySQLdb()
render = web.template.render('templates') #定义模板
urls = (
    '/article','article',
    '/index', 'index', #精确匹配
    '/blog/\d+','blog', #模糊匹配-带组
    '/(.*)','hello' #模糊匹配-不带组
) # 注意:url里有多个使用模糊匹配,模糊匹配范围大的要放在小的后面
app = web.application(urls, globals())
class index:
    def GET(self):
        query = web.input()
        # return query
        return web.seeother('/article') #跳转到文章页面,也可以跳转外部网站
class blog:
    # def GET(self):
    #     query = web.input()
    #     return query
    def GET(self):
        return web.ctx.env
    def POST(self):
        data = web.input()
        return data
class hello:
    def GET(self, name):
        # return open(r'2.html').read()
        # return render.hello2()
        return render.hello2(name) #引用模板-带参数
class article:
    def GET(self):
        conn =pymysql.connect(host='localhost',user='root',passwd='root',db='py_webpy',cursorclass=pymysql.cursors.DictCursor)
        cur=conn.cursor()
        cur.execute('select * from articles')
        r = cur.fetchall()
        cur.close()
        conn.close()
        print(r)
        return  render.article(r)
if __name__ == "__main__":
    app.run()

hello.py

三、分析

e9f0bfd217d8664e5443e98d9a78613e149.jpg

fa4b85e6a1bfc2100e9ce3cc5548939abf3.jpg

初始化数据库表结构脚本

4c70c50113b6b21958db277147a7cc836c4.jpg

classname必须有

af427500d3a0bf4057c42a221708aa7395b.jpg

密码生成一个铭文的

09d1aea570c6bbeb58d8da61ad6da91ea93.jpg

56f60944f8b374e340296b7716bb59b83b7.jpg

代码分析

14e94e6cb4ef7ad7181548e387cb9503e6a.jpg

首页内容

2995083d99e92ca138d550b6e3a1b039974.jpg

相关

查找的github上相关代码:https://github.com/sunchen009/chouyangblog

完结

转载于:https://my.oschina.net/u/3018050/blog/1944106

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值