简体   繁体   English

Python 使用 ngrok 进行套接字服务器端口转发

[英]Python socket server port forwarding with ngrok

I am making a cli-chat system with python, sockets.我正在使用 python、sockets 制作一个 cli-chat 系统。 When the server and client are in the same machine, the code works fine.当服务器和客户端在同一台机器上时,代码可以正常工作。

But now I am trying to port forward my local server with ngrok, to chat with remote clients.但现在我正在尝试使用 ngrok 将我的本地服务器端口转发,以与远程客户端聊天。

When the client wants to connect he would have to give the server url.当客户端想要连接时,他必须给服务器 url。 If I am giving my localhost url, client connects to the socket server and everything works fine.如果我给我的本地主机 url,客户端连接到套接字服务器,一切正常。

But whenever I am giving the ngrok's tcp tunnel url, it doesn't works and throws the error below但是每当我给 ngrok 的 tcp 隧道 url 时,它就不起作用并抛出下面的错误

Traceback (most recent call last):
  File "/storage/emulated/0/Sun/Python/cmd-chat/user.py", line 5, in <module>
    client.connect((serverUrl, 5000))              #connecting client to server
socket.gaierror: [Errno 7] No address associated with hostname

The url I am giving as input is, tcp://x.tcp.ngrok.io:17xxx The url I am giving as input is, tcp://x.tcp.ngrok.io:17xxx

Hey I think I got where you are going wrong嘿,我想我知道你哪里错了

# Your Server Url should be
serverUrl = hostAddress = "x.tcp.ngrok.io"

# The Port in which we want to connect to the server Should be
port = 17234 # The number given in the end of URL link

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM