Last active
May 10, 2024 17:45
-
-
Save Manouchehri/67b53ecdc767919dddf3ec4ea8098b20 to your computer and use it in GitHub Desktop.
Simple Python UDP echo server
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
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
# Author: David Manouchehri <[email protected]> | |
# This script will always echo back data on the UDP port of your choice. | |
# Useful if you want nmap to report a UDP port as "open" instead of "open|filtered" on a standard scan. | |
# Works with both Python 2 & 3. | |
import socket | |
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
server_address = '0.0.0.0' | |
server_port = 31337 | |
server = (server_address, server_port) | |
sock.bind(server) | |
print("Listening on " + server_address + ":" + str(server_port)) | |
while True: | |
payload, client_address = sock.recvfrom(1) | |
print("Echoing data back to " + str(client_address)) | |
sent = sock.sendto(payload, client_address) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
@Manouchehri that doesn't seem to be the case. According to the documentation, which you can see with
help(socket.socket.recv)
in the Python interpreter, it is stated that the first argument is thebuffersize
and calling therecv
method will "receive up tobuffersize
bytes from the socket." This would mean that, even if a Python socket only received 1 byte in a UDP packet whilte it was receiving data, it would return that 1 byte. I believe the point of thebuffersize
is to avoid receiving more data than the program can handle (a potential buffer overflow).I just tested it and it works fine with any size of message. If you change the
buffersize
to1024
and then run this client program, you'll see it print outTest passed
.