Python Debug Setups?

Hey everyone,

I've been attempting to setup a debugging instance for a while with my Python HTTP Server, that implements the BaseHTTPServer class. Anyways, I've spent about a day trying to find an ideal way to debug this application. In node docker containers, I can easily attach to a remote instance with Visual Studio Code and debug via breakpoints. However, I for the life of me can't get a working debugger instance to work. I've been able to replicate one with flask using the vs code remote debugging extension, but have found it's difficult to setup with a non-flask application.

Ideally I could configure it use docker-compose as I want to docker to replicate the directory structure of the server this application will run on.

Any suggestions, or previous experience with configuring launch.json files for vs code, or debugging python code in docker?

Here's an example of the server code:

The code itself runs and compiles, however, I would like to be able to attach a debugger after the container runs the application, or alternatively, find a way to launch the code once a debugger has connected to the code.

import time import BaseHTTPServer import json import os HOST_NAME = '0.0.0.0' PORT_NUMBER = 9000 # Maybe set this to 9000. class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler): def do_HEAD(self): self.send_response(200) self.send_header("Content-type", "text/json") self.end_headers() def do_GET(self): print(self.path) """Respond to a GET request.""" self.send_response(200) self.send_header("Content-type", "text/json") self.end_headers() if(self.path == "/helloWorld"): return self.wfile.write("hello world") def do_POST(self): if(self.path == "/helloWorld"): # implement post logic here return self.wfile.write("hello world") else: self.send_error(501, "Wrong endpoint - rejected") if __name__ == '__main__': print(path) print(ENVIRONMENT) server_class = BaseHTTPServer.HTTPServer httpd = server_class((HOST_NAME, PORT_NUMBER), MyHandler) print time.asctime(), "Server Starts - %s:%s" % (HOST_NAME, PORT_NUMBER) try: httpd.serve_forever() except KeyboardInterrupt: pass httpd.server_close() print time.asctime(), "Server Stops - %s:%s" % (HOST_NAME, PORT_NUMBER) 

and an example docker-compose file:

version: '3' services: python-server: build: dockerfile: Dockerfile context: . ports: - "9000:9000" - "3000:3000" volumes: - ./gitRepositories:/gitRepositories 

submitted by /u/Justlegos
[link] [comments]
Source: Reddit