websocket变得奇怪的字符

时间:2012-06-22 08:34:32

标签: python websocket

嗨,我一直在测试websockets,到目前为止,我已将它连接起来了。但是当我开始向服务器发送数据时,我得到了一堆奇怪的字符。

(从搜索中获取这些代码)

这是服务器:

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 "RECV----------------------------------"
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 "SEND----------------------------------"
print response
client.send(response)

while 1:
    try :
        print "SEND----------------------------------"
        client.sendall('hello from server')
        print "RECV----------------------------------"
        print client.recv(1024)
    except :
        print "except"
        break

这是客户:

<!DOCTYPE html>
<html lang="en">
<head>
  <title>WebSocket Echo Client</title>
  <meta charset="UTF-8" />
  <script>
    "use strict";
    // Initialize everything when the window finishes loading
    window.addEventListener("load", function(event) {
      var status = document.getElementById("status");
      var url = document.getElementById("url");
      var open = document.getElementById("open");
      var close = document.getElementById("close");
      var send = document.getElementById("send");
      var text = document.getElementById("text");
      var message = document.getElementById("message");
      var socket;

      status.textContent = "Not Connected";
      url.value = "ws://localhost:8999";
      close.disabled = true;
      send.disabled = true;

      // Create a new connection when the Connect button is clicked
      open.addEventListener("click", function(event) {
        open.disabled = true;
        socket = new WebSocket(url.value, "echo-protocol");

        socket.addEventListener("open", function(event) {
          close.disabled = false;
          send.disabled = false;
          status.textContent = "Connected";
        });

        // Display messages received from the server
        socket.addEventListener("message", function(event) {
          message.textContent = "Server Says: " + event.data;
        });

        // Display any errors that occur
        socket.addEventListener("error", function(event) {
          message.textContent = "Error: " + event;
        });

        socket.addEventListener("close", function(event) {
          open.disabled = false;
          status.textContent = "Not Connected";
        });
      });

      // Close the connection when the Disconnect button is clicked
      close.addEventListener("click", function(event) {
        close.disabled = true;
        send.disabled = true;
        message.textContent = "";
        socket.close();
      });

      // Send text to the server when the Send button is clicked
      send.addEventListener("click", function(event) {
        socket.send(text.value);
        text.value = "";
      });
    });
  </script>
</head>
<body>
  Status: <span id="status"></span><br />
  URL: <input id="url" /><br />
  <input id="open" type="button" value="Connect" />&nbsp;
  <input id="close" type="button" value="Disconnect" /><br />
  <input id="send" type="button" value="Send" />&nbsp;
  <input id="text" /><br />
  <span id="message"></span>
</body>
</html>

这是我点击连接按钮后得到的内容:

RECV----------------------------------
GET / HTTP/1.1
Upgrade: websocket
Connection: Upgrade
Host: localhost:8999
Origin: null
Sec-WebSocket-Protocol: echo-protocol
Sec-WebSocket-Key: v6Fu1rJURofc7iIPbeaw0Q==
Sec-WebSocket-Version: 13
Sec-WebSocket-Extensions: x-webkit-deflate-frame
Cookie: BG_PREFS=searches_includeapocrypha@no&fontsize@medium&language@en&default_version_display@all&default_version@SND&default_
version_overrides@no&quicksearch_search@&pslookup_language1@en&pslookup_language2@&pslookup_language3@&pslookup_language4@&pslooku
p_language5@&pslookup_showmoresearches@closed&pslookup_showversions@open&pslookup_showmoreversions@closed&pslookup_showoptions@ope
n&pslookup_showfootnotes@yes&pslookup_showxrefs@no&pslookup_showwoj@no&pslookup_showversenums@yes&pslookup_showheadings@yes&pslook
up_showindent@no&pslookup_multilayout@columns&pslookup_multisort@passage&pslookup_embed-versenum@true&pslookup_embed-xref@false&ps
lookup_embed-footnote@false&pslookup_embed-heading@false&keysearch_search@&keysearch_language1@en&keysearch_language@en&
SEND----------------------------------
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Protocol: echo-protocol
Sec-WebSocket-Accept: qVkmFtRs1w8OUwZSe5nMpTWNxbI=


SEND----------------------------------
RECV----------------------------------
keysearch_bookset@&keysearch_spanbegin@1&keysearch_spanend@73&keysearch_limit@none&keysearch_startnumber@1&keysearch_searchtype@al
l&keysearch_showversions@open&keysearch_showmoreversions@closed&keysearch_showoptions@open&keysearch_displayas@long&keysearch_resu
ltspp@25&keysearch_sort@bookorder&keysearch_wholewordsonly@no&commentary_source@1&topindex_source@1&topindex_search@&topindex_sear
ch_type@any&topindex_resultspp@25&audio_source@3&audio_book@&audio_chapter@&dict_source@1&dict_search@&dict_search_type@any&pslook
up_search@>>>>&pslookup_version@NIV>>>>&undefined&keysearch_version@31>>>>; CoreID6=83844698672113373932637&ci=90320803; __atuvc=2
|20

握手似乎很好,它连接起来。 然后当我从客户端发送文本“test”时,我得到了这个。

üäQk╩¼%♫╣╪

这与字符编码有关吗? 我无法获得我发送到服务器的“测试”字符串。 我也没有在客户端得到任何东西。

注意: 我在Chrome 19上测试了客户端。

2 个答案:

答案 0 :(得分:2)

根据RFC,客户端不会发送文本数据,而是binary frames

答案 1 :(得分:1)

WebSockets是一种框架协议。它与原始套接字具有相似的延迟,但数据不是通过线路原始发送的:

  • 标题:帧的开头有一个双字节标题。如果有效负载分别长于125字节或65535字节,这也可以是4字节或10字节的报头。

  • 二进制或文本:标题还指示数据是二进制还是文本。在您的情况下,数据是文本。要发送二进制数据,您必须从Javascript发送ArrayBuffer或Blob。如果发送字符串,则数据将为文本。如果服务器发送二进制帧,则onmessage事件将接收blob或arraybuffer,具体取决于WebSocket.binaryType字段的设置。

  • 屏蔽:必须屏蔽从客户端(浏览器)到服务器的所有数据。这是为了解决行为不当缓存中介的理论问题。不得屏蔽从服务器到客户端的数据。标头有一个位,指示有效负载是否被屏蔽。如果它被屏蔽,则标题之后的前四个字节是掩码。这将作为正在运行的XOR应用于有效负载数据,以便取消屏蔽它。

在您的示例中,您发送了一个文本字符串“test”。服务器接收10个字节的数据:2字节头,4字节掩码,4字节掩码有效载荷。

请参阅section 5.2 of the IETF 6455 WebSocket protocol specification,了解框架的工作原理以及标题中字段的位级细分。