Javascript 网袋。如何正确地将消息从服务器发送回浏览器

Javascript 网袋。如何正确地将消息从服务器发送回浏览器,javascript,python,websocket,Javascript,Python,Websocket,握手之后,我向服务器发送一些“你好” 然后服务器尝试读取此消息并发回另一条消息 `#server.py` .... text = client.recv(1024) client.send('hello from server') 但是js控制台给了我一个错误 到“ws://127.0.0.1:8999/”的WebSocket连接失败:帧头无效 更新 这是我的服务器文件 import socket sock = socket.socket(socket.AF_INET, socket.SOCK

握手之后,我向服务器发送一些“你好”

然后服务器尝试读取此消息并发回另一条消息

`#server.py`
....
text = client.recv(1024)
client.send('hello from server')
但是js控制台给了我一个错误

到“ws://127.0.0.1:8999/”的WebSocket连接失败:帧头无效

更新

这是我的服务器文件

import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 9999))
sock.listen(5)

import hashlib
import base64

def makeHash(key):
    hash0 = key.replace(" ","")+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
        return hash0
handshaken = False

print("TCPServer Waiting for client on port 9999")

import sys

data = ''
header = ''

client, address = sock.accept()
while True:
    if not handshaken:
        data = client.recv(16).decode("utf-8");
        header+=data
        if header.find('\r\n\r\n')>=0:
            key = header.split('Sec-WebSocket-Key: ')[1].split('\r\n')[0]
            hash0 = makeHash(key).encode('utf-8')
            hash1 = hashlib.sha1(hash0)
            ready = base64.b64encode(hash1.digest())
            newKey = str(ready)[2:][:-1]
            answer = "HTTP/1.1 101 Switching Protocols\r\n"+"Upgrade:         websocket\r\n"+"Connection: Upgrade\r\n"+"Sec-WebSocket-Accept: "+newKey+"\r\n\r\n"
        client.send(answer.encode('utf-8'))
        handshaken = True

    else:
        print("-")
else:
    tmp = client.recv(1024)
    data = str(tmp);
    print(data)

    answ = ('Hello').encode('utf-8')
    client.send(answ)

什么是客户?您在Python代码中使用了哪些websocket功能。如果它只是一个普通的套接字,则需要更多的代码。我的意思是
import socket
在网上寻找一些东西,向你展示如何用Python来做websocket的东西,例如免责声明我没有尝试过,但它应该给你一些基础知识。websocket不仅仅是一个普通的TCP套接字。你不能只发送纯文本信息。这是一个基于连接、协议、安全方案和数据帧格式的完整握手。您必须使用确切的webSocket协议才能与webSocket连接进行通信。有许多现有的库已经为几乎所有的语言实现了webSocket协议,因此您当然可以找到一个用于python的库。
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(("", 9999))
sock.listen(5)

import hashlib
import base64

def makeHash(key):
    hash0 = key.replace(" ","")+"258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
        return hash0
handshaken = False

print("TCPServer Waiting for client on port 9999")

import sys

data = ''
header = ''

client, address = sock.accept()
while True:
    if not handshaken:
        data = client.recv(16).decode("utf-8");
        header+=data
        if header.find('\r\n\r\n')>=0:
            key = header.split('Sec-WebSocket-Key: ')[1].split('\r\n')[0]
            hash0 = makeHash(key).encode('utf-8')
            hash1 = hashlib.sha1(hash0)
            ready = base64.b64encode(hash1.digest())
            newKey = str(ready)[2:][:-1]
            answer = "HTTP/1.1 101 Switching Protocols\r\n"+"Upgrade:         websocket\r\n"+"Connection: Upgrade\r\n"+"Sec-WebSocket-Accept: "+newKey+"\r\n\r\n"
        client.send(answer.encode('utf-8'))
        handshaken = True

    else:
        print("-")
else:
    tmp = client.recv(1024)
    data = str(tmp);
    print(data)

    answ = ('Hello').encode('utf-8')
    client.send(answ)