[英]Unable to connect to local server on WSL2 from Windows host
I have a Python3 project using waitress
to serve on localhost on WSL2/Ubuntu 20. I start the server from VSCode remote but I can't connect to it from a browser on Windows using address http://127.0.0.1:5998 or http://localhost:5998, getting unable to connect error. I have a Python3 project using
waitress
to serve on localhost on WSL2/Ubuntu 20. I start the server from VSCode remote but I can't connect to it from a browser on Windows using address http://127.0.0.1:5998 or http ://localhost:5998,无法连接错误。 I couldn't find what's wrong with it and appreciate any help.我找不到它有什么问题并感谢任何帮助。
Python server: Python 服务器:
@app.route('/')
def index():
return 'Success'
...
if __name__ == '__main__':
from waitress import serve
process_count = multiprocessing.cpu_count()
serve(app, host="0.0.0.0", port=5998, threads=process_count)
I see it listening on WSL:我看到它在 WSL 上收听:
> sudo lsof -i -P -n | grep LISTEN
python3 1263 xxx 8u IPv4 39138 0t0 TCP *:5998 (LISTEN)
I also tried 127.0.0.1 as serve() ip instead of 0.0.0.0 but didn't help.我还尝试了 127.0.0.1 作为 serve() ip 而不是 0.0.0.0 但没有帮助。
I have a rule in Windows firewall for it:我在 Windows 防火墙中有一条规则:
> Get-NetFirewallRule -DisplayName WSL
Name : {9c5c5f2b-a9c7-42b7-82ac-f0c2b1819103}
DisplayName : WSL
Description :
DisplayGroup :
Group :
Enabled : True
Profile : Any
Platform : {}
Direction : Inbound
Action : Allow
EdgeTraversalPolicy : Block
LooseSourceMapping : False
LocalOnlyMapping : False
Owner :
PrimaryStatus : OK
Status : The rule was parsed successfully from the store. (65536)
EnforcementStatus : NotApplicable
PolicyStoreSource : PersistentStore
PolicyStoreSourceType : Local
I've checked ports in use on Windows using netstat -o
and nothing seem to be using port 5998.我已经使用
netstat -o
检查了 Windows 上使用的端口,但似乎没有使用端口 5998。
I've solved this problem adding port forwarding on Windows.我已经解决了在 Windows 上添加端口转发的问题。
Run this on WSL:在 WSL 上运行:
ifconfig
The inet IP on eth0
entry is your WSL IP. eth0
条目上的 inet IP 是您的 WSL IP。
Run this command on Windows:在 Windows 上运行此命令:
netsh interface portproxy add v4tov4 listenport=<port> listenaddress=0.0.0.0 connectport=<port> connectaddress=<your WSL IP>
After this I could connect using localhost:<port>
or 127.0.0.1:<port>
or <WSL IP>:<port>
from browser on Windows.在此之后,我可以从 Windows 上的浏览器使用
localhost:<port>
或127.0.0.1:<port>
或<WSL IP>:<port>
进行连接。
You can list current port proxies using netsh interface portproxy show all
command.您可以使用
netsh interface portproxy show all
命令列出当前端口代理。
You may need to add a firewall rule for your port on Windows.您可能需要为 Windows 上的端口添加防火墙规则。
More info: https://github.com/microsoft/WSL/issues/4150#issuecomment-927091364更多信息: https://github.com/microsoft/WSL/issues/4150#issuecomment-927091364
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.