更改本地网络上网页的 url

发布于 2025-01-10 02:09:45 字数 976 浏览 1 评论 0原文

我在 ESP32 芯片上构建了一个网页,负责创建一个接入点,允许我的计算机连接以访问此页面。
目前我只能通过在浏览器中输入 ESP 的 IP 来访问它,但这可能非常麻烦。
我想知道是否可以使用文字而不是 ESP 的 IP 来更改页面的 url。
也许我遗漏了一些技术术语,但我在互联网上没有找到任何解决方案。

PS:我使用 micropython 和套接字来从板上提供 html 文件:

def handleClient(client_socket):
    headers, data = loadRequest(client_socket.recv(1024).decode('utf-8'))
    # print('[*] Received:\n%s\n%s\n' % (headers, data))
    if headers['method'] == 'GET' and '/connect' == headers['route']:#'/connect' in headers['route']:
        ssid, password, status, code = connect(headers)
        client_socket.sendall(RESPONSE_TEMPLATE % (code, status, {'ssid': ssid, 'password': password}, code))
        return ssid, password
    elif headers['method'] == 'GET' and headers['route'] == '/':
        renderWebPage(client_socket)
    client_socket.close()
    return None, None

I built a webpage on an ESP32 chip, charged to create an access point allowing my computer to connect in order to access this page.
For the moment I can only access it using the IP of my ESP by typing it in a browser but it can be very bothersome.
I'd like to know if it was possible to change the url of the page using words instead of the ESP's IP.
Maybe I'm missing some technical terms but I didn't find any solution on the internet.

PS: I'm using micropython with sockets to serve html files from the board:

def handleClient(client_socket):
    headers, data = loadRequest(client_socket.recv(1024).decode('utf-8'))
    # print('[*] Received:\n%s\n%s\n' % (headers, data))
    if headers['method'] == 'GET' and '/connect' == headers['route']:#'/connect' in headers['route']:
        ssid, password, status, code = connect(headers)
        client_socket.sendall(RESPONSE_TEMPLATE % (code, status, {'ssid': ssid, 'password': password}, code))
        return ssid, password
    elif headers['method'] == 'GET' and headers['route'] == '/':
        renderWebPage(client_socket)
    client_socket.close()
    return None, None

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

享受孤独 2025-01-17 02:09:45

解决您的问题需要两个部分:

  1. 发布名称(使用 mdns)
  2. 从客户端解析该名称

MicroPython 自 v1.12 起内置了对 mdns 的支持。
基本代码是使用以下代码分配主机名:

wlan.config(dhcp_hostname="prettyname")

请注意,您的客户端还需要有 mdns 支持才能解析该地址。这可能/将取决于您的客户。

完整的示例如下:

import network
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
    wlan.active(True)
    mac = wlan.config('mac')
    host = "prettyname"
    wlan.config(dhcp_hostname = host)
    wlan.connect('myssid', 'mypassword')
    while not wlan.isconnected():
        pass
        
host = wlan.config('dhcp_hostname')
print('Wifi connected as {}/{}, net={}, gw={}, dns={}'.format(
    host, *wlan.ifconfig()))

来源:MicroPython 论坛

there are two parts needed to solve your Q:

  1. publish a name (using mdns)
  2. resolve that name from a client

MicroPython has built-in support for mdns since v1.12.
The essential code is to assign a hostname using the below:

wlan.config(dhcp_hostname="prettyname")

Note that your client also needs to have mdns support in order to be able to resolve that address. That may/will depend on your client.

a complete sample would be:

import network
wlan = network.WLAN(network.STA_IF)
if not wlan.isconnected():
    wlan.active(True)
    mac = wlan.config('mac')
    host = "prettyname"
    wlan.config(dhcp_hostname = host)
    wlan.connect('myssid', 'mypassword')
    while not wlan.isconnected():
        pass
        
host = wlan.config('dhcp_hostname')
print('Wifi connected as {}/{}, net={}, gw={}, dns={}'.format(
    host, *wlan.ifconfig()))

Source: MicroPython Forum

沉溺在你眼里的海 2025-01-17 02:09:45

当然。最简单的选项是启用 mDNS 。这允许同一本地网络中的主机将设备名称(例如espressif.local)解析为其IP。仅适用于本地网络,并且需要在计算机上安装 mDNS 客户端(Mac、Linux 和 Windows 现在都倾向于内置它)。

但不知道如何在 Micropython 中做到这一点。尝试一下谷歌。

Certainly. The easiest option is enabling mDNS. This allows hosts in the same local network to resolve the device's name (e.g. espressif.local) into its IP. Only works in local network and requires an mDNS client on the computer (Mac, Linux and Windows all tend to have it built in these days).

No idea how to do it in Micropython, though. Give Google a try.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文