Created
April 15, 2023 11:33
-
-
Save 44670/c053274566bf0fb0acf18df89a5eb17c to your computer and use it in GitHub Desktop.
Netcat-like UDP Tool in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Netcat-like UDP Tool in Python | |
This Python script provides a simple and lightweight netcat-like UDP tool that allows users to | |
send and receive data over UDP using IPv4 and IPv6 addresses. It is designed to be easy to use | |
and versatile, making it suitable for various networking tasks, including testing, debugging, | |
and learning purposes. | |
By following the netcat command-line style, this script offers users a familiar interface for | |
communicating over UDP. When launched with only a port number, the script will listen for incoming | |
UDP traffic. When supplied with a target IP address and port, the script will both send and | |
receive UDP data. | |
Keywords: Python, Netcat, UDP, Networking, IPv4, IPv6, Socket Programming, Command Line Tool, | |
Lightweight, Versatile, Testing, Debugging, Learning | |
To use this script, run the following commands: | |
1. For receiving data only: | |
python netcat_udp.py <port> | |
2. For sending and receiving data: | |
python netcat_udp.py <port> <target_host> <target_port> | |
Replace <port> with the desired local port number, <target_host> with the target IPv4 or IPv6 | |
address, and <target_port> with the target port number. | |
""" | |
import socket | |
import sys | |
import threading | |
def receiver(recv_sock, port): | |
recv_sock.bind(('', port)) | |
print(f"Listening on UDP port {port}...") | |
while True: | |
data, addr = recv_sock.recvfrom(4096) | |
print(f"Received from {addr}: {data.decode('utf-8')}") | |
def sender(send_sock, target_addr): | |
print(f"Sending data to {target_addr[0]} on UDP port {target_addr[1]}...") | |
while True: | |
line = input() | |
send_sock.sendto(line.encode('utf-8'), target_addr) | |
def main(): | |
argc = len(sys.argv) | |
if argc != 2 and argc != 4: | |
print("Usage: python netcat_udp.py <port> [target_host target_port]") | |
sys.exit(1) | |
port = int(sys.argv[1]) | |
recv_sock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
recv_sock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, False) | |
recv_thread = threading.Thread(target=receiver, args=(recv_sock, port)) | |
recv_thread.daemon = True # exit when main thread exits | |
recv_thread.start() | |
if argc == 4: | |
target_host = sys.argv[2] | |
target_port = int(sys.argv[3]) | |
addr_info = socket.getaddrinfo(target_host, target_port, socket.AF_UNSPEC, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
target_family, _, _, _, target_addr = addr_info[0] | |
send_sock = socket.socket(target_family, socket.SOCK_DGRAM, socket.IPPROTO_UDP) | |
send_thread = threading.Thread(target=sender, args=(send_sock, target_addr)) | |
send_thread.daemon = True # exit when main thread exits | |
send_thread.start() | |
send_thread.join() | |
recv_thread.join() | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment