2012-01-29 53 views
0

我試圖實現我的第一個websocket的例子,但我不能讓它的工作。Python的WebSocket不工作

我用一個python web服務器:

import threading 
import socket 

def start_server(): 
    tick = 0 
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) 
    sock.bind(('localhost', 1234)) 
    sock.listen(100) 
    while True: 
     print 'listening...' 
     csock, address = sock.accept() 
     tick+=1 
     print 'connection!' 
     handshake(csock, tick) 
     print 'handshaken' 
     while True: 
      interact(csock, tick) 
      tick+=1 


def send_data(client, str): 
    #_write(request, '\x00' + message.encode('utf-8') + '\xff') 
    str = '\x00' + str.encode('utf-8') + '\xff' 
    return client.send(str) 

def recv_data(client, count): 
    data = client.recv(count)  
    return data.decode('utf-8', 'ignore') 

def handshake(client, tick): 
    our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:   WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n" 
    shake = recv_data(client, 255) 
    print shake 
    #We want to send this without any encoding 
    client.send(our_handshake) 

def interact(client, tick): 
    data = recv_data(client, 255) 
    print 'got:%s' %(data) 
    send_data(client, "clock ! tick%d" % (tick)) 
    send_data(client, "out ! %s" %(data)) 

if __name__ == '__main__': 
    start_server() 

和HTML:

<!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:1234/"); 
     s.onopen = function(e) { s.send('Ping'); } 
     s.onmessage = function(e) { alert("got: " + e.data); } 
     s.onclose = function(e) { alert("closed"); } 
     }; 
    </script> 
    </head> 
    <body> 
     <div id="holder" style="width:600px; height:300px"></div> 
    </body> 
</html> 

當我指出我的瀏覽器http://localhost/websocket.html過的Apache2

我得到了以下錯誤:

python websocketserver.py 
listening... 
connection! 
GET/HTTP/1.1 
Upgrade: websocket 
Connection: Upgrade 
Host: localhost:1234 
Origin: http://localhost 
Sec-WebSocket-Key: A4sVkUhjVlTZbJrp2NUrqg== 
Sec-WebSocket-Version: 13 


handshaken 
got: 
Traceback (most recent call last): 
    File "websocketserver.py", line 43, in <module> 
    start_server() 
    File "websocketserver.py", line 17, in start_server 
    interact(csock, tick) 
    File "websocketserver.py", line 40, in interact 
    send_data(client, "out ! %s" %(data)) 
    File "websocketserver.py", line 24, in send_data 
    return client.send(str) 
socket.error: [Errno 32] Broken pipe 

有人可以幫我解決這個問題嗎?

感謝

+1

我的建議是,不要試圖自己實現WebSocket堆棧,而是使用支持它們的服務器和庫。例如,一些起點https://duckduckgo.com/?q=!pypi+websocket – 2012-01-29 13:57:24

+0

Tornado有一個websocket實現。 – 2012-01-29 15:03:48

回答

3

你與老Hixie 75協議響應,但客戶只能講新HyBi/IETF RFC 6455 WebSocket協議。

你的握手迴應應該看起來更像這個(在接受值從鍵值計算從客戶端):

HTTP/1.1 101 Switching Protocols 
Upgrade: websocket 
Connection: Upgrade 
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo= 

在HyBi/6455,該框架將不再與\ X00分隔和\ xff。相反,每個幀都有一個標題,其中包含多個數據段,包括幀類型和有效負載長度。

有關更多信息,請參閱spec。或者更好的是,您可以參考和/或使用現有的python WebSocket實現,例如pywebsocket,tornado或我自己的項目websockify,其中包含websocket.py,它是一個通用的websocket服務器庫。

+0

謝謝隊友。我會嘗試使用websockify。我已經檢查過您已經建立的VNC,並且我會從那裏採納想法,以便構建我自己的實施並滿足我的需求。我想早些給你發電子郵件,但我不能通過github向你發送電子郵件。 – glarkou 2012-01-30 00:56:44

+0

kanaka,我可以在哪裏找到一個簡單的websockify例子,用python服務器發送圖像?還有最後一個問題。對於沒有啓用HTML5的瀏覽器的任何人,websockify是否可以使用flashs版本的websockets? – glarkou 2012-02-03 20:04:41

+0

我沒有發送圖像的簡單示例,但是如果使用websockify和websock.js發送原始數據字符串,則會在另一端將其作爲二進制字符串獲取。是的,websockify支持[web-socket-js](htttp://github.com/kanaka/websockify)和include/websock.js會自動加載它,如果需要的話。 – kanaka 2012-02-06 22:53:54