Scalable Server to listen to POST messages

我是研究僧i 提交于 2019-12-13 04:55:01

问题


I have a python Flask listener waiting on port 8080.I expect a client to POST documents to this listener.

#!/usr/bin/env python2
from __future__ import print_function
from flask import Flask, request
from flask.ext.cors import CORS
from datetime import datetime
import os, traceback, sys 
import json
import os

app = Flask('__name__')
cors = CORS(app)

@app.route('/',methods=['GET','POST','OPTIONS'])                                                                                                                                         
def recive_fe_events():
    try:
        data = request.get_data()

        if request.content_length < 20000 and request.content_length != 0:
            filename = 'out/{0}.json'.format(str(datetime.now()))
            with open(filename, 'w') as f:
                 f.write(data)

            print('Wrote', filename)
        else:
            print("Request too long", request.content_length)
            content = '{{"status": 413, "content_length": {0}, "content": "{1}"}}'.format(request.content_length, data)
            return content, 413 
    except:
        traceback.print_exc()
        return None, status.HTTP_500_INTERNAL_SERVER_ERROR

    return '{"status": 200}\n'

if __name__ == '__main__':
    app.run(host='0.0.0.0',debug=False,port=8080)

This server is able to listen to POST messages and decode them and dump them to a file.However there is a concern that the frequency of POST messages may become very high. I am thinking about ways to make this application scalable. What is the best way.Do I implement a load balancer in front, listening on port 8080 and then balance traffic across multiple listeners on 8081,8082 and 8083 for example.

来源:https://stackoverflow.com/questions/30362121/scalable-server-to-listen-to-post-messages

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!