WS | /api/kernels /<kernel_id>/channels | Websocket stream |
With the default handler, that would be logging in with a POST with username/password to the login URL, which sets a cookie, and then including that cookie when making the websocket connection.
Your login_handler_class can override
.get_user(handler)
, which should return the logged-in username (None if not authenticated). I’m not sure how you are implementing custom login, but this is one place where custom logic can reside, in particular the ability to be authenticated without setting any cookies by using a token in headers, etc.-MinRK
On Tue, May 10, 2016 at 7:41 AM, 薛亚兰 <nju08e...@gmail.com> wrote:
Hi MinRK,Thanks for your kind recommedation and suggestion, and we have got the cookie using post method by POST the login url , like the following, and cookies will be stored in the request object y:
para = {'password':'123')
y = s.post(post_url,data=para)
however, we really have no way in how to inlcude the cookie in the websocket connection, could you help give some hint(regardless of the authhandler defined by ourself, just using the default handler), it's really important for us, and we really found no way to solve this. and the way we are using websocket to submit job to kernel is as follows:
wss_url = "ws://asc-08.ma.platformlab.ibm.com:8891/api/kernels/3dde3fd9-cd05-4c74-a48f-168ac105f308/channels?session_id=46FC60A7B6B14D729296D3BC5FE4CC02"
ws.connect(wss_url)the s and ws are objects created with python's request and websocket module.
Cookies can be passed to the websocket via the cookie
kwarg in WebSocket.connect
:
import requests
import websocket
nb_url = 'https://notebook-host:8888'
password = 'yourpasword'
kernel_id = 'u-u-i-d'
# login with requests
login = requests.post(nb_url + '/login', data={'password': password}, allow_redirects=False)
login.raise_for_status()
cookie = login.headers['set-cookie']
# connect websocket with our login cookie:
ws_url = 'ws' + nb_url[4:] # http[s] -> ws[s]
ws = websocket.WebSocket()
ws.connect('{0}/api/kernels/{1}/channels'.format(ws_url, kernel_id), cookie=cookie)
-MinRK