-
Notifications
You must be signed in to change notification settings - Fork 346
mdns
Multicast DNS (mDNS) is the protocol that creates a device-uniqueidentifier to register as a hostname via a multicast service on local networks.
Usage:
import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("WiFi_SSID", "WiFi_password")
tmo = 50
while not sta_if.isconnected():
utime.sleep_ms(100)
tmo -= 1
if tmo == 0:
break
if sta_if.isconnected():
mdns = network.mDNS()
_ = mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"})
_ = mdns.addService('_telnet', '_tcp', 23, "MicroPython", {"board": "ESP32", "service": "mPy Telnet REPL"})
_ = mdns.addService('_http', '_tcp', 80, "MicroPython", {"board": "ESP32", "service": "mPy Web server"))name string, server host name
instance string, mDNS instance description
Returns True on success, False on fail.
After the mdns is started, the MicroPython host will be visible in local network as name.local
stop the mdns server, free the resources
Returns True on success, False on fail.
Add service to mdns server.
service string, service type, use names like '_http', '_ftp', _telnet', '_mytcp'
protocol string, protocol type, _tcp or _udp
port integer, the port on which the service runs
instance string, service instance name
txdata optional, dictionary (max 8 items), describe the service characteristics
Returns True on success, False on fail.
>>> mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"})
TrueRemove the previously added service from mdns server.
service string, service type
protocol string, protocol type
Returns True on success, False on fail.
Query the IP address of the LAN host hostname.
hostname string, host name to query.
timeout optional; default=2000; timeout for host query in ms.
Returns IPv4 addresse as strings.
>>> mdns.queryHost('ubuntumate')
'192.168.0.63'Query the service info from hosts on LAN.
service string, service type
protocol string, protocol type
timeout optional; default=2000; timeout for host query in ms.
maxres optional; default=8; maximal number of results to return.
Returns list of items containing services information.
Each service information item is a tuple with the following items:
-
interface_type'STA', 'AP' or 'ETH' -
protocol_type'V4' or 'V6' -
instance_namestring orNone -
host_namestring orNone -
portinteger orNone -
ip_addrlist of IP addresses orNone -
tx_recorddictionary of TX record items orNone
>>> mdns.queryService('_smb', '_tcp')
[('STA', 'V4', 'UBUNTUMATE', 'UbuntuMate.local', 445, ['192.168.0.63', '254.128.0.0'], None)]
>>>