flask shortly例子

1.shortly的目錄

目錄結構有static目錄,

保存style.class文件

templates目錄是jinja2的目錄,裏面保存有404.html和layout.html、new_url.html的和short_link_details.html的文件

以及文件shortly.py文件。

2.代碼分析

2.1 shortly.py的文件

 

代碼入口爲:

//通過導入werkzeug.serving 進行 ,服務端口爲端口爲5000

if __name__ == "__main__":
    from werkzeug.serving import run_simple

    app = create_app()
    run_simple("127.0.0.1", 5000, app, use_debugger=True, use_reloader=True)

2.2 文件進入創建create_app

//可以看出是啓用本地的redis的服務,服務端口爲6379服務,啓用服務,通過判斷是否用with_static的boolean值,如果是with_static是true的值,去訪問static靜態目錄的靜態值,相當於緩存

def create_app(redis_host="localhost", redis_port=6379, with_static=True):
    app = Shortly({"redis_host": redis_host, "redis_port": redis_port})
    if with_static
        app.wsgi_app = SharedDataMiddleware(
            app.wsgi_app, {"/static": os.path.join(os.path.dirname(__file__), "static")}
        )
    return app

2.3 通過wsgi_app 來創建一個arequst的服務

def wsgi_app(self, environ, start_response):
    request = Request(environ)
    response = self.dispatch_request(request)
    return response(environ, start_response)

 

3.運行效果

後天運行結果爲:\python.exe D:/IdeaProjects/werkzeug-master/examples/shortly/shortly.py
 * Restarting with stat
 * Debugger is active!
 * Debugger PIN: 189-329-335
 * Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
127.0.0.1 - - [11/Mar/2020 17:12:24] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:12:25] "GET /static/style.css HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:12:26] "GET /favicon.ico HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:12:39] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:13:17] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:13:27] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:13:44] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:13:51] "GET /layout.html HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:14:17] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:14:46] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:14:50] "GET /shortly HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:15:26] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:15:31] "POST / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:16:36] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:16:36] "GET /1+ HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:16:42] "GET /1 HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:16:42] "GET /shortly.html HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:16:56] "GET /2+ HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:17:01] "GET /1 HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:01] "GET /shortly.html HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:17:11] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:11] "GET /2+ HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:14] "GET /2 HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:14] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:21] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:21] "GET /b'2'+ HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:17:26] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:34] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:34] "GET /3+ HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:36] "GET /3 HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:36] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:45] "POST / HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:45] "GET /b'2'+ HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:17:47] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:17:50] "GET /3 HTTP/1.1" 302 -
127.0.0.1 - - [11/Mar/2020 17:17:50] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:18:03] "GET /layout.html HTTP/1.1" 404 -
127.0.0.1 - - [11/Mar/2020 17:18:07] "GET / HTTP/1.1" 200 -
127.0.0.1 - - [11/Mar/2020 17:18:12] "POST / HTTP/1.1" 200 -

 

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