欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

mod_wsgi 初体验

程序员文章站 2022-06-04 11:19:47
1, 安装 ./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python3 make && make install 2,配置 LoadModule wsgi_module modules/mod_ ......
1, 安装
./configure --with-apxs=/usr/local/apache2/bin/apxs --with-python=/usr/bin/python3
make && make install

 

2,配置
loadmodule wsgi_module modules/mod_wsgi.so
 
wsgiscripalias /test /usr/local/apache2/htdocs/wsgi/test.wsgi application-group=%{global}

/*
ps:
1,wsgi脚本,得放到 $(documentroot)的目录里,否则会出现403;
2,application-group=%{global}
   https://code.google.com/archive/p/modwsgi/wikis/quickconfigurationguide.wiki
   https://code.google.com/archive/p/modwsgi/wikis/reloadingsourcecode.wiki
*/
 
 
3,编辑模块
 
def application(environ, start_response):
    start_response('200 ok', [('content-type', "text/html')])
    return [to_byte('<h1>test wsgi</h1>')]
 
/*
ps:
1,待return的数据,必须是用[] 包含。
2,typeerror: sequence of byte string values expected, value of type int found
   指的是:期望的是byte类型,返回的却是其他类型(如 int),需要在返回之前进行转码
*/
 
def to_byte(input):
    return str(input).encode(encoding='utf-8')

 

4,import
modulenotfounderror: no module named 'ttt'
 
import的时候,并未根据wsgi的当前路径搜索。因此,需要将当前路径添加到sys.path中。
 
import os
import sys
 
sys.insert(0,os.path.abspath(os.path.dirname(__file__)))
 
import ttt

 

5,后缀
后缀不必非得.wsgi
 
6,其他
文档:https://www.python.org/dev/peps/pep-3333/
 
7,environ
127.0.0.1/test001?a=aa&b=bb
 
{
    "gateway_interface": "cgi/1.1",
    "server_protocol": "http/1.1",
    "request_method": "get",
    "query_string": "a=aa&b=bb",
    "request_uri": "/test001",
    "script_name": "/test001",
    "http_host": "127.0.0.1"
   ... }

 

8,start_response
100 continue
101 switching protocols
200 ok
201 created
202 accepted
203 non-authoritative information
204 no content
205 reset content
206 partial content
300 multiple choices
301 moved permanently
302 found
303 see other
304 not modified
305 use proxy
306 (unused)
307 temporary redirect
400 bad request
401 unauthorized
402 payment required
403 forbidden
404 not found
405 method not allowed
406 not acceptable
407 proxy authentication required
408 request timeout
409 conflict
410 gone
411 length required
412 precondition failed
413 request entity too large
414 request-uri too long
415 unsupported media type
416 requested range not satisfiable
417 expectation failed
500 internal server error
501 not implemented
502 bad gateway
503 service unavailable
504 gateway timeout
505 http version not supported