Python/JavaScript-WebSockets连接问题
我在使用WebSockets服务器时出现了几个错误 以前,当建立连接时,我在确认服务器时遇到很多问题。在绑定地址中,我有一个空字符串,它等于Python/JavaScript-WebSockets连接问题,javascript,python,websocket,portforwarding,dmz,Javascript,Python,Websocket,Portforwarding,Dmz,我在使用WebSockets服务器时出现了几个错误 以前,当建立连接时,我在确认服务器时遇到很多问题。在绑定地址中,我有一个空字符串,它等于0.0.0,因此我认为问题可能来自于此。目前,只有当服务器停止timeasync函数时,客户端才能知道它已连接。我有时会出错 send.html:14到ws://86.205.245.32:5678的WebSocket连接/失败:WebSocket打开握手超时 但很少 我有很多问题可以回答这个帖子。 使用subtocol有帮助吗?如果是,如何将其用于Pyt
0.0.0
,因此我认为问题可能来自于此。目前,只有当服务器停止time
async函数时,客户端才能知道它已连接。我有时会出错
send.html:14到ws://86.205.245.32:5678的WebSocket连接/失败:WebSocket打开握手超时
但很少
我有很多问题可以回答这个帖子。
- 使用subtocol有帮助吗?如果是,如何将其用于Python服务器
- 使用WSS有帮助吗
- 是否需要将
的绑定IP更改为另一个0.0.0.0
1*行时,它无法连接到服务器。奇怪的是,如果没有DMZ和Windows计算机上的服务器,这是相同的,但对于发送数据,客户端只能在服务器关闭连接时看到服务器的数据
我尝试了什么
- 我试图关闭防火墙
- 我问了一些关于StackOverflow的问题
当我在客户端执行期间关闭服务器时,我经常会遇到这种问题,即使它没有执行打印打印(“连接”)的块时间
:
硬件
- Windows计算机(客户端)
- Rasberry Pi(服务器)
谢谢你的帮助,如果你已经有了这个问题,请告诉我你做了什么来解决它。或者如果您在代码中发现可能导致此类问题的错误
谢谢我试图重现您的问题,但我认为您的问题是由您的网络基础设施造成的
服务器和客户端是非常基本的,它们工作顺利
将问题分为不同阶段:
使用本地环境调试代码(localhost
中的客户端和服务器,在PC上)
使用本地网络在客户端和服务器之间进行第一次隔离
使用云提供商的一些免费计划来测试扩展网络(AWS、GCP、DigitalOcean)
一些建议可能是:
- 完全禁用Windows防火墙
- 检查您的Linux发行版上是否没有防火墙(UFW、IPTables…)
- 使用Raspberry Pi的LAN IP进行测试,而不是使用其公共IP
- 确保在Livebox游戏中没有防火墙或一些路由规则
您好,感谢您的第一点,我已经完全禁用了所有windows防火墙,第二点我们告诉我rasberry pi上没有带debian的防火墙,第三点它可以与lan ip一起工作。关于最后一点,我已经做了portfowarding,即使在防火墙中我做了个性化设置,并说接受端口中的每个连接,或者使用最低保护,它仍然不起作用。但是,即使我在portfowarding中的socket有问题,我也可以让客户端用同一台计算机和同一个网络连接到服务器。他有一种方法可以使用websocket连接到pythonhie中最接近socket的服务器,我没有钱每天支付300欧元,它与socket一起工作的事实并没有给你线索,websocket配置中没有问题吗?你不认为子程序会有帮助吗?你的代码没有问题,客户端和服务器都正确配置了。您的问题来自您的网络基础架构。
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>
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
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");
}