我有一个Python项目使用waitress
在 WSL2/Ubuntu 20 上的本地主机上提供服务。我从 VSCode 远程启动服务器,但无法使用地址从 Windows 上的浏览器连接到它http://127.0.0.1:5998 http://127.0.0.1:5998或 http://localhost:5998,出现无法连接错误。我找不到它有什么问题并感谢任何帮助。
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)
我看到它在 WSL 上监听:
> sudo lsof -i -P -n | grep LISTEN
python3 1263 xxx 8u IPv4 39138 0t0 TCP *:5998 (LISTEN)
我还尝试使用 127.0.0.1 作为serve() ip而不是0.0.0.0,但没有帮助。
我在 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
我已经检查了 Windows 上使用的端口netstat -o
似乎没有任何东西在使用端口 5998。
我已经通过在 Windows 上添加端口转发解决了这个问题。
在 WSL 上运行:
ifconfig
inet IP 上eth0
条目是您的 WSL IP。
在 Windows 上运行此命令:
netsh interface portproxy add v4tov4 listenport=<port> listenaddress=0.0.0.0 connectport=<port> connectaddress=<your WSL IP>
之后我可以使用连接localhost:<port>
or 127.0.0.1:<port>
or <WSL IP>:<port>
从 Windows 上的浏览器。
您可以使用列出当前端口代理netsh interface portproxy show all
命令。
您可能需要添加防火墙规则 https://www.nextofwindows.com/allow-server-running-inside-wsl-to-be-accessible-outside-windows-10-host用于 Windows 上的端口。
更多信息:https://github.com/microsoft/WSL/issues/4150#issuecomment-927091364 https://github.com/microsoft/WSL/issues/4150#issuecomment-927091364
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)