Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Nginx和WSGI运行Python脚本-卡住_Python_Apache_Api_Nginx_Wsgi - Fatal编程技术网

使用Nginx和WSGI运行Python脚本-卡住

使用Nginx和WSGI运行Python脚本-卡住,python,apache,api,nginx,wsgi,Python,Apache,Api,Nginx,Wsgi,我是python新手,一直在承担构建电子表格解析器的任务。我创建了一个python脚本,用于读取xlsx文件并解析数据。我已经设置了一个Nginx服务器,它将被托管在这个服务器上。我需要这个脚本作为API端点,这样我就可以将解析后的数据作为JSON传回。我一直在阅读有关WSGI for production server的文章,并尝试按照构建的路线进行。我能够在服务器上提供一条路径,并让它输出wsgi python脚本。该脚本具有以下功能: def application(environ, st

我是python新手,一直在承担构建电子表格解析器的任务。我创建了一个python脚本,用于读取xlsx文件并解析数据。我已经设置了一个Nginx服务器,它将被托管在这个服务器上。我需要这个脚本作为API端点,这样我就可以将解析后的数据作为JSON传回。我一直在阅读有关WSGI for production server的文章,并尝试按照构建的路线进行。我能够在服务器上提供一条路径,并让它输出wsgi python脚本。该脚本具有以下功能:

def application(environ, start_response):
status = '200 OK'
html = '<html>\n' \
       '<body>\n' \
       ' Hooray, mod_wsgi is working\n' \
       '</body>\n' \
       '</html>\n'
response_header = [('Content-type','text/html')]
start_response(status, response_header)
return [html]
@app.route('/parser/direct_energy',methods=['GET'])

def get_data(): 返回jsonify(佣金数据)

如果name='main':
app.run(host='0.0.0.0')

您不想为此使用原始WSGI

使用诸如(或)之类的软件包,让一切都变得更容易

例如,使用FastAPI,具有接收二进制(Excel)文件并返回JSON响应的端点的应用程序大约是

from fastapi import FastAPI, File, UploadFile

app = FastAPI()


@app.post("/process")
def process_file(file: UploadFile = File()):
    response = my_data_processing_function(data)
    return {"response": response}
见:

  • 要开始:
  • 要处理文件,请执行以下操作:
  • 要部署您的服务(在Nginx后面):

我使用
python/flask
进行开发&
gunicorn
进行生产

为了让它接受HTTP请求,我使用函数装饰器。这是最常见的方式

@application.route('/epp/api/v1.0/request', methods=['POST'])
def eppJSON():
    if flask.request.json is None:
        return abort(400, "No JSON data was POSTed")
    return jsonRequest(flask.request.json, flask.request.remote_addr)
因此在这里,url
/epp/api/v1.0/request
接受
POST
ed JSON并返回JSON

在开发模式下运行
flask
时,它将侦听
http://127.0.0.1:5000


这些都是我的
python/flask
项目。请随意复制。它们各自在一个容器负载中运行多个python代码实例,该负载由
nginx
平衡-非常巧妙的组合。

更新

我通过NGinx、flask和GUnicorn工作。但是,我的flask应用程序只有在转到“/”时才起作用。如果我转到/parser/de/v1这样的路由,我会得到一个404未找到。
以下是我对NGinx的设置:

server {
    listen 80 default_server;
    listen [::]:80 default_server;

    # SSL configuration
    #
    # listen 443 ssl default_server;
    # listen [::]:443 ssl default_server;
    #
    # Note: You should disable gzip for SSL traffic.
    # See: https://bugs.debian.org/773332
    #
    # Read up on ssl_ciphers to ensure a secure configuration.
    # See: https://bugs.debian.org/765782
    #
    # Self signed certs generated by the ssl-cert package
    # Don't use them in a production server!
    #
    # include snippets/snakeoil.conf;

    root /var/www/html/excel_parser;

    # Add index.php to the list if you are using PHP
    index index.html index.htm index.nginx-debian.html index.php;

    server_name 208.97.141.147;

    location / {
            # First attempt to serve request as file, then
            # as directory, then fall back to displaying a 404.
            proxy_pass http://127.0.0.1:5000;
            proxy_connect_timeout 75s;
            proxy_read_timeout 300s;
            try_files $uri $uri/ =404;
    }

    # pass PHP scripts to FastCGI server
    #
    #location ~ \.php$ {
    #       include snippets/fastcgi-php.conf;
    #
    #       # With php-fpm (or other unix sockets):
    #       fastcgi_pass unix:/var/run/php/php7.0-fpm.sock;
    #       # With php-cgi (or other tcp sockets):
    #       fastcgi_pass 127.0.0.1:9000;
    #}

    # deny access to .htaccess files, if Apache's document root
    # concurs with nginx's one
    #
    #location ~ /\.ht {
    #       deny all;
    #}

       

我的
nginx.conf
看起来有些不同,部分原因是我正在运行多个WSGI实例,然后让
nginx
在它们上面实现负载平衡

worker_processes  3;
events {
    worker_connections  1024;
}
user daemon;
http {
    access_log      off;
    error_log       stderr error;
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream dns_servers {
        server unix:/ram/dnsflsk_1.sock;
        server unix:/ram/dnsflsk_2.sock;
        server unix:/ram/dnsflsk_3.sock;
        }

    server {
        listen 800 ssl;
        server_name localhost;
        ssl_certificate      certkey.pem;
        ssl_certificate_key  certkey.pem;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://dns_servers;
        }
    }
}

但是有了这个,所有的URL都被传递到python/wsgi

非常感谢,我也尝试过使用flask,但不知道如何持续运行flask服务器,以便为路由提供服务。我在实际的api脚本中有这样的代码:app=Flask(name)@app.route('/parser/direct_energy',methods=['GET'])def GET_data():如果name='main',则返回jsonify(commissions_data):app.run(host='0.0.0')但是这不适用于我的IP地址Flask也有关于如何运行服务器的说明:AKX-我尝试了gunicorn api:app,服务器启动正常。它说它正在侦听:,但是如何在服务器上访问该端点?附加了:8000的我的ip地址不起作用除了“/”之外,我在flask中的所有路径在我的NGinx服务器上找不到404有什么原因吗?请尝试删除
根目录
索引
worker_processes  3;
events {
    worker_connections  1024;
}
user daemon;
http {
    access_log      off;
    error_log       stderr error;
    include         mime.types;
    default_type    application/octet-stream;
    sendfile        on;
    keepalive_timeout  65;
    upstream dns_servers {
        server unix:/ram/dnsflsk_1.sock;
        server unix:/ram/dnsflsk_2.sock;
        server unix:/ram/dnsflsk_3.sock;
        }

    server {
        listen 800 ssl;
        server_name localhost;
        ssl_certificate      certkey.pem;
        ssl_certificate_key  certkey.pem;
        ssl_session_cache    shared:SSL:1m;
        ssl_session_timeout  5m;
        ssl_ciphers  HIGH:!aNULL:!MD5;
        ssl_prefer_server_ciphers  on;
        location / {
            proxy_pass http://dns_servers;
        }
    }
}