当前位置 博文首页 > python web框架的总结

    python web框架的总结

    作者:小妮浅浅 时间:2021-05-07 18:27

    1、Django

    Django可能是最具代表性的Python框架,是遵循MMVC结构模式的开源框架。其名字来自DjangoReinhardt,法国作曲家和吉他演奏家,很多人认为他是历史上最伟大的吉他演奏家。位于堪萨斯州的Lawrence城市的LawrenceJournal-World报社有两名程序员,AdrianHolovaty和SimonWillison,他们在2003年开发了Django,为报纸开发了网络程序。

    2、TurboGears

    TurboGears是SQLAlchemy、WebOb、Repoze、Genshi等着名Python项目构筑的框架。从某种意义上说,TurboGears是将多个已经建立的开放平台粘在一起。和Django一样,采用MVC结构。最近还包含了最小模式,可以作为微框架。

    3、Flask

    Flask是一个基于Jinja2和Werkzeug的python微框架,类似于其他框架。是BSD授权的,有少量限制的免费软件许可。使用Flask的网站包括领英LinkedIN和Pinterest。

    知识点扩展:

    基于socket

    自己处理请求

    #!/usr/bin/env python3
    #coding:utf8
    import socket
    def handle_request(client):
     #接收请求
     buf = client.recv(1024)
     print(buf)
     #返回信息
     client.send(bytes('<h1>welcome liuyao webserver</h1>','utf8'))
    def main():
     #创建sock对象
     sock = socket.socket()
     #监听80端口
     sock.bind(('localhost',8000))
     #最大连接数
     sock.listen(5)
     print('welcome nginx')
     #循环
     while True:
     #等待用户的连接,默认accept阻塞当有请求的时候往下执行
     connection,address = sock.accept()
     #把连接交给handle_request函数
     handle_request(connection)
     #关闭连接
     connection.close()
    if __name__ == '__main__':
     main()
    

    基于wsgi

    WSGI,全称 Web Server Gateway Interface,或者 Python Web Server Gateway Interface ,是为 Python 语言定义的 Web 服务器和 Web 应用程序或框架之间的一种简单而通用的接口。自从 WSGI 被开发出来以后,许多其它语言中也出现了类似接口。

    WSGI 的官方定义是,the Python Web Server Gateway Interface。从名字就可以看出来,这东西是一个Gateway,也就是网关。网关的作用就是在协议之间进行转换。

    WSGI 是作为 Web 服务器与 Web 应用程序或应用框架之间的一种低级别的接口,以提升可移植 Web 应用开发的共同点。WSGI 是基于现存的 CGI 标准而设计的。

    很多框架都自带了 WSGI server ,比如 Flask,webpy,Django、CherryPy等等。当然性能都不好,自带的 web server 更多的是测试用途,发布时则使用生产环境的 WSGI server或者是联合 nginx 做 uwsgi 。

    python标准库提供的独立WSGI服务器称为wsgiref。

    #!/usr/bin/env python
    #coding:utf-8
    #导入wsgi模块
    from wsgiref.simple_server import make_server
    
    def RunServer(environ, start_response):
     start_response('200 OK', [('Content-Type', 'text/html')])
     return [bytes("welcome webserver".encode('utf8'))]
    
    if __name__ == '__main__':
     httpd = make_server('', 8000, RunServer)
     print ("Serving HTTP on port 8000...")
     httpd.serve_forever()
     #接收请求
     #预处理请求(封装了很多http请求的东西)
    
    
    js
    下一篇:没有了