python-web.py開發入門(推薦) 原

淺顯易懂,推薦 

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

 一、課程介紹

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

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

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

安裝webpy

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

輸入運行文件命令。(我這裏pycharm2018.2不曉得爲嘛terminal調整不了字間距,而且文件路徑的/都變了,文字顏色也不曉得哪裏能改,其他地方的顯示都正常也能修改,這裏除了文字大小能調整外,別的都不起作用。TVT)

返回0.0.0.0:8080

瀏覽器訪問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

name

一些前課程前端代碼

<!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文件

測試返回1.html內容

2.web.py學習

(1)url映射

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)請求處理

A.請求參數獲取

添加參數獲取

<!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

B.請求頭獲取

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)相應處理

A.模板文件讀取

pip install pymysql #python3

定義模板主目錄

添加文章路徑

無參數render

B.獲取數據結果

建立數據庫信息

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

首頁跳轉

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

三、分析

初始化數據庫表結構腳本

classname必須有

密碼生成一個銘文的

代碼分析

首頁內容

相關

查找的github上相關代碼:https://github.com/sunchen009/chouyangblog

完結

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章