Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@jollyreal
Created May 4, 2021 12:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jollyreal/20ba35c1d0cb2d760486474807b2669d to your computer and use it in GitHub Desktop.
Save jollyreal/20ba35c1d0cb2d760486474807b2669d to your computer and use it in GitHub Desktop.
use the concurrent module to execute the UDP request
import socket
import select
import time
from concurrent import futures
def udp_request(src_ip,src_port,dst_ip,dst_port):
'''send an UDP packet that contain a string of "hello" to the dst_ip:dst_port from src_ip:src_port'''
# create the request socket
request_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
request_socket.bind((src_ip,src_port))
request_socket.setblocking(0)
# prepare the payload
payload_string = "hello\r\n"
payload_ascii = payload_string.encode("utf-8").hex()
payload = bytes.fromhex(payload_ascii)
# send out the UDP packet
request_socket.sendto(payload,(dst_ip,dst_port))
# use select to set timeout of 1 sec
# if there is a response from the server, return the payload from server
# if nothing response after 1 sec, return "no response"
ready = select.select([request_socket],[],[],1)
if ready[0]:
(server_data,(server_ip,server_port)) = request_socket.recvfrom(1024)
response = f"{server_ip}:{server_port} -> {src_ip}:{src_port} == {server_data.decode().strip()}"
else:
response = f"no response from {dst_ip}"
request_socket.close()
return response
def concurrent_request(src_ips,src_ports,dst_ips,dst_ports,max_worker=20):
'''use the concurrent method to execute the udp_request function'''
# decide the number of maximum workers
workers = min(max_worker,len(dst_ports))
with futures.ThreadPoolExecutor(workers) as executor:
res = executor.map(udp_request,src_ips,src_ports,dst_ips,dst_ports)
answer = []
for item in res:
answer.append(item)
return answer
if __name__ == "__main__":
# constant of the program
SRC_IP = "192.168.11.13"
SRC_PORT_START = 12024
SRC_PORT_END = 12124
DST_IP = "192.168.11.10"
DST_PORT_START = 10024
DST_PORT_END = 10124
src_ip_list = [SRC_IP for _ in range(SRC_PORT_END - SRC_PORT_START)]
src_port_list = [i for i in range(SRC_PORT_START,SRC_PORT_END)]
dst_ip_list = [DST_IP for _ in range(DST_PORT_END-DST_PORT_START)]
dst_port_list = [i for i in range(DST_PORT_START,DST_PORT_END)]
# use the for loop method to send 100 times udp request print out the time taken of execution
time_before = time.time()
for _src_port,_dst_port in zip(src_port_list,dst_port_list):
response = udp_request(SRC_IP,_src_port,DST_IP,_dst_port)
# print(response)
time_after = time.time()
print(f"one by one: {time_after-time_before} sec")
# use the concurrent method to send 100 times udp request print out the time taken of execution
time_before = time.time()
concurrent_request(src_ip_list,src_port_list,dst_ip_list,dst_port_list)
time_after = time.time()
print(f"concurrent: {time_after-time_before} sec")
print("program end")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment