Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Simple Python UDP echo server
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
# Author: David Manouchehri <manouchehri@protonmail.com>
# 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)
@eriksons

This comment has been minimized.

Copy link

@eriksons eriksons commented Apr 27, 2018

How can I print the message I'm sending back and forth?

@ale-rinaldi

This comment has been minimized.

Copy link

@ale-rinaldi ale-rinaldi commented Dec 29, 2019

How can I print the message I'm sending back and forth?

Edit line 22:
print("Echoing data back to " + str(client_address) + ": " + payload)

@obito02

This comment has been minimized.

Copy link

@obito02 obito02 commented Feb 12, 2020

Thanks for this scrip, is small and easy for use, am using for test UDP packets thank you.

@jcwarp

This comment has been minimized.

Copy link

@jcwarp jcwarp commented Apr 27, 2021

David, thanks for this little gist; I updated it for python 3
https://gist.github.com/jcwarp/738ea6a00b5b1d19d4a0316663bb3151

@andria-dev

This comment has been minimized.

Copy link

@andria-dev andria-dev commented Aug 25, 2021

It looks like this one already runs in Python 3 just fine. Also, I'm not sure if sock.recvfrom(1) was intentional but nmap can't detect it as an echo server so if you do service detection, it sits there for quite some time guessing incorrectly. Using sock.recvfrom(1024) lets nmap quickly detect that it is an echo server.

@Manouchehri

This comment has been minimized.

Copy link
Owner Author

@Manouchehri Manouchehri commented Aug 28, 2021

@andria-dev The issue with using sock.recvfrom(1024), is that if you send <1024 bytes of data, you won't get a reply.

@andria-dev

This comment has been minimized.

Copy link

@andria-dev andria-dev commented Aug 29, 2021

@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 the buffersize and calling the recv method will "receive up to buffersize 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 the buffersize 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 to 1024 and then run this client program, you'll see it print out Test passed.

#!/usr/bin/env python3
import socket

client_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
server_address = '0.0.0.0'
server_port = 31337
client_socket.connect((server_address, server_port))

message = 'Hello World'
client_socket.send(message.encode())
response = client_socket.recv(1024).decode()
print("Test passed" if message == response else "Test failed")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment