Skip to content

Commit 2ce95f5

Browse files
committed
Refactor examples
1 parent 6a7cd31 commit 2ce95f5

3 files changed

Lines changed: 21 additions & 19 deletions

File tree

examples/zwebbrowse.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
""" Example of browsing for a service (in this case, HTTP) """
44

55
import socket
6-
import time
76

8-
from zeroconf import ServiceBrowser, Zeroconf
7+
from zeroconf import raw_input, ServiceBrowser, Zeroconf
98

109

1110
class MyListener(object):
@@ -36,10 +35,11 @@ def addService(self, zeroconf, type, name):
3635

3736
if __name__ == '__main__':
3837
print("Multicast DNS Service Discovery for Python Browser test")
39-
r = Zeroconf()
38+
zeroconf = Zeroconf()
4039
print("Testing browsing for a service...")
41-
type = "_http._tcp.local."
4240
listener = MyListener()
43-
browser = ServiceBrowser(r, type, listener)
44-
time.sleep(5)
45-
r.close()
41+
browser = ServiceBrowser(zeroconf, "_http._tcp.local.", listener)
42+
try:
43+
raw_input("Waiting (press Enter to exit)...")
44+
finally:
45+
zeroconf.close()

examples/zwebtest.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,7 @@
44

55
import socket
66

7-
from zeroconf import ServiceInfo, Zeroconf
8-
9-
try:
10-
raw_input
11-
except NameError:
12-
raw_input = input
7+
from zeroconf import raw_input, ServiceInfo, Zeroconf
138

149
desc = {'path': '/~paulsm/'}
1510

@@ -18,10 +13,12 @@
1813
socket.inet_aton("10.0.1.2"), 80, 0, 0,
1914
desc, "ash-2.local.")
2015

21-
r = Zeroconf()
16+
zeroconf = Zeroconf()
2217
print("Registration of a service...")
23-
r.registerService(info)
24-
raw_input("Waiting (press Enter to exit)...")
25-
print("Unregistering...")
26-
r.unregisterService(info)
27-
r.close()
18+
zeroconf.registerService(info)
19+
try:
20+
raw_input("Waiting (press Enter to exit)...")
21+
finally:
22+
print("Unregistering...")
23+
zeroconf.unregisterService(info)
24+
zeroconf.close()

zeroconf.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@
5757
else:
5858
byte_ord = ord
5959

60+
try:
61+
raw_input = raw_input
62+
except NameError:
63+
raw_input = input
64+
6065
# hook for threads
6166

6267
_GLOBAL_DONE = False

0 commit comments

Comments
 (0)