Skip to content

Instantly share code, notes, and snippets.

@thomaskeck
Last active September 25, 2022 21:10
Show Gist options
  • Save thomaskeck/54de54a0872f49eb7ff7d3938af0bb2a to your computer and use it in GitHub Desktop.
Save thomaskeck/54de54a0872f49eb7ff7d3938af0bb2a to your computer and use it in GitHub Desktop.
A Python Remote Post Mortem Debugger
# Server Application which should be debugged
def acticvate_remote_post_mortem_debugger():
def info(type, value, tb):
import traceback
traceback.print_exception(type, value, tb)
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind(('127.0.0.1', 8002))
server.listen(1)
connection, address = server.accept()
iofile = connection.makefile("rw")
import pdb
debugger = pdb.Pdb(stdin=iofile, stdout=iofile)
debugger.reset()
debugger.interaction(None, tb)
import sys
sys.excepthook = info
acticvate_remote_post_mortem_debugger()
# Client used to connect to the server debugger
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('127.0.0.1', 8002))
import sys
run = True
def output():
while run:
sys.stdout.write(s.recv(1).decode('utf-8'))
import threading
thr = threading.Thread(target=output)
thr.start()
while run:
line = sys.stdin.readline()
if line == 'quit\n':
run = False
s.send(line.encode('utf-8'))
thr.join()
s.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment