Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/300.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
Python 返回500个错误_Python_Api_Amazon Web Services_Deployment_Flask - Fatal编程技术网

Python 返回500个错误

Python 返回500个错误,python,api,amazon-web-services,deployment,flask,Python,Api,Amazon Web Services,Deployment,Flask,因此,我试图将我的python flask应用程序部署到ubuntu AWS EC2实例。我已经设置了mod_wsgi,将虚拟主机设置配置为virtualenv,并创建了别名来为静态文件提供服务器。由于某些原因,我无法获取api路由的自定义url以返回正确的信息。我已经尝试了所有我搜索过的地方这是我最后的选择 #!/usr/bin/env python import threading import subprocess import uuid import json from celery i

因此,我试图将我的python flask应用程序部署到ubuntu AWS EC2实例。我已经设置了mod_wsgi,将虚拟主机设置配置为virtualenv,并创建了别名来为静态文件提供服务器。由于某些原因,我无法获取api路由的自定义url以返回正确的信息。我已经尝试了所有我搜索过的地方这是我最后的选择

#!/usr/bin/env python
import threading
import subprocess
import uuid
import json
from celery import Celery
from celery.task import Task
from celery.decorators import task
from celery.result import AsyncResult
from scripts.runTable import runTable
from scripts.getCities import getCities
from scripts.pullScript import createOperation
from flask import Flask, render_template, make_response, url_for, abort, jsonify, request, send_from_directory, Response
app = Flask(__name__)
app.config['CELERY_BROKER_URL'] = 'amqp://guest:guest@localhost:5672//'
app.config['CELERY_RESULT_BACKEND'] = 'amqp'
app.config['CELERY_TASK_RESULT_EXPIRES'] = 18000
app.config['CELERY_ACCEPT_CONTENT'] = ['json']
app.config['CELERY_TASK_SERIALIZER'] ='json'
app.config['CELERY_RESULT_SERIALIZER'] = 'json'

operation = createOperation()
cities = getCities()
table = runTable()
value = ''
state = ''

celery = Celery(app.name, backend=app.config['CELERY_RESULT_BACKEND'], broker=app.config['CELERY_BROKER_URL'])
celery.conf.update(app.config)

@task(bind=True)
def pull_async_data(self, data):
    global state
    state = pull_async_data.request.id
    operation.runSequence(data)


@app.route('/api/v1/getMapInfo', methods=['GET'])
def map():
    mp = operation.getMapData()
    resp = Response(mp, status=200, mimetype="application/json")
    return resp


@app.route('/api/v1/getTable', methods=['GET'])
def tables():
    tb = table.getTableInfo()
    resp = Response(tb, status=200, mimetype="application/json")
    return resp


##Get states from the DB
@app.route('/api/v1/getStates', methods=['GET'])
def states():
    st = cities.getStatesFromDB()
    resp = Response(st, status=200, mimetype="application/json")
    return resp


@app.route('/api/v1/getCities', methods=['POST'])
def city():
    data = request.get_json()
    # print data
    ct = cities.getCitiesFromDB(data)
    resp = Response(ct, status=200, mimetype="application/json")
    return resp


@app.route('/api/v1/getQueue', methods=['GET'])
def queue():
    queue = operation.getCurrentQueue()
    resp = Response(queue, status=200, mimetype="application/json")
    return resp



##Checking the status of api progress
@app.route('/api/v1/checkStatus', methods=['GET'])
def status():
    res = pull_async_data.AsyncResult(state).state
    js = json.dumps({'State': res})
    resp = Response(js, status=200, mimetype="application/json")
    return resp


##Perform the pull and start the script
@app.route('/api/v1/pull', methods=['POST'])
def generate():
    global value
    value = json.dumps(request.get_json())
    count = operation.getCurrentQueue(value)
    pull_async_data.apply_async(args=(value, ))
    js = json.dumps({"Operation": "Started", "totalQueue": count})
    resp = Response(js, status=200, mimetype="application/json")
    return resp


##Check main app
if __name__ == "__main__":
    app.run(debug=True)
这是WSGI文件oakapp.WSGI

#!/usr/bin/python
import sys

activate_this = '/var/www/oakapp/venv/bin/activate_this.py'
execfile(activate_this, dict(__file__=activate_this))

sys.path.append('/var/www/oakapp')

print sys.path.insert(0, '/var/www/oakapp/scripts')

