Skip to content

Commit ff882e2

Browse files
committed
udp 测试工具
1 parent 80aeaa3 commit ff882e2

2 files changed

Lines changed: 52 additions & 0 deletions

File tree

‎udp/client.py‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import socket
4+
from sys import argv
5+
import time
6+
7+
if len(argv) < 3:
8+
print("input error")
9+
print(argv)
10+
exit(-1)
11+
12+
address = argv[1]
13+
port = int(argv[2])
14+
15+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
16+
addr = (address, port)
17+
18+
19+
while True:
20+
try:
21+
str = "tester"
22+
s.sendto(bytes(str, encoding="utf8"), addr)
23+
response, addr = s.recvfrom(1024)
24+
print("Receive from %s:%s" % addr)
25+
print(response.decode())
26+
time.sleep(10)
27+
except KeyboardInterrupt:
28+
break

‎udp/server.py‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#!/usr/bin/env python3
2+
# -*- coding: utf-8 -*-
3+
import socket
4+
from sys import argv
5+
6+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
7+
print(argv)
8+
9+
if len(argv) < 2:
10+
print("no port...")
11+
exit(-1)
12+
13+
port = int(argv[1])
14+
s.bind(("0.0.0.0", port))
15+
16+
print("UDP bound on port ", port)
17+
18+
while True:
19+
try:
20+
data, addr = s.recvfrom(1024)
21+
print("Receive from %s:%s" % addr)
22+
s.sendto(b"Hello %s!\n" % data, addr)
23+
except KeyboardInterrupt:
24+
break

0 commit comments

Comments
 (0)