Skip to content

Instantly share code, notes, and snippets.

@jonEbird
Created February 28, 2013 02:18
Show Gist options
  • Save jonEbird/5053648 to your computer and use it in GitHub Desktop.
Save jonEbird/5053648 to your computer and use it in GitHub Desktop.
Slightly modified version of a remote Python pdb session found elsewhere. Just had to add SO_REUSEADDR socket option.
#!/bin/env python
# Taken from http://www.dzone.com/snippets/remote-debugging-python-using
# Only added the SO_REUSEADDR socket option since I hit the breakpoint often
import pdb, socket, sys
class Rdb(pdb.Pdb):
def __init__(self, port=4444):
self.old_stdout = sys.stdout
self.old_stdin = sys.stdin
self.skt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.skt.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
self.skt.bind((socket.gethostname(), port))
self.skt.listen(1)
(clientsocket, address) = self.skt.accept()
handle = clientsocket.makefile('rw')
pdb.Pdb.__init__(self, completekey='tab', stdin=handle, stdout=handle)
sys.stdout = sys.stdin = handle
def do_continue(self, arg):
sys.stdout = self.old_stdout
sys.stdin = self.old_stdin
self.skt.close()
self.set_continue()
return 1
do_c = do_cont = do_continue
# Example usage - connect with 'telnet <hostname> 4444'
if __name__=='__main__':
def buggy_method():
x = 3
remote_debug = Rdb()
remote_debug.set_trace()
print x
buggy_method()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment