我刚刚第一次进入Flask并尝试使用它来创建一个API,它有GET,POST和DELETE路由来管理网址黑名单。
经过一段时间后,POST路由会给我一个错误:"无法得到任何回复。"奇怪的是,我的GET和DELETE路线仍然可以正常工作。我试图让一些伐木进行,但是天堂还没有得到多少。我很感激任何帮助!
这是我的应用:
@Override
public void setTitle(final CharSequence title) {
toolBar.postDelayed(new Runnable() {
@Override
public void run() {
MainActivity.super.setTitle(title);
}
}, 200);
}
这是我的配置文件:
from flask import Flask, request
from flask_cors import CORS, cross_origin
from flask_restful import Resource, Api
import pickle
app = Flask(__name__)
app.config['DEBUG'] = True
CORS(app)
@app.route("/get-blacklist")
def loadblacklist():
blacklist = pickle.load( open( "blacklist.p", "rb" ) )
return dumps(blacklist)
@app.route("/post-blacklist", methods=['POST'])
def puturl():
# Get and clean URL
url = request.form['url']
url = sanitise(url)
# Load the pickle
blacklist = list(pickle.load( open( "blacklist.p", "rb" ) ))
# Check if it's already in there
if url not in blacklist:
blacklist.append(url)
# Save the pickle
pickle.dump( blacklist, open( "blacklist.p", "wb" ) )
return 'You posted {}'.format(url)
@app.route("/remove-blacklist", methods=['DELETE'])
def removeurl():
# Get and clean the URL
url = request.form['url']
url = sanitise(url)
# Load the pickle
blacklist = list(pickle.load( open( "blacklist.p", "rb" ) ))
# Remove if its in there
if url in blacklist:
blacklist.remove(url)
# Save the pickle
pickle.dump( blacklist, open( "blacklist.p", "wb" ) )
return 'You removed {}'.format(url)
def sanitise(url):
# Sanitise
url = url.lower()
url = url.replace('www.', '')
url = url.replace('http://', '')
url = url.replace('https://', '')
if url[-1] == '/':
url = url[:-1]
return url
if __name__ == '__main__':
app.run()
以下是我用来启动工人的命令:
import os
bind = '0.0.0.0:8000'
workers = 3
backlog = 2048
worker_class = "sync"
debug = False
proc_name = 'gunicorn.proc'
pidfile = './gunicorn.pid'
logfile = './debug.log'
loglevel = 'debug'
timeout = 1000
我在POST路线中看不到会导致这种情况发生特别不同的事情 - 特别是因为它似乎有效了一段时间。
我在一个virtualenv,Flask,在apache2和Ubuntu 14.04上使用gunicorn运行Python 3.
任何帮助将不胜感激!
答案 0 :(得分:0)
我发现解决方案是使用with
语句打开我的pickle文件。似乎可以帮助解决任何开/关冲突。