简体   繁体   English

python 服务器和 javascript 客户端之间的 Websocket

[英]Websocket between python server and javascript client

I am very new to web development, so any tips regarding the following matter will be useful, So.我对 web 开发非常陌生,所以关于以下事项的任何提示都会很有用,所以。 the client written in javascript is supposed to communicate with the server written in python, I am trying to establish websocket connection between two PCs running UBUNTU and Windows OS They work perfectly fine when I run them using UBUNTU. the client written in javascript is supposed to communicate with the server written in python, I am trying to establish websocket connection between two PCs running UBUNTU and Windows OS They work perfectly fine when I run them using UBUNTU. using localhost, Also.使用本地主机,也。 everything works fine when the server is in UBUNTU and the client is in Windows.当服务器位于 UBUNTU 并且客户端位于 Windows 时,一切正常。 Only when the server is located in Windows and the client is in UBUNTU I keep running into the same error: 'Error in connection establishment: net:.ERR_CONNECTION_TIMED_OUT.只有当服务器位于 Windows 并且客户端位于 UBUNTU 时,我才会一直遇到相同的错误:'连接建立错误:net:.ERR_CONNECTION_TIMED_OUT。

I tried turning off the firewall settings in Windows, but it didn't work.我尝试关闭 Windows 中的防火墙设置,但没有成功。

Any input will be very appreciated!任何输入将不胜感激!

Python Server Python 服务器

import asyncio
import websockets

async def hello(websocket, path):
    name = await websocket.recv()
    print(f"< {name}")

    greeting = f"Hello {name}!"

    await websocket.send(greeting)
    print(f"> {greeting}")

start_server = websockets.serve(hello, "localhost", 8765)

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

Javascript Client Javascript 客户端

var ws = new WebSocket("ws://localhost:1337/");

ws.onopen = function(){
    console.log("Connection is Established");
    ws.send("Message to Send");
};

ws.onmessage = function(evt) {
    var received_msg = evt.data;
    console.log(received_msg);
};

Okay, I found what was wrong.好的,我发现了问题所在。 Completely forgot that I had to change my router settings for port forwarding.完全忘记了我必须更改我的路由器设置以进行端口转发。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将javascript客户端连接到python websocket服务器 - connect javascript client to python websocket server javascript客户端和netty服务器之间的安全websocket连接 - secure websocket connection between javascript client and netty server WebSocket javascript客户端和python服务器。 检索输出中的垃圾 - WebSocket javascript client and python server. Retrieving garbage in output WebSocket-python服务器无法使用javascript - WebSocket - python server is not working with javascript 在客户端 javascript 或 websocket 服务器上转义? - Escape in client-side javascript or on websocket server? WebSocket Javascript客户端未从服务器接收消息 - WebSocket Javascript client not receiving messages from server JavaScript和Jetty嵌入式Websocket服务器之间的Websocket困境 - Websocket dilemma between javascript and jetty embedded websocket server 如何使用javascript作为客户端和python作为web服务器在websocets之间进行通信 - How to communicate between websocets using javascript as client and python as web server 如何使用 AJAX 和 Flask 在 python 服务器和 javascript 客户端之间进行通信? - How to communicate between python server and javascript client using AJAX and Flask? 无法在反应客户端和快速服务器之间建立 websocket 连接 - Can't make a websocket connection between react client and express server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM