Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/446.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript WebSocket python服务器和JS客户端握手错误_Javascript_Python_Sockets_Websocket - Fatal编程技术网

Javascript WebSocket python服务器和JS客户端握手错误

Javascript WebSocket python服务器和JS客户端握手错误,javascript,python,sockets,websocket,Javascript,Python,Sockets,Websocket,我正在尝试设置python脚本(它将对javascript无法完成的数据进行大量计算,并将数据作为json发送)和javascript客户端之间的通信 我的python服务器有以下代码: import socket import sys from thread import * HOST = '' # Symbolic name meaning all available interfaces PORT = 9888 # Arbitrary non-privileged port s =

我正在尝试设置python脚本(它将对javascript无法完成的数据进行大量计算,并将数据作为json发送)和javascript客户端之间的通信

我的python服务器有以下代码:

import socket
import sys
from thread import *

HOST = ''   # Symbolic name meaning all available interfaces
PORT = 9888 # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
s.listen(10)
print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(conn):
    #Sending message to connected client
    conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string

    #infinite loop so that function do not terminate and thread do not end.
    while True:

        #Receiving from client
        data = conn.recv(1024)
        reply = 'OK...' + data
        if not data: 
            break

        conn.sendall(reply)

    #came out of loop
    conn.close()

#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    conn, addr = s.accept()
    print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    start_new_thread(clientthread ,(conn,))

s.close()
下面是我的javascript客户端的代码:

var connection = new WebSocket('ws://127.0.0.1:8999');
connection.onopen = function () {
  connection.send('Hello'); // Send the message to the server
};
Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
我从javascript客户端收到以下错误:

var connection = new WebSocket('ws://127.0.0.1:8999');
connection.onopen = function () {
  connection.send('Hello'); // Send the message to the server
};
Error during WebSocket handshake: net::ERR_INVALID_HTTP_RESPONSE
以及我的python服务器的以下输出

Socket created
Socket bind complete
Socket now listening
Connected with 127.0.0.1:53956
Unhandled exception in thread started by <function clientthread at 0x10abac578>
Traceback (most recent call last):
  File "server.py", line 71, in clientthread
    data = conn.recv(1024)
socket.error: [Errno 54] Connection reset by peer
已创建套接字
套接字绑定完成
套接字正在侦听
与127.0.0.1:53956连接
由启动的线程中存在未处理的异常
回溯(最近一次呼叫最后一次):
clientthread中第71行的文件“server.py”
数据=conn.recv(1024)
socket.error:[Errno 54]对等方重置连接
有人知道怎么了吗


编辑:忘记提到我以前见过这个,但我的问题不一样,或者更确切地说,OP遇到的错误与我的不一样。

WebSocket与您创建的普通TCP套接字不同。WebSocket是TCP之上的一种协议,它从HTTP握手开始,然后继续使用基于帧的协议。如果要用Python实现WebSocket服务器,则需要按照或使用中的指定实现此协议

使用WebSocket的服务器端python代码示例如下:

import asyncio
import websockets

async def handle_message(message):
    print(message)

async def consumer_handler(websocket, path):
    while True:
        message = await websocket.recv()
        await handle_message(message)

start_server = websockets.serve(consumer_handler, 'localhost', 8765)

asyncio.get_event_loop().run_until_complete(start_server)
asyncio.get_event_loop().run_forever()

谢谢你的指点。我接受了你的答案,并在这里添加了一个示例代码,使其更加完整。