Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
UDP Hole Punching test tool
#!/usr/bin/env python
# - UDP Hole Punching test tool
# Usage: remote_host remote_port
# Run this script simultaneously on 2 hosts to test if they can punch
# a UDP hole to each other.
# * remote_port should be identical on 2 hosts.
# * if remote_port < 1024, must be root.
# * tested on python 2.5.
# Copyright (C) 2009 Dmitriy Samovskiy,
# License: Apache License, Version 2.0
import sys, os, time, socket, random
from select import select
def log(*args):
print time.asctime(), ' '.join([str(x) for x in args])
def puncher(remote_host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
sock.bind(('', port))
my_token = str(random.random())
log("my_token =", my_token)
remote_token = "_"
remote_knows_our_token = False
for i in range(60):
r,w,x = select([sock], [sock], [], 0)
if remote_token != "_" and remote_knows_our_token:
log("we are done - hole was punched from both ends")
if r:
data, addr = sock.recvfrom(1024)
log("recv:", data)
if remote_token == "_":
remote_token = data.split()[0]
log("remote_token is now", remote_token)
if len(data.split()) == 3:
log("remote end signals it knows our token")
remote_knows_our_token = True
if w:
data = "%s %s" % (my_token, remote_token)
if remote_token != "_": data += " ok"
log("sending:", data)
sock.sendto(data, (remote_host, port))
log("sent", i)
return remote_token != "_"
if __name__ == '__main__':
remote_host = sys.argv[1]
port = int(sys.argv[2])
if puncher(remote_host, port):
log("Punched UDP hole to %s:%d successfully" % (remote_host, port))
log("Failed to punch hole")

This comment has been minimized.

Copy link

avaranovich commented Jan 26, 2013

I tried to change the code to support TCP hole punching, by introducing this:

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind(('', port))

Once I start the server, getting the following error

Sat Jan 26 18:51:02 2013 my_token = 0.398781599164
Traceback (most recent call last):
File "./", line 75, in
if puncher(remote_host, port):
File "./", line 49, in puncher
data, addr = sock.recvfrom(1024)
socket.error: [Errno 107] Transport endpoint is not connected

Any ideas, why select indicates "read" and socket tries to read the data, but obviously there is no client connected at this moment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.