【发布时间】:2020-05-31 14:38:37
【问题描述】:
我有一个非常简单的 python REST 应用程序:
main.py
>from app import app
def main():
parser = ArgumentParser(description="Character information hosting server!")
parser.add_argument('--parse-word-doc', dest="parse_doc", required=False, action='store_true',
help='Parses a word document for characters and then serializes them with pickle to a file')
parser.add_argument('--source-file', dest="source_file", type=str, required=False,
help='Path and name of the source word document you want to read from. Defaults to source.docx.',
default="source.docx")
parser.add_argument('--port', dest="port", required=False, type=int, default=5000,
help='Specify the port you want Flask to run on')
parser.add_argument('--log-level', metavar='LOG_LEVEL', dest="log_level", required=False, type=str, default="info",
choices=['debug', 'info', 'warning', 'error', 'critical'],
help='The log level at which you want to run.')
args = parser.parse_args() # type: argparse.Namespace
if not Path(args.source_file).is_file():
logging.error("Could not find " + args.source_file + ". Are you sure you got the path right?")
exit(1)
logging.info("Reading data from the file \"database\" from disk")
with open('database', 'rb') as database:
characters = pickle.load(database)
app.config['DATABASE'] = characters
if args.log_level:
if args.log_level == "debug":
logging.basicConfig(level=logging.DEBUG)
app.config['DEBUG'] = True
elif args.log_level == "info":
logging.basicConfig(level=logging.INFO)
elif args.log_level == "warning":
logging.basicConfig(level=logging.WARNING)
elif args.log_level == "error":
logging.basicConfig(level=logging.ERROR)
elif args.log_level == "critical":
logging.basicConfig(level=logging.CRITICAL)
else:
logging.basicConfig(level=logging.INFO)
app.run(host='0.0.0.0', port=args.port)
if __name__ == '__main__':
main()
app/init.py
>from flask import Flask
# Initialize the app
app = Flask(__name__)
# Load the views
from app import views
app/config.py
>import os
class Config(object):
# Enable Flask's debugging features. Should be False in production
DEBUG = os.environ.get('DEBUG') or True
SECRET_KEY = os.environ.get('SECRET_KEY') or 'default-secret-just-for-csrf-attacks-nbd'
app/views.py
>from flask import request
from app import app
@app.route('/api/lookup', methods=['GET'])
def lookup():
input_text = request.args.get('character_to_lookup')
if input_text in app.config['DATABASE']:
return app.config['DATABASE'][input_text]
else:
return {}
问题
如果我发送一个简单的 curl 命令:curl -X GET -d '{"character_to_lookup": "test"}' http://127.0.01:5000/api/lookup -H 'Content-Type: application/json'
并检查 PyCharm 进行调试 - 请求根本不存在。它没有设置为无,它只是不存在。我缺少的应用上下文一定有一些东西,但不清楚。
我在网上唯一能找到的就是你需要导入请求,我已经这样做了,我想不出另一个根本不存在请求的原因。
【问题讨论】: