basic_auth_wsh.py (975B)
1 #!/usr/bin/python 2 3 """A WebSocket handler that enforces basic HTTP authentication. Username is 4 'foo' and password is 'bar'.""" 5 6 7 from pywebsocket3.handshake import AbortedByUserException 8 9 10 def web_socket_do_extra_handshake(request): 11 authorization = request.headers_in.get('authorization') 12 if authorization is None or authorization != 'Basic Zm9vOmJhcg==': 13 if request.protocol == "HTTP/2": 14 request.status = 401 15 request.headers_out["Content-Length"] = "0" 16 request.headers_out['www-authenticate'] = 'Basic realm="camelot"' 17 else: 18 request.connection.write(b'HTTP/1.1 401 Unauthorized\x0d\x0a' 19 b'Content-Length: 0\x0d\x0a' 20 b'WWW-Authenticate: Basic realm="camelot"\x0d\x0a' 21 b'\x0d\x0a') 22 raise AbortedByUserException('Abort the connection') 23 24 25 def web_socket_transfer_data(request): 26 pass