from app import app as application
这是虚拟主机环境

    <VirtualHost *:80>
            ServerName oakapp.com

            DocumentRoot /var/www/oakapp

            Alias /js /var/www/oakapp/js
            Alias /css /var/www/oakapp/css

            WSGIDaemonProcess oakapp user=apps group=ubuntu threads=5
            WSGIScriptAlias / /var/www/oakapp/oakapp.wsgi


            <Directory /var/www/oakapp/>
                    WSGIProcessGroup oakapp
                    WSGIApplicationGroup %{GLOBAL}
                    WSGIScriptReloading On
                    Order allow,deny
                    Allow from all
            </Directory>

            ErrorLog /var/www/oakapp/logs/oakapp_error.log
            LogLevel info
            CustomLog /var/www/oakapp/logs/oakapp_access.log combined
    </VirtualHost>
这是一个netstate-plunt输出

    Active Internet connections (only servers)
    Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
    tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1062/sshd       
    tcp        0      0 127.0.0.1:3031          0.0.0.0:*               LISTEN      29277/uwsgi     
    tcp        0      0 0.0.0.0:46035           0.0.0.0:*               LISTEN      24222/beam      
    tcp6       0      0 :::22                   :::*                    LISTEN      1062/sshd       
    tcp6       0      0 :::5672                 :::*                    LISTEN      24222/beam      
    tcp6       0      0 :::80                   :::*                    LISTEN      2608/apache2    
    tcp6       0      0 :::4369                 :::*                    LISTEN      24197/epmd      
    udp        0      0 0.0.0.0:17372           0.0.0.0:*                           568/dhclient    
    udp        0      0 0.0.0.0:68              0.0.0.0:*                           568/dhclient    
    udp6       0      0 :::28264                :::*                                568/dhclient 
下面是目录结构

    ├── app.py
    ├── app.pyc
    ├── css
    │   ├── fonts
    │   │   ├── untitled-font-1.eot
    │   │   ├── untitled-font-1.svg
    │   │   ├── untitled-font-1.ttf
    │   │   └── untitled-font-1.woff
    │   ├── leaflet.css
    │   └── master.css
    ├── js
    │   ├── images
    │   │   ├── layers-2x.png
    │   │   ├── layers.png
    │   │   ├── marker-icon-2x.png
    │   │   ├── marker-icon.png
    │   │   └── marker-shadow.png
    │   ├── leaflet.js
    │   └── main.js
    ├── json
    │   └── states.json
    ├── logs
    │   ├── oakapp_access.log
    │   └── oakapp_error.log
    ├── oakapp.wsgi
    ├── sass
    │   └── master.scss
    ├── scripts
    │   ├── database
    │   │   ├── cities_extended.sql
    │   │   ├── oak.db
    │   │   └── states.sql
    │   ├── getCities.py
    │   ├── getCities.pyc
    │   ├── __init__.py
    │   ├── __init__.pyc
    │   ├── pullScript.py
    │   ├── pullScript.pyc
    │   ├── runTable.py
    │   └── runTable.pyc
    ├── templates
        └── index.html
感谢您的帮助

下面是从我的个人计算机运行到主机的curl请求

    djove:.ssh djowinz$ curl http://**.*.**.139/api/v1/getTable
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>500 Internal Server Error</title>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>
下面是根据请求使用localhost从主机运行到主机的curl请求

    root@ip-172-31-24-66:/var/www/oakapp# curl http://localhost/api/v1/getTable
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>500 Internal Server Error</title>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>

事实证明,这是我如何实例化uwsgi的一个问题。我从头开始删除了所有内容,并使用uwsgi浏览了烧瓶安装说明,使所有内容都能正常工作。

可以使用curl output?@kracekumar本地或远程显示请求和响应,例如从服务器或我的个人机器?它没有提供足够的关于/api/v1/getTable的信息。这看起来不像是配置问题。查看/var/www/oakapp/logs/oakapp_error.log中的内容。这可能会给你一些线索。@kracekumar[Tue Apr 28 05:03:56.050981 2015][:error][pid 3574:tid 140512686647040][remote 72.219.180.235:10929]TypeError:“module”对象不可调用问题是你使用的函数/方法是module。我认为你应该很容易从这里开始。
    root@ip-172-31-24-66:/var/www/oakapp# curl http://localhost/api/v1/getTable
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
    <title>500 Internal Server Error</title>
    <h1>Internal Server Error</h1>
    <p>The server encountered an internal error and was unable to complete your request.  Either the server is overloaded or there is an error in the application.</p>