def login(ctx, url, username, password):
"""Performs login on the remote server at the specified URL."""
login_url = urljoin(url, "/hub/login")
payload = {"username": username, "password": password}
# Unfortunately, jupyterhub handles the afterlogin with an immediate
# redirection, meaning that we have to check for a 302 and prevent
# redirection in order to capture the cookies.
try:
response = requests.post(login_url, payload, verify=False,
allow_redirects=False)
except Exception as e:
print("Could not perform request. {}".format(e), file=sys.stderr)
sys.exit(1)
if response.status_code == 302:
cookies_dict = requests.utils.dict_from_cookiejar(response.cookies)
cred = Credentials(url, username, cookies_dict)
cred.write(ctx.obj.credentials_file)
else:
print("Failed to perform login. Server replied with error: {}".format(
response.status_code), file=sys.stderr)
sys.exit(1)
# -------------------------------------------------------------------------
评论列表
文章目录