首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >HTML5 WebSocket服务器

HTML5 WebSocket服务器
EN

Stack Overflow用户
提问于 2012-05-10 12:17:05
回答 1查看 2.8K关注 0票数 2

我试图创建一个基本的websocket服务器,服务器接收来自客户端的握手,但是客户端似乎不接受服务器的握手响应。我对“Sec-WebSocket-Key”也有疑问,我认为哈希值太长了!谢谢:)

代码语言:javascript
复制
 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;

编辑的

客户:

代码语言:javascript
复制
<!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>

服务器输出:

代码语言:javascript
复制
<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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-05-10 21:04:10

我想你需要打电话给sha.digest()而不是hexdigest()。您希望将一个20字节的二进制哈希传递给您的base64编码器;digest()执行此操作,而hexdigest()则将每个字节转换为2字节十六进制表示形式。

有关细节,请参阅python文档

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/10533790

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档