Attaching CGI python script to PyCharm debugger?

1.6k views Asked by At

I'm using Community Edition PyCharm 4.5.1 and I'm developing CGI python scripts. My needs are to start the debugger and attach the script (then break to the first breakpoint) once it is called by my HTTP client.

I don't know if I can, I hope. Everything works fine, from the Python server to the HTML/JavaScript code that calls my CGI script. Also, I'm perfectly able to debug a Python script I just start normally. But now the problem is that it is the HTTP server that starts the script, neither me (from command line) nor the debugger itself.

Any idea? Thanks!

2

There are 2 answers

1
Alex Ivanov On

AFAIK, you just put into your script

import cgitb    
cgitb.enable()  
print "Content-type: text/html\n\n"

and it's being debugged by itself.

0
Tony_Tong On

I have meet the same problem as yours when i use pycharm on CentOS, but i found pycharm can attach to cgi script automaticly on windows, so i try to follow the source code of CGIRequestHandler, I found there is a difference in CGIRequestHandler.run_cgi() function, it will use fork on linux, and subprocess on windows, so i guess may be these two different ways of creating child process leading to different result. so i try the following code, force it subprocess on linux, and it works!

    CGIHTTPRequestHandler.have_fork = 0
    httpd = HTTPServer(('', port), CGIHTTPRequestHandler)