码迷,mamicode.com
首页 > 其他好文 > 详细

使用WSGIServer修改静态文件

时间:2020-02-15 11:30:10      阅读:78      评论:0      收藏:0      [点我收藏+]

标签:脚本   style   exist   doc   fastcgi   cgi   amp   odi   nss   

背景:公司水务项目要求提供一个以POST方法修改静态文件的接口,由于nginx已经关闭了对POST方法的支持,并且开启POST方法后也无法去修改静态文件,所以需要额外的一个api接口去修改,而python的WSGIServer正好适用

一、使nginx支持POST方法

1、下载nginx的tar包:http://nginx.org/download/nginx-1.15.9.tar.gz

2、解压tar包后,修改src/http/modules/ngx_http_static_module.c文件,将文件中的NGINX_HTTP_POST选项,注释,如下图:

技术图片

 

3、为nginx添加nginx-upload-module模块:

模块下载地址:https://github.com/winshining/nginx-upload-module

直接git conle https://github.com/winshining/nginx-upload-module即可

4、然后编译并安装nginx即可,下面为Dockerfile内容:

FROM ubuntu
ADD nginx-1.15.9.tar.gz nginx-upload-module.tar.gz /opt/
RUN apt update   && apt -y install python3.7 python3-pip libpcre3 libpcre3-dev zlib1g-dev openssl libssl-dev make gcc   && cd /opt/nginx-1.15.9   && ./configure --with-debug --prefix=/opt/nginx --sbin-path=/usr/bin/nginx --add-module=../nginx-upload-module --with-http_ssl_module   && make   && make install   && mkdir /opt/nginx/conf/conf.d   && rm -rf /opt/nginx-1.15.9 /opt/nginx-upload-module   && ln -s /usr/bin/python3.7  /usr/bin/python   && apt -y autoclean
COPY nginx.conf /opt/nginx/conf/
COPY water.conf /opt/nginx/conf/conf.d/
ADD ["upload.py", "/opt/"]
ENV PYTHONPATH="/usr/local/lib/python3.6/dist-packages:$PYTHONPATH"
RUN pip3 install flup-py3 
EXPOSE 80 443
VOLUME /opt/openapi
CMD nginx && python /opt/upload.py

二、WSGIServer的脚本编写

1、编写upload.py脚本

#!/usr/bin/env python
# coding=utf-8

import os
from flup.server.fcgi import WSGIServer


def write_file(filename, file_body):
    if os.path.exists(filename):
        with open(filename, w+, encoding=utf-8) as f:
            f.write(file_body)


def get_environ(environ):
    rquest_method = environ["REQUEST_METHOD"]
    str1 = "rquest_method:" + rquest_method + "\r\n"
    query_string = environ["QUERY_STRING"]
    str1 += ",query_string:" + query_string + "\r\n"
    request_uri = environ["REQUEST_URI"]
    str1 += ", request_uri:" + request_uri + "\r\n"
    remote_addr = environ["REMOTE_ADDR"]
    str1 += ",remote_addr:" + remote_addr + "\r\n"
    remote_port = environ["REMOTE_PORT"]
    str1 += ",remote_port:" + remote_port + "\r\n"

    request_body_size = int(environ.get(CONTENT_LENGTH, 0))
    body = environ[wsgi.input].read(request_body_size)
    str_body = str(body, encoding="utf-8")
    str1 += ",body:" + str_body + "\r\n"
    if len(str_body):
        file_uri = request_uri.replace(/upload, /opt/openapi)
        write_file(file_uri, str_body)
    return str1


def webapp(environ, start_response):
    start_response(200 OK, [(Content-Type, text/plain)])
    str2 = get_environ(environ)
    content = str2
    return [content]


if __name__ == __main__:
    WSGIServer(webapp, multithreaded=True, multiprocess=False, bindAddress=(127.0.0.1, 10080)).run()

2、配置nginx的配置文件:

server {
    listen       80;
    server_name  dev-water.wavewisdom-bj.com;
    access_log  /opt/nginx/logs/water_access.log;
    error_log /opt/nginx/logs/water_error.log;

    location / {
        root   /usr/share/nginx/html;
        index  index.html index.htm;
    }

    location /openapi {
          alias /opt/openapi;
        add_header Content-Type application/json; 
    }
    location /upload {
        include fastcgi_params;
        fastcgi_pass 127.0.0.1:10080;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }
}

三、以上即为全部内容,然后进行post测试

1、post方法修改文件

技术图片

 

 2、获取文件

技术图片

 

使用WSGIServer修改静态文件

标签:脚本   style   exist   doc   fastcgi   cgi   amp   odi   nss   

原文地址:https://www.cnblogs.com/caibao666/p/12310941.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!