-
Notifications
You must be signed in to change notification settings - Fork 0
/
receiver.py
53 lines (41 loc) · 1.48 KB
/
receiver.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
53
#!/usr/bin/env python3
#receiver.py
import os, sys, getopt, time
from netinterface import network_interface
NET_PATH = './'
OWN_ADDR = 'B'
# ------------
# main program
# ------------
try:
opts, args = getopt.getopt(sys.argv[1:], shortopts='hp:a:', longopts=['help', 'path=', 'addr='])
except getopt.GetoptError:
print('Usage: python receiver.py -p <network path> -a <own addr>')
sys.exit(1)
for opt, arg in opts:
if opt == '-h' or opt == '--help':
print('Usage: python receiver.py -p <network path> -a <own addr>')
sys.exit(0)
elif opt == '-p' or opt == '--path':
NET_PATH = arg
elif opt == '-a' or opt == '--addr':
OWN_ADDR = arg
if (NET_PATH[-1] != '/') and (NET_PATH[-1] != '\\'): NET_PATH += '/'
if not os.access(NET_PATH, os.F_OK):
print('Error: Cannot access path ' + NET_PATH)
sys.exit(1)
if len(OWN_ADDR) > 1: OWN_ADDR = OWN_ADDR[0]
if OWN_ADDR not in network_interface.addr_space:
print('Error: Invalid address ' + OWN_ADDR)
sys.exit(1)
# main loop
netif = network_interface(NET_PATH, OWN_ADDR)
print('Main loop started...')
while True:
# Calling receive_msg() in non-blocking mode ...
# status, msg = netif.receive_msg(blocking=False)
# if status: print(msg) # if status is True, then a message was returned in msg
# else: time.sleep(2) # otherwise msg is empty
# Calling receive_msg() in blocking mode ...
status, msg = netif.receive_msg(blocking=True) # when returns, status is True and msg contains a message
print(msg.decode('utf-8'))