Do some experiment about UDP by Python3 and netstat.
Code
1 | # coding: utf-8 |
A and B start
A: 1
2
3
4
5
6
7
8
9
10
11$ python3 client.py
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:
B: 1
2
3
4
5
6
7
8
9
10
11$ python3 client.py
Python 3.5.2 (default, Oct 8 2019, 13:06:37)
Type "copyright", "credits" or "license" for more information.
IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.
In [1]:1
2
3$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
A bind
A: 1
In [1]: sock.bind(('localhost', 10001))
1
2
3
4$ netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 localhost:10001 *:*
B send to A
B: 1
2In [1]: sock.sendto(b'hello world', ('localhost', 10001))
Out[1]: 11
A: 1
2In [2]: sock.recvfrom(1024)
Out[2]: (b'hello world', ('127.0.0.1', 42616))
1 | $ netstat -au |
A send to B
A: 1
2In [3]: sock.sendto(b'This is a response.', ('127.0.0.1', 42616))
Out[3]: 19
B: 1
2In [2]: sock.recvfrom(1024)
Out[2]: (b'This is a response.', ('127.0.0.1', 10001))
1 | $ netstat -au |
B connect to A
B: 1
In [4]: sock.connect(('127.0.0.1', 10001))
1 | $ netstat -au |
A: 1
2In [4]: sock.sendto(b'This is another response.', ('127.0.0.1', 42616))
Out[4]: 25
B: 1
2In [5]: sock.recvfrom(1024)
Out[5]: (b'This is another response.', ('127.0.0.1', 10001))
C: 1
2
3
4In [1]: sock.bind(('127.0.0.1', 10010))
In [2]: sock.sendto(b'This is a response from C.', ('127.0.0.1', 42616))
Out[2]: 26
B: 1
2In [7]: sock.recvfrom(1024)
blocked