|
| 1 | +--- |
| 2 | +title: "MDNS" |
| 3 | +aliases: |
| 4 | + - firmwareapi/pycom/network/mdns.html |
| 5 | + - firmwareapi/pycom/network/mdns.md |
| 6 | + - chapter/firmwareapi/pycom/network/mdns |
| 7 | +--- |
| 8 | +This class provides an interface to use the MDNS functionality. |
| 9 | + |
| 10 | +## Quick Usage Example |
| 11 | + |
| 12 | +Example for advertising own services: |
| 13 | + |
| 14 | +```python |
| 15 | +import time |
| 16 | +from network import MDNS |
| 17 | +# As a first step connection to network should be estbalished, e.g. via WLAN |
| 18 | + |
| 19 | +# Initialize the MDNS module |
| 20 | +MDNS.init() |
| 21 | +# Set the hostname and instance name of this device |
| 22 | +MDNS.set_name(hostname = "my_hostname", instance_name = "my_instance") |
| 23 | +# Add a TCP service to advertise |
| 24 | +MDNS.add_service("_http", MDNS.PROTO_TCP, 80) |
| 25 | +# Add an UDP service to advertise |
| 26 | +MDNS.add_service("_myservice", MDNS.PROTO_UDP, 1234, txt= (("board","esp32"),("u","user"),("p","password"))) |
| 27 | + |
| 28 | +# Give the other devices time to discover the services offered |
| 29 | +time.sleep(60) |
| 30 | + |
| 31 | +# Remove a service, it will no longer be advertised |
| 32 | +MDNS.remove_service("_http", MDNS.PROTO_TCP) |
| 33 | + |
| 34 | +``` |
| 35 | + |
| 36 | +Example for querying: |
| 37 | + |
| 38 | +```python |
| 39 | +import time |
| 40 | +from network import MDNS |
| 41 | +# As a first step connection to network should be estbalished, e.g. via WLAN |
| 42 | + |
| 43 | +# Initialize the MDNS module |
| 44 | +MDNS.init() |
| 45 | + |
| 46 | +# Perform a query for 2000 ms |
| 47 | +q = MDNS.query(2000, "_http", MDNS.PROTO_TCP) |
| 48 | + |
| 49 | +# Print out the query's result |
| 50 | +if q is not None: |
| 51 | + for elem in q: |
| 52 | + print(elem.instance_name()) |
| 53 | + print(elem.hostname()) |
| 54 | + print(elem.addr()) |
| 55 | + print(elem.port()) |
| 56 | + print(elem.txt()) |
| 57 | + |
| 58 | +``` |
| 59 | + |
| 60 | +## Constructor |
| 61 | + |
| 62 | +### class network.MDNS() |
| 63 | + |
| 64 | +Initializes the MDNS module. |
| 65 | + |
| 66 | +## Methods |
| 67 | + |
| 68 | +#### MDNS.deinit() |
| 69 | + |
| 70 | +Deinitializes the MDNS module and removes all registered services. |
| 71 | + |
| 72 | +#### MDNS.set_name(\*, hostname=None, instance_name=None) |
| 73 | + |
| 74 | +Sets the hostname and instance name of the device that is going to be advertised. |
| 75 | + |
| 76 | +The arguments are: |
| 77 | + |
| 78 | +* `hostname` is the hostname to set |
| 79 | +* `instance_name` is the instance name to set |
| 80 | + |
| 81 | +#### MDNS.add_service(service_type, proto, port, \*, txt) |
| 82 | + |
| 83 | +Adds a service to the MDNS module which will be advertised. |
| 84 | + |
| 85 | +The arguments are: |
| 86 | + |
| 87 | +* `service_type` is the type of the offered service, e.g.: _http, _ftp or can be custom service |
| 88 | +* `proto` is the Layer 4 protocol (TCP or UDP), can be `MDNS.PROTO_TCP` or `MDNS.PROTO_UDP` |
| 89 | +* `port` is the port number |
| 90 | +* `txt` is the TXT entries to be placed into the advertisement. The TXT entry is a (key, value) tuple and several TXT entries can be included in an advertisement. In that case, 'txt' must be given as a list of tuples. |
| 91 | + |
| 92 | +#### MDNS.remove_service(service_type, proto) |
| 93 | + |
| 94 | +Removes a service from the MDNS module so it will not be advertised anymore. |
| 95 | + |
| 96 | +The arguments are: |
| 97 | + |
| 98 | +* `service_type` is the type of the service, e.g.: _http, _ftp or can be custom service |
| 99 | +* `proto` is the Layer 4 protocol (TCP or UDP), can be `MDNS.PROTO_TCP` or `MDNS.PROTO_UDP` |
| 100 | + |
| 101 | +#### MDNS.query(timeout, service_type, proto, \*, instance_name=None) |
| 102 | + |
| 103 | +Performs a query with the given parameters. |
| 104 | + |
| 105 | +The arguments are: |
| 106 | + |
| 107 | +* `timeout` is the timeout value in milliseconds to wait to receive the results |
| 108 | +* `service_type` is the type of the service to be queried, e.g.: _http, _ftp or can be custom service |
| 109 | +* `proto` is the Layer 4 protocol (TCP or UDP), can be `MDNS.PROTO_TCP` or `MDNS.PROTO_UDP` |
| 110 | +* `instance_name` is the instance name of the service to be queried |
| 111 | + |
| 112 | +If the service is found then the function returns with a list of `MDNS_Query` objects. |
| 113 | + |
| 114 | +## MDNS_Query class |
| 115 | + |
| 116 | +The `MDNS_Query` aggregates all of the properties of a successful query session entry: |
| 117 | +* `hostname` is the hostname of the host advertising the service |
| 118 | +* `instance_name` is the instance_name of the service |
| 119 | +* `addr` is the IPv4 address belonging to the service |
| 120 | +* `port` is the port number belonging to the service |
| 121 | +* `txt` is the TXT entries from the advertisement. The TXT entry is a (key, value) tuple, and several TXT entries can be included in an advertisement. |
| 122 | + |
| 123 | +#### MDNS_Query.hostname() |
| 124 | + |
| 125 | +Returns with the hostname of the queried service. |
| 126 | + |
| 127 | +#### MDNS_Query.instance_name() |
| 128 | + |
| 129 | +Returns with the instance name of the queried service. |
| 130 | + |
| 131 | +#### MDNS_Query.addr() |
| 132 | + |
| 133 | +Returns with the IPv4 address of the queried service. |
| 134 | + |
| 135 | +#### MDNS_Query.port() |
| 136 | + |
| 137 | +Returns with the port of the queried service. |
| 138 | + |
| 139 | +#### MDNS_Query.txt() |
| 140 | + |
| 141 | +Returns with the TXT entries of the queried service. |
| 142 | + |
| 143 | +## Constants |
| 144 | + |
| 145 | +* TCP and UDP protocol types: `MDNS.PROTO_TCP`, `MDNS.PROTO_UDP` |
| 146 | + |
| 147 | + |
0 commit comments