Python:连接到wifi中继器(WiFi NAT路由器)上连接的设备的IP
Python: connecting to the IP of a device connected on a wifi repeater (WiFi NAT Router)
我有一个有效的 python 脚本,它通过连接到其 IP 地址从 wifi 设备(NodeMCU*)检索数据:
import requests
response = requests.get('http://192.168.1.12/data.json')
data = response.json()
print(data)
此脚本之所以有效,是因为我的计算机和设备都连接到同一个 wifi SSID(我们称之为主 SSID)。
但是我的 wifi 信号对我的设备来说不够强(因为它在室外),所以我设置了一个 wifi 中继器**,它连接到主 SSID 并创建了一个接入点。然后我将我的设备连接到这个 wifi 中继器接入点。
我在上面的脚本中使用了我设备的新 IP 地址 (10.24.1.2
*** ),但这仅在我断开计算机与主 SSID 的连接并将其连接到 wifi 中继器接入点时有效.但我不想这样做:我的电脑应该保留在主 SSID 上,我的设备应该在 wifi 中继器接入点上。
所以我的问题是:如何从连接到 wifi 中继器的设备检索 data.json,而我的计算机仍使用主 SSID?
我查看了 wifi 中继器可以执行的端口转发,但我的设备似乎没有端口:如果我连接到 http://10.24.1.2/data.json
,我会看到一堆数据,但如果我连接到 http://10.24.1.2/data.json:3
(或任何数字)我得到 "Not found".
但我可以在我的设备上使用特定的服务器、路径和端口设置 API(尽管需要做一些研究才能知道如何使用它)
*) 该设备是 lufdaten sensor,由 nodemcu 构建
) 我的 wifi 中继器是用这个 https://github.com/martin-ger/esp_wifi_repeater
编程的另一个 nodemcu
*) 我使用 PuTTY
通过 TCP/telnet 连接到我的 wifi 中继器 IP 地址来检索我设备的新 IP
您的 "repeater" 不像大多数 WiFi 中继器那样扩展现有网络,而是一个 WiFi 路由器,它创建一个具有不同地址的新网络 space。您的主 WiFi 路由器 ("main SSID") 只是这个新网络中的一个设备,这意味着它将使用 NAT 提供从 "main SSID" 中的任何设备到 "repeater SSID" 的访问。
但是由于这种 NAT 将多个 IP 动态映射到单个 IP,因此它只能在一个方向上工作:从 "main network" (192.168.1/24) 内部连接到 "repeater network" (10.24.1/24) 可以创建,但没有从中继器网络连接到主网络。这实际上与无法直接从互联网访问家庭网络内的设备的原因相同。
如果您的 "main SSID" 路由器可以做到这一点,您需要创建一个明确的端口转发才能使这项工作正常进行。或者,您可以使用不创建新网络而只是扩展现有网络的中继器。不知道您现有的设备是否可以执行此操作,因为它被明确记录为 "a WiFi NAT router".
只是为了完成 Steffen Ullrich 的回答:
从连接到主 SSID/network 的计算机检索 http://10.24.1.2/data.json
上 wifi 中继器网络上可用的 json 的步骤:
1) 在 wifi 中继器中创建一个端口转发(我使用的 wifi 中继器可以通过 TCP/telnet 进行控制,因此如果我的计算机连接到主 SSID 或通过 arduino 串口,则通过 putty 发送此命令监控我的电脑是否连接到 wifi 中继器)
portmap add TCP 999 10.24.1.2 80
999 : 随机选择的数字
10.24.1.2 : 连接到 wifi 中继器网络的设备的 IP 地址(通过命令 "show dhcp" 检索)
80 : HTTP 请求的默认端口
2) 连接到主 SSID 后,转到 http://192.168.1.39:999/data.json
(根据您在步骤 1 中选择的号码更改 999)
如果您想从任何互联网连接(从任何地方)访问您的设备,您还需要在主 SSID/network 上创建一个端口转发(我必须先转到 192.168.1.1,然后是 v4 网络 > NAT):
WifiRepeater TCP Port 8432 192.168.1.39 999
wifiRepeater : 端口转发的自定义名称
TCP:这是使用的协议
端口:是使用的类型
8432:一个随机数,参见howtogeek.com的解释:
You can use any number you want here between 1 and 65353, but
practically most of the lower numbers are taken up by standard
services (like email and web servers) and many of the higher numbers
are assigned to fairly common applications. With that in mind, we’d
recommend picking a number above 5,000 and, to be extra safe, using
Ctrl+F to search this long list of TCP/UDP port numbers to make sure
you’re not selecting a port that conflicts with an existing service
you’re already using.
192.168.1.39 : livebox wifi中继IP地址
999 : 第1步随机选择的数字
我有一个有效的 python 脚本,它通过连接到其 IP 地址从 wifi 设备(NodeMCU*)检索数据:
import requests
response = requests.get('http://192.168.1.12/data.json')
data = response.json()
print(data)
此脚本之所以有效,是因为我的计算机和设备都连接到同一个 wifi SSID(我们称之为主 SSID)。
但是我的 wifi 信号对我的设备来说不够强(因为它在室外),所以我设置了一个 wifi 中继器**,它连接到主 SSID 并创建了一个接入点。然后我将我的设备连接到这个 wifi 中继器接入点。
我在上面的脚本中使用了我设备的新 IP 地址 (10.24.1.2
*** ),但这仅在我断开计算机与主 SSID 的连接并将其连接到 wifi 中继器接入点时有效.但我不想这样做:我的电脑应该保留在主 SSID 上,我的设备应该在 wifi 中继器接入点上。
所以我的问题是:如何从连接到 wifi 中继器的设备检索 data.json,而我的计算机仍使用主 SSID?
我查看了 wifi 中继器可以执行的端口转发,但我的设备似乎没有端口:如果我连接到 http://10.24.1.2/data.json
,我会看到一堆数据,但如果我连接到 http://10.24.1.2/data.json:3
(或任何数字)我得到 "Not found".
但我可以在我的设备上使用特定的服务器、路径和端口设置 API(尽管需要做一些研究才能知道如何使用它)
*) 该设备是 lufdaten sensor,由 nodemcu 构建
) 我的 wifi 中继器是用这个 https://github.com/martin-ger/esp_wifi_repeater
编程的另一个 nodemcu
*) 我使用 PuTTY
您的 "repeater" 不像大多数 WiFi 中继器那样扩展现有网络,而是一个 WiFi 路由器,它创建一个具有不同地址的新网络 space。您的主 WiFi 路由器 ("main SSID") 只是这个新网络中的一个设备,这意味着它将使用 NAT 提供从 "main SSID" 中的任何设备到 "repeater SSID" 的访问。
但是由于这种 NAT 将多个 IP 动态映射到单个 IP,因此它只能在一个方向上工作:从 "main network" (192.168.1/24) 内部连接到 "repeater network" (10.24.1/24) 可以创建,但没有从中继器网络连接到主网络。这实际上与无法直接从互联网访问家庭网络内的设备的原因相同。
如果您的 "main SSID" 路由器可以做到这一点,您需要创建一个明确的端口转发才能使这项工作正常进行。或者,您可以使用不创建新网络而只是扩展现有网络的中继器。不知道您现有的设备是否可以执行此操作,因为它被明确记录为 "a WiFi NAT router".
只是为了完成 Steffen Ullrich 的回答:
从连接到主 SSID/network 的计算机检索 http://10.24.1.2/data.json
上 wifi 中继器网络上可用的 json 的步骤:
1) 在 wifi 中继器中创建一个端口转发(我使用的 wifi 中继器可以通过 TCP/telnet 进行控制,因此如果我的计算机连接到主 SSID 或通过 arduino 串口,则通过 putty 发送此命令监控我的电脑是否连接到 wifi 中继器)
portmap add TCP 999 10.24.1.2 80
999 : 随机选择的数字 10.24.1.2 : 连接到 wifi 中继器网络的设备的 IP 地址(通过命令 "show dhcp" 检索) 80 : HTTP 请求的默认端口
2) 连接到主 SSID 后,转到 http://192.168.1.39:999/data.json
(根据您在步骤 1 中选择的号码更改 999)
如果您想从任何互联网连接(从任何地方)访问您的设备,您还需要在主 SSID/network 上创建一个端口转发(我必须先转到 192.168.1.1,然后是 v4 网络 > NAT):
WifiRepeater TCP Port 8432 192.168.1.39 999
wifiRepeater : 端口转发的自定义名称
TCP:这是使用的协议
端口:是使用的类型
8432:一个随机数,参见howtogeek.com的解释:
You can use any number you want here between 1 and 65353, but practically most of the lower numbers are taken up by standard services (like email and web servers) and many of the higher numbers are assigned to fairly common applications. With that in mind, we’d recommend picking a number above 5,000 and, to be extra safe, using Ctrl+F to search this long list of TCP/UDP port numbers to make sure you’re not selecting a port that conflicts with an existing service you’re already using.
192.168.1.39 : livebox wifi中继IP地址
999 : 第1步随机选择的数字