-
Notifications
You must be signed in to change notification settings - Fork 0
/
client_tcp.py
47 lines (39 loc) · 1.25 KB
/
client_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
38
39
40
41
42
43
44
45
46
47
import sys
import net
def handle_conn(c):
print(f'local_addr: {c.local_addr()} | remote_addr: {c.remote_addr()}')
data = b"random test data"
n = c.write(data)
c.write(b'') # eof
# c.close()
# sys.exit(0)
b = c.read(n)
if data == b:
print('===> read write into socket successful')
print(f'sent and recieved => {b.decode("utf8")} of len {len(b)}')
else:
print('<=== reading and writing sockets not working as expected')
print(f'recieved => {b.deccode("utf8")} of len {len(b)}')
c.close()
def use_dial(address, network):
tcp_client = net.dial(address, network)
# tcp_client.settimeout(20)
handle_conn(tcp_client)
def use_dial_tcp(addr, addr2, network):
laddr = None
if addr:
laddr = net.resolve_tcp_addr(addr, network)
raddr = net.resolve_tcp_addr(addr2, network)
tcp_client = net.dial_tcp(laddr, raddr, network)
handle_conn(tcp_client)
def main():
args = sys.argv[1:]
if len(args) < 2:
print('<=== expected address "host:port" and network "network" but got', *args)
exit(1)
if len(args) == 3:
use_dial_tcp(args[0], args[1], args[2])
else:
use_dial(args[0], args[1])
if __name__ == '__main__':
main()