-
Notifications
You must be signed in to change notification settings - Fork 344
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():
try:
mdns = network.mDNS()
mdns.start("mPy","MicroPython with 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"})
except:
print("mDNS not started")
boris@UbuntuMate:~$ avahi-browse -avtr
Server version: avahi 0.6.32; Host name: UbuntuMate.local
E Ifce Prot Name Type Domain
....
+ eth0 IPv4 MicroPython Web Site local
+ eth0 IPv4 MicroPython Telnet Remote Terminal local
+ eth0 IPv4 MicroPython FTP File Transfer local
= eth0 IPv4 MicroPython Web Site local
hostname = [mPy.local]
address = [192.168.0.16]
port = [80]
txt = ["service=mPy Web server" "board=ESP32"]
= eth0 IPv4 MicroPython Telnet Remote Terminal local
hostname = [mPy.local]
address = [192.168.0.16]
port = [23]
txt = ["service=mPy Telnet REPL" "board=ESP32"]
= eth0 IPv4 MicroPython FTP File Transfer local
hostname = [mPy.local]
address = [192.168.0.16]
port = [21]
txt = ["board=ESP32" "passive=True" "service=mPy FTP File transfer"]
: Cache exhausted
: All for now
boris@UbuntuMate:~$
name
string, server host name
instance
string, mDNS instance description
Raises an exception if not succesfully started.
After the mdns is started, the MicroPython host will be visible in local network as name.local
stop the mdns server, free the resources
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.
Raises an exception if mDNS server not started or wrong parameters are given.
>>> mdns.addService('_ftp', '_tcp', 21, "MicroPython", {"board": "ESP32", "service": "mPy FTP File transfer", "passive": "True"})
True
Remove the previously added service from mdns server.
service
string, service type
protocol
string, protocol type
Returns True
on success, False
on fail.
Raises an exception if mDNS server not started or wrong parameters are given.
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_name
string orNone
-
host_name
string orNone
-
port
integer orNone
-
ip_addr
list of IP addresses orNone
-
tx_record
dictionary of TX record items orNone
>>> mdns.queryService('_smb', '_tcp')
[('STA', 'V4', 'UBUNTUMATE', 'UbuntuMate.local', 445, ['192.168.0.63', '254.128.0.0'], None)]
>>>