import socket import time def sendpacket(targetip, targetport, packetsize, protocol,…
- User
import socket import time
def send_packet(target_ip, target_port, packet_size, protocol, source_port=None): message = b'15' * packet_size # Create a message with the desired packet size
if protocol.lower() == 'udp': sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) elif protocol.lower() == 'tcp': sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) else: raise ValueError("Invalid protocol. Use 'tcp' or 'udp'.") try: if source_port: sock.bind(('', source_port)) # Bind to the specified source port if protocol.lower() == 'tcp': sock.connect((target_ip, target_port)) time.sleep(0.1) # Small delay to ensure connection is established sock.sendall(message) print(f"Sent {packet_size} bytes to {target_ip}:{target_port} via TCP") else: sock.sendto(message, (target_ip, target_port)) print(f"Sent {packet_size} bytes to {target_ip}:{target_port} via UDP") except Exception as e: print(f"An error occurred: {e}") finally: sock.close()if name == "main": target_ip = "192.168.2.50" # Example IP address target_port = 27015 # Example port number packet_size = 800 # Example packet size in bytes repeat_times = 146700 # Number of times to send the packet delay_between_packets = 0 # Delay between packets in seconds (optional) protocol = "udp" # Protocol: "tcp" or "udp"
for i in range(repeat_times): try: send_packet(target_ip, target_port, packet_size, protocol) except Exception as e: print(f"Failed to send packet #{i+1}: {e}") if i < repeat_times - 1: # Add delay if it's not the last iteration time.sleep(delay_between_packets) - Assistant