首页 > 解决方案 > python/java 脚本:如何在 python 服务器上使用子协议?

问题描述

在websocket中我发现连接到带有端口转发的rasberry pi很复杂,当客户端尝试连接服务器时看到它但不是客户端只看到他在他提出这个错误后已经被接受

start serveur
start 5677
connect
Error in connection handler
Traceback (most recent call last):
  File "/usr/lib/python3.5/asyncio/tasks.py", line 241, in _step
    result = coro.throw(exc)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 674, in transfer_data
    message = yield from self.read_message()
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 742, in read_message
    frame = yield from self.read_data_frame(max_size=self.max_size)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 815, in read_data_frame
    frame = yield from self.read_frame(max_size)
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 884, in read_frame
    extensions=self.extensions,
  File "/usr/local/lib/python3.5/dist-packages/websockets/framing.py", line 99, in read
    data = yield from reader(2)
  File "/usr/lib/python3.5/asyncio/streams.py", line 668, in readexactly
    yield from self._wait_for_data('readexactly')
  File "/usr/lib/python3.5/asyncio/streams.py", line 458, in _wait_for_data
    yield from self._waiter
  File "/usr/lib/python3.5/asyncio/futures.py", line 380, in __iter__
    yield self  # This tells Task to wait for completion.
  File "/usr/lib/python3.5/asyncio/tasks.py", line 304, in _wakeup
    future.result()
  File "/usr/lib/python3.5/asyncio/futures.py", line 285, in result
    raise CancelledError
concurrent.futures._base.CancelledError

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/websockets/server.py", line 169, in handler
    yield from self.ws_handler(self, path)
  File "webserveur.py", line 6, in Time
    a=await websocket.recv()
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 434, in recv
    yield from self.ensure_open()
  File "/usr/local/lib/python3.5/dist-packages/websockets/protocol.py", line 658, in ensure_open
    ) from self.transfer_data_exc
websockets.exceptions.ConnectionClosed: WebSocket connection is closed: code = 1006 (connection closed abnormally [internal]), no reason
^CTraceback (most recent call last):
  File "webserveur.py", line 19, in <module>
    loop.run_forever()
  File "/usr/lib/python3.5/asyncio/base_events.py", line 421, in run_forever
    self._run_once()
  File "/usr/lib/python3.5/asyncio/base_events.py", line 1388, in _run_once
    event_list = self._selector.select(timeout)
  File "/usr/lib/python3.5/selectors.py", line 445, in select
    fd_event_list = self._epoll.poll(timeout, max_ev)
KeyboardInterrupt

这就是客户端出现的情况: 客户合影 所以为了帮助我考虑使用子协议,但我在谷歌上没有发现代码或堆栈溢出,所以我问了这个问题how can i use sub protocols with a python server

<!DOCTYPE html>
<html>
<head>
    <title>hello server</title>
</head>
<body>
    

    <script>
        alert("started");

        var ws = new WebSocket("ws://public ip/", "chat-1.0");//chat-1.0 is only to show that i want to use this sub protocol and it hasn't been here for the test with the error below
        ws.onopen = function (event) {
        alert("connect");
        }

    </script>
</body>

服务器

print("start serveur")
import asyncio
import websockets
async def Time(websocket, path):
   print("connect") 
   a=await websocket.recv()
   print(a)
    
loop=asyncio.get_event_loop()  
print("start 5677") 
start_server = websockets.serve(Time, "0.0.0.0", 5678)


websocket2=loop.run_until_complete(start_server)


loop.run_forever()

thanks for helping

标签: javascriptpythonwebsocketraspberry-pi

解决方案


推荐阅读