2012-05-10 5 views
2

기본 웹 소켓 서버를 만들려고하는데 서버가 클라이언트에서 핸드 셰이크를 받지만 클라이언트가 서버의 핸드 셰이크 응답을 받아들이지 않은 것으로 보입니다. 나는 'Sec-WebSocket-Key'에 대해서도 의심 스럽다. 해쉬 값이 너무 길다고 생각한다. 감사합니다 :)HTML5 WebSocket 서버

import socket 


def handle(s): 
    print repr(s.recv(4096)) 


s = socket.socket() 
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR,1) 
s.bind(('',9876)) 
s.listen(2) 

handshakes='\ 
HTTP/1.1 101 Web Socket Protocol Handshake\r\n\ 
Upgrade: WebSocket\r\n\ 
Connection: Upgrade\r\n\ 
WebSocket-Origin: null\r\n\ 
WebSocket-Location: ws://localhost:9876/\r\n\ 
' 
def handshake(hs): 
    hslist = hs.split('\r\n') 
    body = hs.split('\r\n\r\n')[1] 
    key = '' 
    cc = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11' 

    for h in hslist: 
     if h.startswith('Sec-WebSocket-Key:'): 
      key = h[19:] 
     else: 
      continue 

    print key 

    import hashlib 
    import base64 
    s = hashlib.sha1() 
    s.update(key+cc) 
    h = s.hexdigest() 
    print 's = ', s 
    print 'h = ', h 
    return base64.b64encode(h) 

while True: 
    c,a = s.accept() 
    print c 
    print a 
    msg = c.recv(4096) 
    if(msg): 

     print msg 
     print 'sending handshake ...' 
     handshakes += 'Sec-WebSocket-Accept: '+str(handshake(msg))+'\r\n\r\n' 
     print handshakes 
     c.send(handshakes) 
      c.send('Hello !') 
      break; 

[EDITED]

클라이언트 :

<!DOCTYPE html> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
    <head> 
    <title>Web Socket Example</title> 
    <meta charset="UTF-8"> 
    <script> 
     window.onload = function() { 
     var s = new WebSocket("ws://localhost:9876/"); 
     s.onopen = function(e) { alert("opened"); } 
     s.onclose = function(e) { alert("closed"); } 
     s.onmessage = function(e) { alert("got: " + e.data); } 
     }; 
    </script> 
    </head> 
    <body> 
     <div id="holder" style="width:600px; height:300px"></div> 
    </body> 
</html> 

서버 출력 :

<socket._socketobject object at 0xb727bca4> 
('127.0.0.1', 46729) 
GET/HTTP/1.1 
Host: localhost:9876 
User-Agent: Mozilla/5.0 (X11; Linux i686; rv:15.0) Gecko/15.0 Firefox/15.0a1 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8 
Accept-Language: en-us,en;q=0.5 
Accept-Encoding: gzip, deflate 
Connection: keep-alive, Upgrade 
Sec-WebSocket-Version: 13 
Origin: null 
Sec-WebSocket-Key: wZG2EaSH+o/mL0Rr9Efocg== 
Pragma: no-cache 
Cache-Control: no-cache 
Upgrade: websocket 


sending handshake ... 
wZG2EaSH+o/mL0Rr9Efocg== 
s = <sha1 HASH object @ 0xb729d660> 
h = 49231840aae5a4d6e1488a4b34da39af372452a9 
HTTP/1.1 101 Web Socket Protocol Handshake 
Upgrade: WebSocket 
Connection: Upgrade 
WebSocket-Origin: null 
WebSocket-Location: ws://localhost:9876/ 
Sec-WebSocket-Accept: NDkyMzE4NDBhYWU1YTRkNmUxNDg4YTRiMzRkYTM5YWYzNzI0NTJhOQ== 


handshake sent 
+0

샘플 핸드 셰이크 요청 사본과 서버의 응답을 게시 할 수 있습니까? – simonc

+0

핸드 셰이크()에서 반환 된 데이터에 str()을 사용해야합니까? 핸드 셰이크()가 이미 문자열을 반환 할 것으로 예상 했었습니다. – simonc

+0

안녕하세요, 나는 서버의 답변과 클라이언트 코드 소스를 추가하여 게시물을 편집했습니다. – rednaks

답변

2

난 당신이 hexdigest() 대신 sha.digest()를 호출 할 필요가 있다고 생각합니다. Base64 인코더에 20 바이트 바이너리 해시를 전달하려고합니다. digest()hexdigest()은 각 바이트를 2 바이트 16 진수 표현으로 변환합니다.

자세한 내용은 python sha docs을 참조하십시오.