API Rest不适用于gunicorn + flask

时间:2017-11-22 20:27:09

标签: python flask gunicorn

我正在尝试检查我在Flask中的API Rest是否可以与Gunicorn一起使用,但它并没有响应我在网络浏览器中的请求:

我的python代码是:

import flask
import flask.ext.sqlalchemy
import flask.ext.restless
import datetime
import os

from flask import Response

app = flask.Flask(__name__, static_url_path='')

DATABASE_URL = os.environ.get('DATABASE_URL','sqlite:///test.db')
app.config["DEBUG"] = True
app.config['SQLALCHEMY_DATABASE_URI'] = DATABASE_URL 
db = flask.ext.sqlalchemy.SQLAlchemy(app)

@app.route('/')
def root():
    return app.send_static_file('index.html')



class News(db.Model):
    id = db.Column(db.Integer,primary_key=True)
    link = db.Column(db.Unicode, unique= True)
    received_date = db.Column(db.DateTime(), default=datetime.datetime.now)
    analysed_date = db.Column(db.DateTime())
    status = db.Column(db.Unicode)
    info = db.Column(db.Unicode)
    publish_date = db.Column(db.DateTime())
    text = db.Column(db.Unicode)
    link_image = db.Column(db.Unicode)
    keywords = db.Column(db.Unicode)
    videos  = db.Column(db.Unicode)
    summary = db.Column(db.Unicode)
    title = db.Column(db.Unicode)





if __name__ == '__main__':
    db.create_all()
    port = int(os.environ.get('PORT',5000))
    manager = flask.ext.restless.APIManager(app, flask_sqlalchemy_db=db)
    manager.create_api(News,methods=['GET','POST','DELETE'])
    app.run(host='0.0.0.0', port = port)

我执行:

gunicorn -b 0.0.0.0:5000 rest_api:app

但如果尝试连接到此网络(没有gunicorn工程):

http://localhost:5000/api/news

我收到400错误。

0 个答案:

没有答案