受 ipython-notebook-proxy 启发,基于 ipydra 并扩展后者以支持更复杂的用户认证和代理,因为在我的用例中只能公开端口80。
我正在使用 flask-sockets 作为 gunicorn
工作进程,但我遇到了代理 WebSockets 的问题。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