1. 程式人生 > >Python環境下使用web.py開發

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>