Last active
April 12, 2024 08:13
-
-
Save Zxilly/eaf52c9facb06b51f3192f2f1903f350 to your computer and use it in GitHub Desktop.
This file contains 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
import socket | |
import argparse | |
def main(server_ip, server_port): | |
with socket.socket(socket.AF_INET, socket.SOCK_DGRAM) as server_socket: | |
server_socket.bind((server_ip, server_port)) | |
print(f"Server is listening on {server_ip}:{server_port}") | |
while True: | |
data, client_address = server_socket.recvfrom(10000) | |
print(f"Received data from {client_address}: {len(data)}") | |
server_socket.sendto(data, client_address) | |
print(f"Sent data back to {client_address}: {len(data)}") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser(description="UDP server with command line arguments") | |
parser.add_argument("--server_ip", default="0.0.0.0", help="Server IP address") | |
parser.add_argument("--port", default=45793, type=int, help="Server port number") | |
args = parser.parse_args() | |
main(args.server_ip, args.port) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment