【WSL2笔记5】开放端口与iP转发设置局域网访问WSL的Gradio和Streamlit应用服务
1、未设置前运行测试
1.1 查看网络信息
1.1.1 查看win10端口映射
win10以管理员身份打开PowerShell
按Win键->空格->powershell->以管理员身份运行

netsh interface portproxy show all
没有设置任何IP转发
1.1.2 查看win10局域网IP配置
ipconfig
PS C:\Windows\system32> ipconfig
Windows IP 配置
以太网适配器 以太网:
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : f000::4f05:b900:ca39:f000%4
IPv4 地址 . . . . . . . . . . . . : 192.168.2.112
子网掩码 . . . . . . . . . . . . : 255.255.255.0
默认网关. . . . . . . . . . . . . : 192.168.2.1
以太网适配器 以太网 2:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
以太网适配器 蓝牙网络连接:
媒体状态 . . . . . . . . . . . . : 媒体已断开连接
连接特定的 DNS 后缀 . . . . . . . :
以太网适配器 vEthernet (WSL):
连接特定的 DNS 后缀 . . . . . . . :
本地链接 IPv6 地址. . . . . . . . : f000::50c8:1008:a100:500a%31
IPv4 地址 . . . . . . . . . . . . : 172.19.32.1
子网掩码 . . . . . . . . . . . . : 255.255.240.0
默认网关. . . . . . . . . . . . . :
PS C:\Windows\system32>
Win10局域网IP: 192.168.2.112
在本机能访问这个地址成功,说明局域网的任一电脑都能访问这个IP转发的WSL2服务程序
1.1.3 查看WSL2虚拟网卡信息
ifconfig
(chat38) xf@VP01:~/ai$ ifconfig
eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 172.19.44.201 netmask 255.255.240.0 broadcast 172.19.47.255
inet6 fe80::215:5dff:fe77:6d17 prefixlen 64 scopeid 0x20<link>
ether 00:10:50:00:6d:17 txqueuelen 1000 (Ethernet)
RX packets 1613313 bytes 2185775228 (2.1 GB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 516136 bytes 109694288 (109.6 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
inet 127.0.0.1 netmask 255.0.0.0
inet6 ::1 prefixlen 128 scopeid 0x10<host>
loop txqueuelen 1000 (Local Loopback)
RX packets 19047 bytes 157843978 (157.8 MB)
RX errors 0 dropped 0 overruns 0 frame 0
TX packets 19047 bytes 157843978 (157.8 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
1.2 运行Streamlit应用
streamlit run demo_st.py
import streamlit as st
st.write("""
# Streamlit
*Web App* **Web应用程序框架**
""")
if st.button("测试"):
st.warning("你成功点到我了!")
1.2.2 wsl2中运行
streamlit run demo_st.py
(chat38) xf@VP01:~/ai$ streamlit run demo_st.py
You can now view your Streamlit app in your browser.
Local URL: http://localhost:8501
Network URL: http://172.19.44.201:8501
Streamlit 自动运行在Ubuntu虚拟网卡地址172.19.44.201:8501
Gradio需要手动指定运行在虚拟网卡地址
1.2.3 浏览器查看
-
win10 localhost地址连接成功

-
win10 局域网IP本地连接失败
- Ubuntu 虚拟IP本地连接成功
win10能成功访问wsl虚拟IP,只要把win10局域网IP的请求转发给wsl虚拟IP
1.3 运行Gradio应用
1.3.1 创建demo
import gradio as gr
gr.HTML("""<h1 align="center">Gradio Web</h1>""")
def greet(name):
return "Hello " + name + "!"
demo = gr.Interface(fn=greet, inputs="text", outputs="text")
webapp.queue().launch(server_name="172.19.44.201", share=False, inbrowser=True)
1.3.2 wsl2中运行
python demo_gr.py
(chat38) xf@VP01:~/ai$ python demo_gr.py
Running on local URL: http://127.0.0.1:7860
To create a public link, set `share=True` in `launch()`.
/usr/bin/xdg-open: 882: x-www-browser: not found
/usr/bin/xdg-open: 882: firefox: not found
/usr/bin/xdg-open: 882: iceweasel: not found
/usr/bin/xdg-open: 882: seamonkey: not found
/usr/bin/xdg-open: 882: mozilla: not found
/usr/bin/xdg-open: 882: epiphany: not found
/usr/bin/xdg-open: 882: konqueror: not found
/usr/bin/xdg-open: 882: chromium: not found
/usr/bin/xdg-open: 882: chromium-browser: not found
/usr/bin/xdg-open: 882: google-chrome: not found
1.3.3 浏览器查看
- win10 localhost地址连接成功

- win10 局域网IP本地连接失败

- Ubuntu 虚拟IP本地连接失败
Gradio 需要指定运行在虚拟网卡IP上才可以访问
Streamlit 是因为自动运行虚拟网卡IP上

2、IP/端口重定向转发
2.1 PowerShell Netsh命令
netsh interface portproxy add v4tov4 listenaddress=* listenport=8501 connectaddress=172.19.44.201 connectport=8501 protocol=tcp
netsh interface portproxy add v4tov4 listenaddress=* listenport=7860 connectaddress=172.19.44.201 connectport=7860 protocol=tcp
PS C:\Windows\system32> netsh interface portproxy show all
侦听 ipv4: 连接到 ipv4:
地址 端口 地址 端口
--------------- ---------- --------------- ----------
* 8501 172.19.44.201 8501
* 7860 172.19.44.201 7860
2.2 查看Streamlit应用访问情况
- win10 局域网IP本地连接

2.3 查看Gradio应用访问情况
2.3.1 指定IP运行
- Gradio 应用需要运行在虚拟IP地址
demo.queue().launch(server_name="172.19.44.201", share=False, inbrowser=True)
-
Ubuntu 虚拟IP本地连接

-
win10 局域网IP本地连接

3、防火墙开放端口
本机访问192.168.2.112:8501成功,局域网其他电脑访问8501和7860端口会被防火墙阻拦,需要开放8501和7860两个端口给局域网。


本章完

7834

被折叠的 条评论
为什么被折叠?



