简体   繁体   English

无法从 Windows 主机连接到 WSL2 上的本地服务器

[英]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.

相关问题 问题将 pyodbc 连接到 WSL2 的 Windows 10 Pro 上的 MS SQL 数据库 - &gt; Ubuntu 20.04 - Issue connecting pyodbc to MS SQL database on Windows 10 Pro from WSL2 -> Ubuntu 20.04 Matplotlib WSL2 Pycharm - Matplotlib WSL2 Pycharm 如何在 wsl2 上从 python3 运行 Selenium ChromeDriver? - How to run Selenium ChromeDriver from python3 on wsl2? Flask服务器无法在Windows中打开到本地网络,host = 0.0.0.0无法正常工作 - Flask server not open to local network in windows, host=0.0.0.0 not working WSL2 错误:_tkinter.TclError:无法连接以显示“127.0.0.1:0.0” - WSL2 error: _tkinter.TclError: couldn't connect to display "127.0.0.1:0.0" 运行 Python 脚本时出现问题 - 无法在 windows 11 ubuntu wsl2 上运行脚本 - Problems Running Python Script - not being able to run script on windows 11 ubuntu wsl2 无法连接到Windows共享 - Unable to connect to windows shares 如何在 WSL2 上的 Ubuntu 中安装 cuDNN? - How to install cuDNN in Ubuntu on WSL2? 通过 pip 在 WSL2 上未正确安装 Cfgrib - Cfgrib is not installed correctly on WSL2 through pip 如何从 Linux 主机使用 Windows 身份验证对 SQL 服务器和 ZD233B99A3CCB7966A032466 - How to use Windows authentication for SQL Server from a Linux host with SQLAlchemy?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM