Home
About
Blog
Products
Forum
Support
Contact
Sunbelt Computer Software
PL/B Language Development and Support
Home
About
Blog
Products
Forum
Support
Contact
python-zeroconf/examples/async_interface_monitor.py at master · python-zeroconf/python-zeroconf · GitHub
Skip to content
Navigation Menu
Sign in
Appearance settings
Platform
AI CODE CREATION
GitHub Copilot
Write better code with AI
GitHub Copilot app
Direct agents from issue to merge
MCP Registry
Integrate external tools
DEVELOPER WORKFLOWS
Actions
Automate any workflow
Codespaces
Instant dev environments
Issues
Plan and track work
Code Review
Manage code changes
Code Quality
Enforce quality at merge
APPLICATION SECURITY
GitHub Advanced Security
Find and fix vulnerabilities
Code security
Secure your code as you build
Secret protection
Stop leaks before they start
EXPLORE
Why GitHub
Documentation
Blog
Changelog
Marketplace
View all features
Solutions
BY COMPANY SIZE
Enterprises
Small and medium teams
Startups
Nonprofits
BY USE CASE
App Modernization
DevSecOps
DevOps
CI/CD
View all use cases
BY INDUSTRY
Healthcare
Financial services
Manufacturing
Government
View all industries
View all solutions
Resources
EXPLORE BY TOPIC
AI
Software Development
DevOps
Security
View all topics
EXPLORE BY TYPE
Customer stories
Events & webinars
Ebooks & reports
Business insights
GitHub Skills
SUPPORT & SERVICES
Documentation
Customer support
Community forum
Trust center
Partners
View all resources
Open Source
COMMUNITY
GitHub Sponsors
Fund open source developers
PROGRAMS
Security Lab
Maintainer Community
GitHub Stars
Archive Program
REPOSITORIES
Topics
Trending
Collections
Enterprise
ENTERPRISE SOLUTIONS
Enterprise platform
AI-powered developer platform
AVAILABLE ADD-ONS
GitHub Advanced Security
Enterprise-grade security features
Copilot for Business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
python-zeroconf
python-zeroconf
Repository navigation
Code
Issues
48
(48)
Pull requests
12
(12)
Discussions
Actions
Projects
Wiki
Security and quality
5
(5)
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
python-zeroconf
/
examples
/
async_interface_monitor.py
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
executable file
·
104 lines (83 loc) · 3.88 KB
master
Breadcrumbs
python-zeroconf
/
examples
/
async_interface_monitor.py
Copy path
Top
File metadata and controls
Code
Blame
executable file
·
104 lines (83 loc) · 3.88 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
#!/usr/bin/env python
"""Re-announce services when the host's network interfaces change.
``AsyncZeroconf.async_update_interfaces()`` reconciles the sockets in use to the
live interface set: it binds responders for interfaces that appeared, tears down
the ones that went away, and re-announces existing registrations on the new
senders. A call where nothing changed is a no-op.
zeroconf does not poll for interface changes itself; detection is
platform-specific and is best driven from whatever signal a host already has (a
netlink subscription on Linux, a framework's adapter-change event, etc.). When
no such signal is available, a small periodic poller like the one below is
enough: snapshot the addresses and reconcile only when they change. The piece
worth copying carefully is the lifecycle, cancel the monitor task before
closing the AsyncZeroconf so it does not outlive the instance.
"""
from
__future__
import
annotations
import
argparse
import
asyncio
import
contextlib
import
logging
import
ifaddr
from
zeroconf
.
asyncio
import
AsyncServiceInfo
,
AsyncZeroconf
_LOGGER
=
logging
.
getLogger
(
"interface_monitor"
)
def
address_snapshot
()
->
set
[
tuple
[
str
,
int
]]:
"""A snapshot of the host's current addresses; a change triggers a reconcile."""
return
{(
str
(
ip
.
ip
),
ip
.
network_prefix
)
for
adapter
in
ifaddr
.
get_adapters
()
for
ip
in
adapter
.
ips
}
async
def
monitor_interfaces
(
aiozc
:
AsyncZeroconf
,
interval
:
float
)
->
None
:
"""Reconcile sockets whenever the host's addresses change, until cancelled."""
previous
=
address_snapshot
()
while
True
:
await
asyncio
.
sleep
(
interval
)
current
=
address_snapshot
()
if
current
==
previous
:
continue
try
:
print
(
"Interfaces changed, reconciling sockets..."
)
await
aiozc
.
async_update_interfaces
()
except
Exception
:
# Log and retry on the next tick rather than letting the monitor
# die; leave ``previous`` unchanged so the change is re-attempted.
_LOGGER
.
exception
(
"Interface reconcile failed; will retry"
)
else
:
previous
=
current
class
AsyncRunner
:
def
__init__
(
self
)
->
None
:
self
.
aiozc
:
AsyncZeroconf
|
None
=
None
self
.
monitor
:
asyncio
.
Task
|
None
=
None
async
def
run
(
self
,
info
:
AsyncServiceInfo
,
interval
:
float
)
->
None
:
self
.
aiozc
=
AsyncZeroconf
()
await
self
.
aiozc
.
async_register_service
(
info
)
self
.
monitor
=
asyncio
.
create_task
(
monitor_interfaces
(
self
.
aiozc
,
interval
))
print
(
"Registered; monitoring interfaces. Press Ctrl-C to exit..."
)
await
asyncio
.
Event
().
wait
()
async
def
close
(
self
,
info
:
AsyncServiceInfo
)
->
None
:
assert
self
.
aiozc
is
not
None
# Stop the monitor before closing so it can't reconcile a closed instance.
if
self
.
monitor
is
not
None
:
self
.
monitor
.
cancel
()
with
contextlib
.
suppress
(
asyncio
.
CancelledError
):
await
self
.
monitor
# Await the goodbye broadcast so the TTL-0 records are actually sent.
await
(
await
self
.
aiozc
.
async_unregister_service
(
info
))
await
self
.
aiozc
.
async_close
()
if
__name__
==
"__main__"
:
logging
.
basicConfig
(
level
=
logging
.
INFO
)
parser
=
argparse
.
ArgumentParser
()
parser
.
add_argument
(
"--debug"
,
action
=
"store_true"
)
parser
.
add_argument
(
"--interval"
,
type
=
float
,
default
=
30.0
,
help
=
"poll seconds"
)
args
=
parser
.
parse_args
()
if
args
.
debug
:
logging
.
getLogger
(
"zeroconf"
).
setLevel
(
logging
.
DEBUG
)
info
=
AsyncServiceInfo
(
"_http._tcp.local."
,
"Interface Monitor Demo._http._tcp.local."
,
port
=
80
,
properties
=
{
"path"
:
"/"
},
server
=
"interface-monitor-demo.local."
,
)
loop
=
asyncio
.
get_event_loop
()
runner
=
AsyncRunner
()
try
:
loop
.
run_until_complete
(
runner
.
run
(
info
,
args
.
interval
))
except
KeyboardInterrupt
:
loop
.
run_until_complete
(
runner
.
close
(
info
))
You can’t perform that action at this time.