Python POST api给出错误。找不到日志

时间:2018-02-21 08:54:25

标签: python rest api amazon-web-services post

我是python的新手。我在AWS上使用下面提到的链接中的步骤部署了一个POST api。

https://www.digitalocean.com/community/tutorials/how-to-serve-flask-applications-with-uwsgi-and-nginx-on-ubuntu-16-04

api正在提供内部服务器错误,并且没有打印日志。是否有我缺少的目录或者我能以某种方式打印日志吗?

此外,如果有人可以帮助解决API的可能问题:

#!/usr/bin/env python2.7
#!flask/bin/python
#!/usr/bin/python
import numpy
import sys,os
import codecs
import json
import phonetics
import transliteration
import jellyfish
import traceback
from pyjarowinkler import distance
from flask import Flask, jsonify, request
import panphon.distance
from datetime import datetime
from flask_cors import CORS


def obj_dict(obj):
    return obj.__dict__

# logFile = open('log.txt','a')

app = Flask(__name__)
cors = CORS(app, resources={r"/todo/api/*": {"origins": "*"}})

@app.route('/todo/api/v1.0/tasks', methods=['GET''POST'])
def get_tasks():
    if request.method == 'GET':
        return "done with get request"

    elif request.method == 'POST':
        try :
            content = request.get_json()
            with codecs.open('words.txt', 'a') as file:
                for line in content['words']:
                    file.write("\n"+line.encode('utf-8'))

        except Exception:
            with open('log.txt','a') as logger:
                logger.write("At "+str(datetime.now())+" "+traceback.format_exc())
                logger.write("\n\n")

        return "done"

if __name__ == '__main__':
    import logging
    logging.basicConfig(filename='log.txt',level=logging.DEBUG)
    app.run(host='0.0.0.0', debug = False)

注意:其他进口用于其他目的。我需要POST API才能工作。

1 个答案:

答案 0 :(得分:1)

尝试设置debug = True

例如 if __name__ == '__main__': import logging logging.basicConfig(filename='log.txt',level=logging.DEBUG) app.run(host='0.0.0.0', debug = True)

此外,您不需要导入日志记录,因为您可以使用应该可以使用app.logger

访问的记录器记录器

如果这是生产,则不建议使用debug=True。 如果您未设置日志级别,Flask会将ERROR视为默认日志记录级别 因此,您可以尝试使用app.logger.setLevel(logging.DEBUG)

设置日志级别
@app.route('/todo/api/v1.0/tasks', methods=['GET', 'POST'])
def get_tasks():
    if request.method == 'GET':
        return "done with get request"

    elif request.method == 'POST':
        try :
            content = json.loads(request.get_data())
            with codecs.open('words.txt', 'a') as file:
                for line in content['words']:
                    file.write("\n"+line.encode('utf-8'))

        except Exception:
            with open('log.txt','a') as logger:
                logger.write("At "+str(datetime.now())+" "+traceback.format_exc())
                logger.write("\n\n")

        return "done"