Python環境下使用web.py開發

安裝web.py

mac安裝直接在終端中輸入 一下代碼

sudo pip install web.py

安裝成功

Successfully installed web.py-0.38

如圖
這裏寫圖片描述

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

運行結果 如下:
這裏寫圖片描述

web.py完整web

main.py

#coding=utf-8
import web
#url 匹配 帶組的URL 映射到hello 方法
urls = (
    '/index', 'index',
    '/blog/\d+', 'blog',
    '/(.*)', 'hello'
)
app = web.application(urls, globals())


class hello:
    def GET(self, name):
       return open(r'hell.html','r').read()
class index:
    def GET(self):
        return 'index mothod'

class blog:
    def GET(self):
        return 'blog method'
    def POST(self):
        return 'blog post mothod'

if __name__ == "__main__":
    app.run()

hell.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>我是標題</title>
</head>
<body>

<h1>我是網頁</h1>
</body>
</html>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章