python websocket握手(RFC 6455) [英] python websocket handshake (RFC 6455)

查看:88
本文介绍了python websocket握手(RFC 6455)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用RFC 6455协议在python上实现一个简单的websoket服务器.我从此处

I am trying to implement a simple websoket server on python, using RFC 6455 protocol. I took handshake format from here and here.

我正在使用Chromium 17和Firefox 11作为客户端,并出现此错误:

I am using Chromium 17 and Firefox 11 as clients, and getting this error:

未捕获的错误:INVALID_STATE_ERR:DOM异常11

我希望在浏览器中看到来自服务器的 hello ,在服务器日志中看到来自客户端的 hello .

I expect to see hello from server in my browser and hello from client in server log.

我想我的握手错了,你能指出我的错误吗?

I guess my handshake is wrong, can you point me to my mistake?

GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: 127.0.0.1:8999
Origin: null
Sec-WebSocket-Key: 8rYWWxsBPEigeGKDRNOndg==
Sec-WebSocket-Version: 13

服务器日志,响应:

HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: 3aDXXmPbE5e9i08zb9mygfPlCVw=

原始字符串响应:

HTTP/1.1 101 Switching Protocols\r\nUpgrade: websocket\r\nConnection: Upgrade\r\nSec-WebSocket-Accept: 3aDXXmPbE5e9i08zb9mygfPlCVw=\r\n\r\n

服务器代码:

import socket
import re
from base64 import b64encode
from hashlib import sha1

websocket_answer = (
    'HTTP/1.1 101 Switching Protocols',
    'Upgrade: websocket',
    'Connection: Upgrade',
    'Sec-WebSocket-Accept: {key}\r\n\r\n',
)

GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('127.0.0.1', 8999))
s.listen(1)

client, address = s.accept()
text = client.recv(1024)
print text

key = (re.search('Sec-WebSocket-Key:\s+(.*?)[\n\r]+', text)
    .groups()[0]
    .strip())

response_key = b64encode(sha1(key + GUID).digest())
response = '\r\n'.join(websocket_answer).format(key=response_key)

print response
client.send(response)

print client.recv(1024)
client.send('hello from server')

客户代码:

<!DOCTYPE html>
<html>
<head>
    <title>test</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <script type="text/javascript">
        var s = new WebSocket('ws://127.0.0.1:8999');
        s.onmessage = function(t){alert(t)};
        s.send('hello from client');
    </script>
</head>
<body>
</body>
</html>

推荐答案

您的服务器握手代码看起来不错.

Your server handshake code looks good.

但是,客户端代码看起来将尝试在(异步)握手完成之前发送消息.您可以通过将消息发送到websocket的onopen方法中来避免这种情况.

The client code looks like it'll try to send a message before the (asynchronous) handshake completes however. You could avoid this by moving your message send into your websocket's onopen method.

建立连接后,服务器不会以纯文本形式发送或接收消息.有关详细信息,请参见规范的数据框架部分.(客户端代码可以忽略此操作,因为浏览器会为您处理数据框架.)

Once the connection is established, the server does not send or receive messages as plain text. See the data framing section of the spec for details. (Client code can ignore this as the browser takes care of data framing for you.)

这篇关于python websocket握手(RFC 6455)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