#!/usr/bin/python # # http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-06#section-1.2 # # Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ== # # For this header, the server has to take the value (as present in the # header, e.g. the base64-encoded version), and concatenate this with # the GUID "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" in string form, which # is unlikely to be used by network endpoints that do not understand # the WebSocket protocol. A SHA-1 hash, base64-encoded, of this # concatenation is then returned in the server's handshake # [FIPS.180-2.2002]. # # Concretely, if as in the example above, header |Sec-WebSocket-Key| # had the value "dGhlIHNhbXBsZSBub25jZQ==", the server would # concatenate the string "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" to form # the string "dGhlIHNhbXBsZSBub25jZQ==258EAFA5-E914-47DA-95CA- # C5AB0DC85B11". The server would then take the SHA-1 hash of this, # giving the value 0xb3 0x7a 0x4f 0x2c 0xc0 0x62 0x4f 0x16 0x90 0xf6 # 0x46 0x06 0xcf 0x38 0x59 0x45 0xb2 0xbe 0xc4 0xea. This value is # then base64-encoded, to give the value "s3pPLMBiTxaQ9kYGzzhZRbK+xOo=". This value would then be echoed in the header |Sec-WebSocket- # Accept|. from hashlib import * from base64 import * catStr = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11" def getServerSecWebSocketKey(clientKey): buffer = clientKey + catStr buffer = sha1(buffer) return b64encode(buffer.digest()) if __name__ == '__main__': import string headerStr = "Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==" #Splitting, and remowing whitespace clientKey = string.join( headerStr.split("Sec-WebSocket-Key:")[1].split(), "" ) print ( getServerSecWebSocketKey(clientKey) ) print ("s3pPLMBiTxaQ9kYGzzhZRbK+xOo=") raw_input('wait for the key...')