0
我剛剛第一次進入Flask,並試圖用它創建一個具有GET,POST和DELETE路由的API來管理URL黑名單。Flask POST路由變得無響應
經過一段時間POST路線給我一個錯誤:「無法得到任何迴應。」奇怪的部分是,我的GET和DELETE路線仍然正常工作。我試圖讓一些日誌記錄正在進行,但一直未能獲得太多。我會很感激任何幫助!
這裏是我的應用程序:
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
這裏是我使用啓動工人的命令:
gunicorn -c app.conf -w3 --certfile=/etc/letsencrypt/live/mtfmu4.tk/cert.pem --keyfile=/etc/letsencrypt/live/mtfmu4.tk/privkey.pem api:app
我看不出有什麼特別不同,在我的POST路線會導致這種情況發生 - 特別是因爲它似乎工作一個小時即
我在virtualenv中運行的Python 3,瓶,gunicorn上的Apache2和Ubuntu 14.04。
任何幫助,將不勝感激!
你如何發佈數據?即使用curl或其他一些python腳本? – jprockbelly
實際上,我使用jQuery Ajax調用進行POST。我有一種感覺,我可能已經解決了這個問題,但我會再給它12個小時以查看它是否再次崩潰。如果出現問題,將在下面發佈我的解決方案 –