flask shortly例子

(115) 2024-04-09 19:01:01

1.shortly的目录

目录结构有static目录,

保存style.class文件

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

以及文件shortly.py文件。

flask shortly例子 (https://mushiming.com/)  第1张

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.运行效果

flask shortly例子 (https://mushiming.com/)  第2张

后天运行结果为:\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 -

 

THE END

发表回复