python walkie talkie

Solutions on MaxInterview for python walkie talkie by the best coders in the world

showing results for - "python walkie talkie"
Isidore
06 Jan 2018
1import sys
2import os
3import socket
4import time
5
6sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
7sock.settimeout(2)
8if "--listen" in sys.argv:
9    print("WalkieTalkie Listener Turned On..")
10    server_address = ('', 9434)
11    sock.bind(server_address)
12    while True:
13        try:
14            data, address = sock.recvfrom(4096)
15            data = str(data.decode('UTF-8'))
16            print(str(address[0])+': '+data)
17        except KeyboardInterrupt:
18            print("Aborted!")
19            break
20        except socket.timeout as e:
21            time.sleep(1)
22            continue
23        except socket.error as e:
24            print(e)
25            sys.exit(1)
26else:
27    print("WalkieTalkie Broadcast Turned On..")
28    sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
29    sock.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1)
30    server_address = ('255.255.255.255', 9434)
31    while True:
32        s=input("Enter message: ")
33        if s == "exit":
34            print("WalkieTalkie Broadcast Turning Off..")
35            sys.exit(1)
36        sock.sendto(s.encode(), server_address)