FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
python-zeroconf/src/zeroconf/asyncio.py at master · python-zeroconf/python-zeroconf · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
python-zeroconf
/
python-zeroconf
Public
Notifications
You must be signed in to change notification settings
Fork
230
Star
734
Code
Issues
46
Pull requests
8
Discussions
Actions
Projects
Wiki
Security and quality
5
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
python-zeroconf
/
src
/
zeroconf
/
asyncio.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
300 lines (249 loc) · 11.5 KB
Breadcrumbs
python-zeroconf
/
src
/
zeroconf
/
asyncio.py
Copy path
File metadata and controls
300 lines (249 loc) · 11.5 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
"""Multicast DNS Service Discovery for Python, v0.14-wmcbrine
Copyright 2003 Paul Scott-Murphy, 2014 William McBrine
This module provides a framework for the use of DNS Service Discovery
using IP multicast.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
USA
"""
from
__future__
import
annotations
import
asyncio
import
contextlib
from
collections
.
abc
import
Awaitable
,
Callable
from
types
import
TracebackType
# used in type hints
from
.
_core
import
Zeroconf
from
.
_dns
import
DNSQuestionType
from
.
_exceptions
import
NotRunningException
from
.
_services
import
ServiceListener
from
.
_services
.
browser
import
_ServiceBrowserBase
from
.
_services
.
info
import
AsyncServiceInfo
,
ServiceInfo
from
.
_services
.
types
import
ZeroconfServiceTypes
from
.
_utils
.
net
import
InterfaceChoice
,
InterfacesType
,
IPVersion
from
.
const
import
_BROWSER_TIME
,
_MDNS_PORT
,
_SERVICE_TYPE_ENUMERATION_NAME
__all__
=
[
"AsyncServiceBrowser"
,
"AsyncServiceInfo"
,
"AsyncZeroconf"
,
"AsyncZeroconfServiceTypes"
,
]
class
AsyncServiceBrowser
(
_ServiceBrowserBase
):
"""Used to browse for a service for specific type(s).
Constructor parameters are as follows:
* `zc`: A Zeroconf instance
* `type_`: fully qualified service type name
* `handler`: ServiceListener or Callable that knows how to process ServiceStateChange events
* `listener`: ServiceListener
* `addr`: address to send queries (will default to multicast)
* `port`: port to send queries (will default to mdns 5353)
* `delay`: The initial delay between answering questions
* `question_type`: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU)
The listener object will have its add_service() and
remove_service() methods called when this browser
discovers changes in the services availability.
"""
def
__init__
(
self
,
zeroconf
:
Zeroconf
,
type_
:
str
|
list
,
handlers
:
ServiceListener
|
list
[
Callable
[...,
None
]]
|
None
=
None
,
listener
:
ServiceListener
|
None
=
None
,
addr
:
str
|
None
=
None
,
port
:
int
=
_MDNS_PORT
,
delay
:
int
=
_BROWSER_TIME
,
question_type
:
DNSQuestionType
|
None
=
None
,
)
->
None
:
super
().
__init__
(
zeroconf
,
type_
,
handlers
,
listener
,
addr
,
port
,
delay
,
question_type
)
self
.
_async_start
()
async
def
async_cancel
(
self
)
->
None
:
"""Cancel the browser."""
self
.
_async_cancel
()
async
def
__aenter__
(
self
)
->
AsyncServiceBrowser
:
return
self
async
def
__aexit__
(
self
,
exc_type
:
type
[
BaseException
]
|
None
,
exc_val
:
BaseException
|
None
,
exc_tb
:
TracebackType
|
None
,
)
->
bool
|
None
:
await
self
.
async_cancel
()
return
None
class
AsyncZeroconfServiceTypes
(
ZeroconfServiceTypes
):
"""An async version of ZeroconfServiceTypes."""
@
classmethod
async
def
async_find
(
cls
,
aiozc
:
AsyncZeroconf
|
None
=
None
,
timeout
:
int
|
float
=
5
,
interfaces
:
InterfacesType
=
InterfaceChoice
.
All
,
ip_version
:
IPVersion
|
None
=
None
,
)
->
tuple
[
str
, ...]:
"""
Return all of the advertised services on any local networks.
:param aiozc: AsyncZeroconf() instance. Pass in if already have an
instance running or if non-default interfaces are needed
:param timeout: seconds to wait for any responses
:param interfaces: interfaces to listen on.
:param ip_version: IP protocol version to use.
:return: tuple of service type strings
"""
local_zc
=
aiozc
or
AsyncZeroconf
(
interfaces
=
interfaces
,
ip_version
=
ip_version
)
listener
=
cls
()
async_browser
=
AsyncServiceBrowser
(
local_zc
.
zeroconf
,
_SERVICE_TYPE_ENUMERATION_NAME
,
listener
=
listener
)
# wait for responses
await
asyncio
.
sleep
(
timeout
)
await
async_browser
.
async_cancel
()
# close down anything we opened
if
aiozc
is
None
:
await
local_zc
.
async_close
()
return
tuple
(
sorted
(
listener
.
found_services
))
class
AsyncZeroconf
:
"""Implementation of Zeroconf Multicast DNS Service Discovery
Supports registration, unregistration, queries and browsing.
The async version is currently a wrapper around Zeroconf which
is now also async. It is expected that an asyncio event loop
is already running before creating the AsyncZeroconf object.
"""
def
__init__
(
self
,
interfaces
:
InterfacesType
=
InterfaceChoice
.
All
,
unicast
:
bool
=
False
,
ip_version
:
IPVersion
|
None
=
None
,
apple_p2p
:
bool
=
False
,
zc
:
Zeroconf
|
None
=
None
,
)
->
None
:
"""Creates an instance of the Zeroconf class, establishing
multicast communications, and listening.
:param interfaces: :class:`InterfaceChoice` or a list of IP addresses
(IPv4 and IPv6) and interface indexes (IPv6 only).
IPv6 notes for non-POSIX systems:
* `InterfaceChoice.All` is an alias for `InterfaceChoice.Default`
on Python versions before 3.8.
Also listening on loopback (``::1``) doesn't work, use a real address.
:param ip_version: IP versions to support. If `choice` is a list, the default is detected
from it. Otherwise defaults to V4 only for backward compatibility.
:param apple_p2p: use AWDL interface (only macOS)
"""
self
.
zeroconf
=
zc
or
Zeroconf
(
interfaces
=
interfaces
,
unicast
=
unicast
,
ip_version
=
ip_version
,
apple_p2p
=
apple_p2p
,
)
self
.
async_browsers
:
dict
[
ServiceListener
,
AsyncServiceBrowser
]
=
{}
async
def
async_register_service
(
self
,
info
:
ServiceInfo
,
ttl
:
int
|
None
=
None
,
allow_name_change
:
bool
=
False
,
cooperating_responders
:
bool
=
False
,
strict
:
bool
=
True
,
)
->
Awaitable
:
"""Registers service information to the network with a default TTL.
Zeroconf will then respond to requests for information for that
service. The name of the service may be changed if needed to make
it unique on the network. Additionally multiple cooperating responders
can register the same service on the network for resilience
(if you want this behavior set `cooperating_responders` to `True`).
The service will be broadcast in a task. This task is returned
and therefore can be awaited if necessary.
"""
return
await
self
.
zeroconf
.
async_register_service
(
info
,
ttl
,
allow_name_change
,
cooperating_responders
,
strict
)
async
def
async_unregister_all_services
(
self
)
->
None
:
"""Unregister all registered services.
Unlike async_register_service and async_unregister_service, this
method does not return a future and is always expected to be
awaited since its only called at shutdown.
"""
await
self
.
zeroconf
.
async_unregister_all_services
()
async
def
async_unregister_service
(
self
,
info
:
ServiceInfo
)
->
Awaitable
:
"""Unregister a service.
The service will be broadcast in a task. This task is returned
and therefore can be awaited if necessary.
"""
return
await
self
.
zeroconf
.
async_unregister_service
(
info
)
async
def
async_update_service
(
self
,
info
:
ServiceInfo
)
->
Awaitable
:
"""Registers service information to the network with a default TTL.
Zeroconf will then respond to requests for information for that
service.
The service will be broadcast in a task. This task is returned
and therefore can be awaited if necessary.
"""
return
await
self
.
zeroconf
.
async_update_service
(
info
)
async
def
async_update_interfaces
(
self
,
interfaces
:
InterfacesType
|
None
=
None
,
ip_version
:
IPVersion
|
None
=
None
,
apple_p2p
:
bool
|
None
=
None
,
)
->
None
:
"""Rescan network interfaces and reconcile the sockets in use.
Adds sockets for interfaces that appeared, drops sockets for
interfaces that disappeared, and re-announces existing
registrations on the resulting senders. ``interfaces``,
``ip_version`` and ``apple_p2p`` each default to the construction-time
value. Raises RuntimeError if apple_p2p is set on a non-Apple platform.
"""
await
self
.
zeroconf
.
async_update_interfaces
(
interfaces
,
ip_version
,
apple_p2p
)
async
def
async_close
(
self
)
->
None
:
"""Ends the background threads, and prevent this instance from
servicing further queries."""
if
not
self
.
zeroconf
.
done
:
with
contextlib
.
suppress
(
NotRunningException
):
await
self
.
zeroconf
.
async_wait_for_start
(
timeout
=
1.0
)
await
self
.
async_remove_all_service_listeners
()
await
self
.
async_unregister_all_services
()
await
self
.
zeroconf
.
_async_close
()
# pylint: disable=protected-access
async
def
async_get_service_info
(
self
,
type_
:
str
,
name
:
str
,
timeout
:
int
=
3000
,
question_type
:
DNSQuestionType
|
None
=
None
,
)
->
AsyncServiceInfo
|
None
:
"""Returns network's service information for a particular
name and type, or None if no service matches by the timeout,
which defaults to 3 seconds.
:param type_: fully qualified service type name
:param name: the name of the service
:param timeout: milliseconds to wait for a response
:param question_type: The type of questions to ask (DNSQuestionType.QM or DNSQuestionType.QU)
"""
return
await
self
.
zeroconf
.
async_get_service_info
(
type_
,
name
,
timeout
,
question_type
)
async
def
async_add_service_listener
(
self
,
type_
:
str
,
listener
:
ServiceListener
)
->
None
:
"""Adds a listener for a particular service type. This object
will then have its add_service and remove_service methods called when
services of that type become available and unavailable."""
await
self
.
async_remove_service_listener
(
listener
)
self
.
async_browsers
[
listener
]
=
AsyncServiceBrowser
(
self
.
zeroconf
,
type_
,
listener
)
async
def
async_remove_service_listener
(
self
,
listener
:
ServiceListener
)
->
None
:
"""Removes a listener from the set that is currently listening."""
if
listener
in
self
.
async_browsers
:
await
self
.
async_browsers
[
listener
].
async_cancel
()
del
self
.
async_browsers
[
listener
]
async
def
async_remove_all_service_listeners
(
self
)
->
None
:
"""Removes a listener from the set that is currently listening."""
await
asyncio
.
gather
(
*
(
self
.
async_remove_service_listener
(
listener
)
for
listener
in
list
(
self
.
async_browsers
))
)
async
def
__aenter__
(
self
)
->
AsyncZeroconf
:
return
self
async
def
__aexit__
(
self
,
exc_type
:
type
[
BaseException
]
|
None
,
exc_val
:
BaseException
|
None
,
exc_tb
:
TracebackType
|
None
,
)
->
bool
|
None
:
await
self
.
async_close
()
return
None
Back
|
FazBrowse Home
|
New Git URL