Add a udp program and a ping test script

This commit is contained in:
yufeng 2023-02-06 19:22:44 +08:00
parent 9355acf903
commit d31be8cba0
2 changed files with 63 additions and 0 deletions

18
ping.py Normal file
View file

@ -0,0 +1,18 @@
import socket
import sys
import time
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
addr = ('localhost', 26099)
sock.bind(addr)
print("pinging...", file=sys.stderr)
while True:
buf, raddr = sock.recvfrom(4096)
print("receive: " + buf.decode("utf-8"))
buf = "this is a ping to port 6200!".encode('utf-8')
sock.sendto(buf, ("127.0.0.1", 6200))
buf = "this is a ping to reply!".encode('utf-8')
sock.sendto(buf, raddr)
time.sleep(1)