forked from benmechen/Discoverable-Client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
52 lines (45 loc) · 2.1 KB
/
server.py
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
import socket
import datetime
from zeroconf import ServiceInfo, Zeroconf
HOST = '***ENTER IP ADDRESS HERE***'
PORT = 1024 # Port to listen on (non-privileged ports are > 1023)
address = ""
print("#############################")
print("#### DISCOVERABLE SERVER ####")
print("#############################\n")
serverSocket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
serverSocket.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
hostName = socket.gethostname()
serverSocket.bind((HOST, PORT))
print(" > Server started on " + socket.gethostname() + ":" + HOST + ":" + str(PORT))
zeroconf = Zeroconf()
fqdn = socket.gethostname()
hostname = fqdn.split('.')[0]
desc = {'service': 'Discoverable Service', 'version': '1.0.0'}
info = ServiceInfo('_discoverable._udp.local.',
hostname + ' Service._discoverable._udp.local.',
addresses=[socket.inet_aton(HOST)], port=PORT, properties=desc)
try:
zeroconf.register_service(info)
print(" > Discoverable service " + str(desc) + " registered:\n" + str(info))
while True:
message, address = serverSocket.recvfrom(PORT)
string = message.decode('utf-8')
print(" > Received: " + message.decode('utf-8') + " from " + str(address))
serverSocket.sendto("dscv_ack".encode('utf-8'), address)
if "dscv_discover" in string:
returnMessage = "dscv_shake:" + HOST
print(" > Discover call from client: " + str(address))
print(" > Sending handshake: " + returnMessage + ", to address: " + str(address))
serverSocket.sendto(returnMessage.encode('utf-8'), address)
print(" > [" + str(datetime.datetime.now()) + "] New client connected <" + address[0] + ">")
elif "dscv_disconnect" in string:
break
finally:
print("\n > Shutting down server [" + str(datetime.datetime.now()) + "]")
if address != "":
serverSocket.sendto("dscv_disconnect".encode('utf-8'), address)
zeroconf.unregister_service(info)
zeroconf.close()
serverSocket.close()