我所有路由都收到404,我的flask API正在运行,当我运行它时,该API接收请求并记录该请求,但是,每个请求都给出404 http状态;
shell
127.0.0.1 - - [17/Dec/2018 16:49:12] "GET /api/ping/ HTTP/1.1" 404 -
127.0.0.1 - - [17/Dec/2018 16:50:10] "POST /api/blur/ HTTP/1.1" 404 -
我有一个Server
类,该服务器类收到一个Flask app
并运行它:
from jsonmerge import merge
class Server():
def __init__(self, app):
self.__app = app
self.__config = #snip
#snip
def start(self, options=None):
#snip
self.__app.run(debug=self.__config["debug"],
port=self.__config["server"]["port"], host=self.__config["server"]["ip"])
我有运行我的应用程序的run.py:
if __name__ == '__main__':
srv = Server(app)
srv.start(config)
当我打印self.__app
信息时,输出为:
<Flask 'appfly.app'>
<Config {'ENV': 'production', 'DEBUG': False, 'TESTING': False, 'PROPAGATE_EXCEPTIONS': None, 'PRESERVE_CONTEXT_ON_EXCEPTION': None, 'SECRET_KEY': None, 'PERMANENT_SESSION_LIFETIME': datetime.timedelta(days=31), 'USE_X_SENDFILE': False, 'SERVER_NAME': None, 'APPLICATION_ROOT': '/', 'SESSION_COOKIE_NAME': 'session', 'SESSION_COOKIE_DOMAIN': None, 'SESSION_COOKIE_PATH': None, 'SESSION_COOKIE_HTTPONLY': True, 'SESSION_COOKIE_SECURE': False, 'SESSION_COOKIE_SAMESITE': None, 'SESSION_REFRESH_EACH_REQUEST': True, 'MAX_CONTENT_LENGTH': None, 'SEND_FILE_MAX_AGE_DEFAULT': datetime.timedelta(seconds=43200), 'TRAP_BAD_REQUEST_ERRORS': None, 'TRAP_HTTP_EXCEPTIONS': False, 'EXPLAIN_TEMPLATE_LOADING': False, 'PREFERRED_URL_SCHEME': 'http', 'JSON_AS_ASCII': True, 'JSON_SORT_KEYS':True, 'JSONIFY_PRETTYPRINT_REGULAR': False, 'JSONIFY_MIMETYPE': 'application/json', 'TEMPLATES_AUTO_RELOAD': None, 'MAX_COOKIE_SIZE': 4093}>
Map([<Rule '/api/ping' (GET, HEAD, OPTIONS) -> ping>,
<Rule '/api/blur' (POST, OPTIONS) -> blur>,
<Rule '/static/<filename>' (GET, HEAD, OPTIONS) -> static>])
看,我的url_map
列出了我所有的路线,其中有api/ping
条路线和api/blur
条路线。
许多stackover流响应都说要更改config [“ server_name”],但它不起作用 我已经尝试将SERVER_NAME设置为0.0.0.0:9000,localhost:9000,127.0.0.1:9000
我制作了一个烧瓶教程来验证SERVER_NAME,它是None
from flask import Flask
app = Flask(__name__)
@app.route("/")
def hello():
return "Hello World!"
if __name__ == '__main__':
print(app.config["SERVER_NAME"])
#app.run(...)
> None
我尝试将设置为Flask ENV
到development
,但是它不适用于:
self.__app.config["ENV"]='development'
我以这种方式使用Flask,并且aways工作正常:
---> app.py
from flask import Flask
app = Flask(__name__)
# Routes files
from app.presentation.routes import ping
from app.presentation.routes import rg as rg_scan
# Routes rules
app.add_url_rule('/api/ping', 'Ping', ping.route, methods=['GET'])
app.add_url_rule('/api/rg', 'RG', rg_scan.route, methods=['POST'])
print('API is running...')
---> run.py
from app import app
if __name__ == '__main__':
app.run(debug=True, port=9000, host='0.0.0.0')
但是现在我想用一种不同的方式来做,我想使用服务器类来启动我的Flask API。