受ipython-notebook-proxy启发,基于ipydra,并扩展后者以支持更复杂的用户身份验证以及代理,因为在我的用例中,只能公开端口 80。
我正在为工作人员使用烧瓶套接字gunicorn
,但在代理 WebSocket 时遇到了麻烦。IPython 使用三种不同的 WebSockets 连接,/shell
、/stdin
和/iopub
,但我只能获得101 Switching Protocols
前两个的连接。并在创建后立即/stdin
接收Connection Close Frame
。
这是有问题的摘录代码:
# Flask imports...
from werkzeug import LocalProxy
from ws4py.client.geventclient import WebSocketClient
# I use my own LocalProxy because flask-sockets does not support Werkzeug Rules
websocket = LocalProxy(lambda: request.environ.get('wsgi.websocket', None))
websockets = {}
PROXY_DOMAIN = "127.0.0.1:8888" # IPython host and port
methods = ["GET", "POST", "PUT", "DELETE", "HEAD", "OPTIONS", "PATCH",
"CONNECT"]
@app.route('/', defaults={'url': ''}, methods=methods)
@app.route('/<path:url>', methods=methods)
def proxy(url):
with app.test_request_context():
if websocket:
while True:
data = websocket.receive()
websocket_url = 'ws://{}/{}'.format(PROXY_DOMAIN, url)
if websocket_url not in websockets:
client = WebSocketClient(websocket_url,
protocols=['http-only', 'chat'])
websockets[websocket_url] = client
else:
client = websockets[websocket_url]
client.connect()
if data:
client.send(data)
client_data = client.receive()
if client_data:
websocket.send(client_data)
return Response()
我也尝试创建自己的 WebSocket 代理类,但它也不起作用。
class WebSocketProxy(WebSocketClient):
def __init__(self, to, *args, **kwargs):
self.to = to
print(("Proxy to", self.to))
super(WebSocketProxy, self).__init__(*args, **kwargs)
def opened(self):
m = self.to.receive()
print("<= %d %s" % (len(m), str(m)))
self.send(m)
def closed(self, code, reason):
print(("Closed down", code, reason))
def received_message(self, m):
print("=> %d %s" % (len(m), str(m)))
self.to.send(m)
常规的请求-响应周期就像一个魅力,所以我删除了那个代码。如果有兴趣,完整的代码托管在hidra中。
我运行服务器
$ gunicorn -k flask_sockets.worker hidra:app