Python POST api提供错误。找不到日志

2024-04-23 22:15:55 发布

您现在位置:Python中文网/ 问答频道 /正文

我对Python还不熟悉。我使用下面提到的链接中的步骤在AWS上部署了一个python的postapi。你知道吗

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)

注:其他进口产品用于其他用途。我需要后API来工作。你知道吗


Tags: fromimporttxtlogapiobjappflask
1条回答
网友
1楼 · 发布于 2024-04-23 22:15:55

尝试设置debug = True

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

另外,您不需要导入日志,因为您可以使用flask的记录器,该记录器应该可以使用app.logger访问

如果这是生产,那么debug=True是不推荐的。 如果未设置日志级别,Flask将错误视为默认日志级别 所以您可以尝试使用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"

相关问题 更多 >