使用flask,nginx和gunicorn设置gevent-socket

时间:2015-03-26 19:29:29

标签: python-2.7 nginx gunicorn gevent-socketio

以下是我的python代码

from gevent import monkey;monkey.patch_all()
from flask import Flask,render_template, url_for, request, redirect, flash,jsonify,session,Markup
from socketio import socketio_manage
from socketio.namespace import BaseNamespace
from socketio.server import SocketIOServer

app=Flask(__name__)
app.config['SECRET_KEY'] = 'secret!'

class ChatNamespace(BaseNamespace):
  def recv_connect(self):
    print "successfully connected"
    self.emit('show_result','successfully connect')
  def on_receive_message(self,msg):
    print "message is "+msg["data"]
    self.emit('show_result2',msg["data"])
@app.route('/')
def index():
   #print "in the function"
   return render_template('index.html')

@app.route("/socket.io/<path:path>")
 def run_socketio(path):
  socketio_manage(request.environ, {'/test': ChatNamespace})
  return 'ok'

if __name__=='__main__':
  #app.run(debug=True, port=80, host='0.0.0.0')
   app.debug=True
   #app.run()
   SocketIOServer(('0.0.0.0', 5000), app,resource="socket.io").serve_forever()
print "successfull listen to socket"

以下是nginx配置     服务器{

location / {
  proxy_set_header X-Forward-For $proxy_add_x_forwarded_for;
  proxy_set_header Host $http_host;
  proxy_redirect off;
  try_files $uri @proxy;
}
location @proxy {
    proxy_pass http://127.0.0.1:8000;
}
location /templates {
    alias  /home/www/flask_project/templates/;
}
 location /script {
    alias  /home/www/flask_project/script/;
  }
  location /static {
    alias  /home/www/flask_project/static/;
  }

 }

每次运行应用程序时,我都会使用以下命令 gunicorn main2:app -b localhost:5000

我知道我在实时服务器上运行这个gevent-socketio应用程序时遗漏了很多信息。 任何人都可以帮助我,我对这个网络套接字技术完全陌生

1 个答案:

答案 0 :(得分:0)

你试过FlaskSocketio ??

如果您尝试在没有扩展程序的情况下执行此操作,则需要使用socketio gunicorn worker来运行您的应用程序。

gunicorn --worker-class socketio.sgunicorn.GeventSocketIOWorker module:app