WSGI

WSGI(Web Server Gateway Interface)不是服务器,python模块,框架,API或者任何软件,只是一种规范,描述web server如何与web application通信的规范。

WSGI协议主要包含了server和application两部分
1.web server/getway:接受客户端的请求,并提供并发响应,将request转发给applicaiton,将application返回的response返回给客户端。每个并发中的线程调用web application处理业务逻辑。通常采用C/C++编写,代表有:apache、nginx、IIS。
2.web application/framework:接收server的request,并将处理结果response返回给server。application中可以包含多个栈式的中间件(middlewares),这些中间件必须同时实现server和application,因此可以在WSGI服务器和WSGI服务器之间起到调节作用。

在这里插入图片描述

参考:
https://www.cnblogs.com/-wenli/p/10884168.html

简介

WSGI不是server,不是module,不是framework,不是API,不是任何一种software.它只是一个接口规范,规定server和application如何交流.具体规范可以在PEP 3333查看.

WSGI通过callable object实现,比如function,method,class,或者拥有object.__call__的instance.callable object必须:
1.接收两个positional param:
1)(dictionary) CGI 变量
2)回调函数,来发送HTTP status code/message 和 HTTP headers 去服务端
2.返回(str)response body

Application Interface

# The application interface is a callable object
def application (environ,start_response):# 两个参数,第一个是CGI,第二个是callback
    # Build the response body possibly
    # using the supplied environ dictionary
    response_body = 'Request method: %s' % environ['REQUEST_METHOD']

    # HTTP response code and message
    status = '200 OK'

    # HTTP headers expected by the client
    # They must be wrapped as a list of tupled pairs:
    # [(Header name, Header value)].
    response_headers = [
        ('Content-Type', 'text/plain'),
        ('Content-Length', str(len(response_body)))
    ]

    # Send them to the server using the supplied function
    start_response(status, response_headers)

    # Return the response body. Notice it is wrapped
    # in a list although it could be any iterable.
    return [response_body]

Environment Dictionary

#! /usr/bin/env python

# Python's bundled WSGI server
from wsgiref.simple_server import make_server

def application (environ, start_response):

    # Sorting and stringifying the environment key, value pairs
    response_body = [
        '%s: %s' % (key, value) for key, value in sorted(environ.items())
    ]
    response_body = '\n'.join(response_body)

    status = '200 OK'
    response_headers = [
        ('Content-Type', 'text/plain'),
        ('Content-Length', str(len(response_body)))
    ]
    start_response(status, response_headers)

    return [response_body] # 不能改成return response_body,因为会遍历返回值,如果不是列表则会遍历字符串,所以会变慢

# Instantiate the server
httpd = make_server (
    'localhost', # The host name
    8051, # A port number where to wait for the request
    application # The application object name, in this case a function
)

# Wait for a single request, serve it and quit
httpd.handle_request()

Response Iterable

Parsing the Request - Get

Parsing the Request - Post

参考:
http://wsgi.tutorial.codepoint.net/intro

WSGIPython Web Server Gateway Interface的缩写,是Python应用程序或框架和Web服务器之间的一种接口,用于在Web服务器上运行Python Web应用程序。在Windows上使用WSGI,可以通过以下步骤实现: 1.安装mod_wsgi模块:可以从官方网站(https://www.lfd.uci.edu/~gohlke/pythonlibs/#mod_wsgi)下载对应版本的mod_wsgi模块,然后使用pip进行安装。 2.配置Apache服务器:在httpd.conf文件中添加以下内容: ```apache LoadModule wsgi_module "C:/Python27/Lib/site-packages/mod_wsgi/server/mod_wsgi.pyd" WSGIScriptAlias /myapp "C:/path/to/myapp.wsgi" <Directory "C:/path/to"> Order allow,deny Allow from all </Directory> ``` 其中,第一行是加载mod_wsgi模块,第二行是指定WSGI应用程序的URL路径和WSGI文件的路径,第三至五行是指定WSGI文件所在目录的访问权限。 3.编写WSGI应用程序:创建一个名为myapp.wsgi的文件,编写WSGI应用程序的代码,例如: ```python def application(environ, start_response): status = '200 OK' output = 'Hello World!' response_headers = [('Content-type', 'text/plain'), ('Content-Length', str(len(output)))] start_response(status, response_headers) return [output] ``` 其中,application函数是WSGI应用程序的入口,environ是一个包含HTTP请求信息的字典,start_response是一个回调函数,用于发送HTTP响应头,返回值是HTTP响应体。 4.启动Apache服务器:在命令行中输入httpd命令启动Apache服务器,然后在浏览器中访问http://localhost/myapp即可看到Hello World!的输出。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值