Skip to content

Instantly share code, notes, and snippets.

@ssrlive
Last active February 23, 2022 13:57
Show Gist options
  • Save ssrlive/881c4ba0e6777a71eecefdf98f1329b0 to your computer and use it in GitHub Desktop.
Save ssrlive/881c4ba0e6777a71eecefdf98f1329b0 to your computer and use it in GitHub Desktop.
udp-cli.py
#!/usr/bin/python
'''
python UDP echo client by ccc <yyy@gmail>
USAGE: %s <server_ip> <server_port> <message>
'''
import socket
import time
from sys import argv
def echoUdpData(host, port, msg):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print('connect to server: ', host, port)
print('send msg: ', msg)
sock.sendto(msg.encode('utf-8'), (host, port))
sock.settimeout(3)
try:
data, addr = sock.recvfrom(1024)
print('recv msg: ', data.decode('utf-8'))
except:
print('Failed. socket.recvfrom time out')
sock.close()
print('>>>Start ...')
if __name__ == '__main__':
if len(argv) !=4:
print(__doc__ % argv[0])
else:
echoUdpData(argv[1], int(argv[2]), argv[3])
print('>>>END!')
@ssrlive
Copy link
Author

ssrlive commented Feb 17, 2022

test suits.

#!/usr/bin/python3
# -*- coding: utf-8 -*-

import threading
import time
import os

exitFlag = 0

class myThread (threading.Thread):
    def __init__(self, threadID, name, delay):
        threading.Thread.__init__(self)
        self.threadID = threadID
        self.name = name
        self.delay = delay
    def run(self):
        #print ("start sub-thread " + self.name)
        do_some_job(self.name, self.delay, 5)
        #print ("exiting sub-thread " + self.name)

def do_some_job(threadName, delay, counter):
    while counter:
        if exitFlag:
            threadName.exit()
        time.sleep(delay)
        #print ("%s: %s" % (threadName, time.ctime(time.time())))
        dirname = os.path.dirname(__file__)
        os.system('python {}/udp-cli.py 123.45.67.89 31337 {}'.format(dirname, threadName))
        counter -= 1

# 创建新线程
thread1 = myThread(1, "Thread-1", 1)
thread2 = myThread(2, "Thread-2", 2)
thread3 = myThread(3, "Thread-3", 1)
thread4 = myThread(4, "Thread-4", 1)
thread5 = myThread(5, "Thread-5", 1)
thread6 = myThread(6, "Thread-6", 1)
thread7 = myThread(7, "Thread-7", 1)
thread8 = myThread(8, "Thread-8", 1)
thread9 = myThread(9, "Thread-9", 1)

# 开启新线程
thread1.start()
thread2.start()
thread3.start()
thread4.start()
thread5.start()
thread6.start()
thread7.start()
thread8.start()
thread9.start()

thread1.join()
thread2.join()
thread3.join()
thread4.join()
thread5.join()
thread6.join()
thread7.join()
thread8.join()
thread9.join()

print ("exiting main thread")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment