-
Notifications
You must be signed in to change notification settings - Fork 0
/
listen_tcp.py
37 lines (31 loc) · 1.08 KB
/
listen_tcp.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
import sys
import net
def handle_conn(c: net.TCPConn):
print(f'recieved connection from {c.remote_addr()}')
b = c.read(1024)
print('in listens handle conn after reading')
c.write(b)
print(f'read and wrote msg: {b.decode("utf8")} of len {len(b)} bytes to client connection')
c.close()
"""
yeah guesss what! keyboard interrupts on the sockets do not work.
its only on windows as usual, this people are crazy, nobody wants
to be thinking about special ways of handling keyboard interrupts
this should be a straightforward thing on every platform.
"""
def use_listen_tcp(address, network):
laddr = net.resolve_tcp_addr(address, network)
lstn = net.listen_tcp(laddr, network)
# lstn.settimeout(20.0)
print(f'listening and serving tcp on {lstn.local_addr()}')
while True:
tcp_conn = lstn.accept()
handle_conn(tcp_conn)
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_tcp(args[0], args[1])
if __name__ == '__main__':
main()