簡體   English   中英

Python / JavaScript - WebSockets 連接問題

[英]Python / JavaScript - WebSockets connection problem

我在使用WebSockets服務器時遇到了幾個錯誤。

以前,我在建立連接時確認服務器時遇到了很多問題。 在綁定地址中,我有一個等於0.0.0.0的空字符串,所以我認為問題可能來自那里。 目前,客戶端只能在服務器停止time異步 function 時知道它已連接。 我有時會收到錯誤

send.html:14 WebSocket connection to ws://86.205.245.32:5678/ failed: WebSocket opening handshake timed out

但很少。

我有很多問題可以回答這個帖子。

  • 可以使用子協議幫助嗎? 如果是,如何將它與 Python 服務器一起使用?

  • 可以使用 WSS 幫助嗎?

  • 我是否需要將0.0.0.0的綁定 IP 更改為另一個?

服務器

print("start serveur")
import time, websockets, asyncio

async def time(websocket, path):
    print("connect")
    print(websocket)
    print(await websocket.recv())
start_server = websockets.serve(time, "", 5678)
loop=asyncio.get_event_loop() 

loop.run_until_complete(start_server)


loop.run_forever()

服務器的回溯

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 8, in time
    print(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

客戶端

<script>
    alert("hello");

    var ws = new WebSocket("ws://86.205.245.32:5678/"  );//1*
        
    ws.onopen = function (event) {
       alert("had a handshake answer that cause a connection");
       ws.send("hello");
    }

</script>

WebSockets 請求

General
Request URL: ws://86.205.245.32:5678/
Request Headers
Provisional headers are shown
Accept-Encoding: gzip, deflate
Accept-Language: en-GB,en;q=0.9,en-US;q=0.8,fr;q=0.7
Cache-Control: no-cache
Connection: Upgrade
Host: 86.205.245.32:5678
Origin: file://
Pragma: no-cache
Sec-WebSocket-Extensions: permessage-deflate; client_max_window_bits
Sec-WebSocket-Key: M+CfTACesBfbhkWnFclrsA==
Sec-WebSocket-Version: 13
Upgrade: websocket
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.116 Safari/537.36

我想使用DMZ ,因為我的網絡很奇怪,有很多保護,所以我認為將服務器置於非軍事區會有所幫助。 目前,我無法告訴您它是否有幫助,因為目前當客戶端執行第1* see on the client code ,它無法連接到服務器。 奇怪的是,Windows電腦上沒有DMZ和服務器,都是一樣的,只是為了發送數據,客戶端只有在服務器關閉連接時才能看到服務器的數據。

我嘗試了什么

  • 我試圖停用防火牆
  • 我在 StackOverflow 上提問

當我在客戶端執行期間關閉服務器時,我經常遇到這個問題,即使它沒有執行打印print("connect")的阻塞time

Task was destroyed but it is pending!
task: <Task pending coro=<WebSocketServerProtocol.handler() running at /usr/local/lib/python3.5/dist-packages/websockets/server.py:117> wait_for=<Future pending cb=[Task._wakeup()]>>

我還按如下方式更改了客戶端的代碼,但沒有成功。

var ws = new WebSocket("ws://86.205.245.32:5678/"),
        messages = document.createElement('ul');

        ws.onopen = function (event) {
        alert("connect");
        ws.send("hello");
        
        }

硬件

  • 一台Windows電腦(客戶端)

  • 樹莓派(服務器)

感謝您的幫助,讓我知道您是否已經遇到此問題以及您采取了哪些措施來解決它。 或者,如果您在代碼中發現可能導致此類問題的錯誤

謝謝

我試圖重現您的問題,但我認為您的問題是由您的網絡基礎設施引起的。

服務端客戶端都很基礎,運行流暢。

將您的問題分為不同的階段:

  1. 使用您的本地環境調試代碼(在您的 PC 上的localhost中的客戶端服務器
  2. 使用您的本地網絡在客戶端服務器之間進行第一次隔離
  3. 使用 Cloud Providers 上的一些免費計划來測試擴展網絡(AWS、GCP、DigitalOcean)

一些建議可能是:

  • 完全禁用 Windows 防火牆
  • 檢查您的 Linux 發行版(UFW、IPTables ......)上沒有防火牆
  • 使用 Raspberry Pi 的 LAN IP 進行測試,而不是其公共 IP
  • 確保在Livebox Play中沒有設置防火牆或某些路由規則

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM