Websocket 瓶子+;网袋
是否可以在同一应用程序(同一端口)中承载普通瓶子应用程序和WebSocket应用程序(示例:)?Websocket 瓶子+;网袋,websocket,bottle,Websocket,Bottle,是否可以在同一应用程序(同一端口)中承载普通瓶子应用程序和WebSocket应用程序(示例:)? 因此,/ws将转到WebSocket处理程序,所有其他的将正常路由到其他瓶子处理程序。这很可能不是最简单的答案,但我刚刚完成了一个测试环境的设置,该环境使用Nginx和uWSGI在金字塔上工作,一旦设置好,您就可以非常轻松地扩展它,例如,如果您的/ws占用了太多的资源,那么使用Nginx+uWSGI将/ws重新定位到不同的硬件是很简单的。我的背景是金字塔,我的uWSGI经验只是测试,但在我的阅读中,
因此,/ws将转到WebSocket处理程序,所有其他的将正常路由到其他瓶子处理程序。这很可能不是最简单的答案,但我刚刚完成了一个测试环境的设置,该环境使用Nginx和uWSGI在金字塔上工作,一旦设置好,您就可以非常轻松地扩展它,例如,如果您的/ws占用了太多的资源,那么使用Nginx+uWSGI将/ws重新定位到不同的硬件是很简单的。我的背景是金字塔,我的uWSGI经验只是测试,但在我的阅读中,这似乎是一个应该很有效的解决方案。瓶子说明是谷歌快速搜索的结果。 概念: 这个概念是将你的应用程序,即你的app=make_wsgi_app.from_config(config)置于app.serve_forever()调用之前,然后使用uwsgi将你的应用程序“服务”到一个套接字app1.sock。有许多方法可以配置uWSGI。uWSGI站点提供了更多配置uWSGI与应用程序对话的方法的文档。Nginx配置为至少在当前版本中本地使用uWSGI套接字。你只要通过uwsgi_通行证就行了unix:///tmp/app1.sock;站点配置的路径以及参数。在同一个站点配置文件中执行两次,一次用于location/{,一次用于location/ws{,指向各自的应用程序sock文件,您就可以开始了。 在我设置测试环境时,将应用程序作为文件提供给套接字的概念对我来说是新概念,我希望这会有所帮助。 获得什么: nginx
uWSGI 如何: 将nginx和uwsgi设置信息从应用程序中提取出来,并将其应用到您的应用程序中,用于瓶子特定的设置或前往uwsgi站点查看其配置说明。每种特定技术的文档都非常好,因此即使缺少组合示例,也不难让它们协同工作。使用nginx和uwsgiuWSGI有大量的配置设置,所以一定要查看这些设置。 的确如此 服务器:
#!/usr/bin/python
import json
from bottle import route, run, request, abort, Bottle ,static_file
from pymongo import Connection
from gevent import monkey; monkey.patch_all()
from time import sleep
app = Bottle()
@app.route('/websocket')
def handle_websocket():
wsock = request.environ.get('wsgi.websocket')
if not wsock:
abort(400, 'Expected WebSocket request.')
while True:
try:
message = wsock.receive()
wsock.send("Your message was: %r" % message)
sleep(3)
wsock.send("Your message was: %r" % message)
except WebSocketError:
break
@app.route('/<filename:path>')
def send_html(filename):
return static_file(filename, root='./static', mimetype='text/html')
from gevent.pywsgi import WSGIServer
from geventwebsocket import WebSocketHandler, WebSocketError
host = "127.0.0.1"
port = 8080
server = WSGIServer((host, port), app,
handler_class=WebSocketHandler)
print "access @ http://%s:%s/websocket.html" % (host,port)
server.serve_forever()
瓶子应用程序通常是这样启动的:run(server='gevent',host='localhost',port=8192,debug=True,reloader=True)这些参数现在会去哪里?@spacen jasset run也将其作为一个参数。比如:
battle.run(app=config.application,host='0.0.0',port=3001,reloader=True,server='gevent',handler\u class=WebSocketHandler)
我试图将上述内容添加到一个现有的瓶子应用程序中,确保我已经从gevent.pywsgi import WSGIServer获得了所有导入、/websocket
路径和下面的所有Python代码。使用的Javascript代码如图所示。我已经在virtualenv
中安装了pipgevent
和gevent websocket
>我得到错误:来自geventwebsocket import WebSocketHandler,WebSocketError[Tue Aug 23 14:20:10.425900 2016][wsgi:error][pid 7245:tid 139987894220544][client 127.0.0.1:58218]ImportError:无法从geventwebsocket导入名称WebSocketHandler
@user1063287 use从geventwebsocket导入WebSocketError。handler导入WebSocketHandler
即使没有错误,答案也无法路由到“/”或任何其他非websocket路径。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
var ws = new WebSocket("ws://localhost:8080/websocket");
ws.onopen = function() {
ws.send("Hello, world");
};
ws.onmessage = function (evt) {
alert(evt.data);
};
</script>
</head>
<body>
</body>
</html>
#!/usr/bin/python
from websocket import create_connection
ws = create_connection("ws://localhost:8080/websocket")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Reeiving..."
result = ws.recv()
print "Received '%s'" % result
ws.close()