-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlisten.py
More file actions
51 lines (43 loc) · 1.3 KB
/
Copy pathlisten.py
File metadata and controls
51 lines (43 loc) · 1.3 KB
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
import sys
import net
def printlen(b):
print(f'------ read {len(b)} from the connections ------')
def handle_grams(c):
b, naddr = c.read_from()
printlen(b)
print(f'recieved connection from {c.local_addr()}')
c.write_to(b, naddr)
def handle_streams(c):
print('recieved connection from c.remote_addr()')
b = c.read()
printlen(b)
c.write(b)
c.close()
def use_listen(address, network):
try:
lstn = net.listen(address, network)
# lstn.settimeout(20.0)
print(f'listening and serving {network} on {lstn.local_addr()}')
while True:
if network == 'unix':
lstn.unlink_on_close(True)
if net.net_is_valid('tcp', network) or network == 'unix':
nc = lstn.accept()
handle_streams(nc)
elif net.net_is_valid('udp', network) or network == 'unixgram':
handle_grams(lstn)
else:
print('Invalid network i guess')
lstn.close()
sys.exit(1)
except Exception as e:
lstn.close()
raise e
def main():
args = sys.argv[1:]
if len(args) != 2:
print('you a valid address and network for this to work', args)
exit(1)
use_listen(*args)
if __name__ == '__main__':
main()