Python cgi weird behaviour - python

I have a simple CGI python script with an html output:
print "Content-Type: text/html"
print
print hours,minutes,seconds,","
When I run it from my linux shell, it runs fine but when called from the browser, all I get is a blank page. I used to have this problem before when I used CRLF EOF instead of LF but I changed that and it worked fine until some recent modification.
I don't know what to do when the script runs fine from shell but not when called from a browser.

<!-- SHTML Wrapper - 500 Server Error --> means that something is going wrong on the server. The most common cause in CGI scripts for this error is that there is an uncaught error in the CGI script itself, causing it to prematurely terminate. Your best bet is to check your Apache error logs. These will normally be in /var/log/<name of Apache install>/error.log (This could be /var/log/apache2/error.log or /var/log/httpd/error.log, etc.)
Since you've said that this works well in the shell, my guess is that this is an issue with sys.path (remember, Apache is a different user, it will have different settings), but I would need to see more to be certain.
Other issues could be:
Different Python version (3 vs. 2)
File has the wrong permissions (755 is the standard, I believe)
Syntax error somewhere in Python which is environmentally dependant

Related

django devserver can't connect

I have a situation where if I run Apache with wsgi (now uninstalled), a test website works, but running the same server with runserver 0.0.0.0:8080 gives ERR_CONNECTION_REFUSED from local or remote (even with the apache2 service stopped).
Edit: I don't think it's Apache, I've reproduced the problem on a clean server with no Apache installed, so unless Apache somehow modified something under source control it's not that
My knowledge of web details is hazy, I don't even know where to troubleshoot this problem - the devserver runs (runserver prints as expected and doesn't give any errors) but never receives a request, I have nothing in iptables.
Sorry for anyone who read this, it would probably have been impossible to solve given my supplied information.
What had actually happened was that I'd been having to modify my wsgi.py script in order to make it happy inside the Apache server, and I'd added a line which said "os.system('/bin/bash --rcfile )" to try and make sure that when running inside apache it got the virtualenv activated.
This line must have been causing some strange problem, another symptom was that I realised when I was running "runserver", it wasn't crashing the the python process was backgrounding itself, where normally it runs inside that console window.
Thanks everyone who asked questions helping me debug!

Attaching CGI python script to PyCharm debugger?

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!
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.
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)

Debugging Python bottle apps with WingIDE

I'm writing an Python Bottle application (Python 2.7.2 and Bottle 0.10.9) and developing it in the WingIDE (3.2.8-1) Professional for Linux. This all works well, except when I want to debug the Bottle application. I have it running in standalone mode within WingIDE, but it won't stop at any of my break points in the code, even if I set Bottle.debug(False). Does anyone have any suggestions/ideas about how I can setup Bottle so it will stop on breakpoints within WingIDE?
If you have the reloader set to true bottle starts a subproccess for the actual app. In Wing you need to turn off the reloader, then it should work.
run(reloader=False).
But you will have to restart the app in wing every time you make changes.
Are you debugging under WSGI using wingdbstub.py or launching bottle from the IDE? I'm not that familiar with bottle but a common problem is a web framework's reload mechanism running code in a sub-process that is not debugged. I'm not certain bottle would do that under WSGI, however, but printing the process id at time of importing wingdbstub (or startup if launching from the IDE) and again at the line where the breakpoint is missed would rule this in our out. The "reloader" arg for Bottle.__init__ may be relevant here. If set to True, try setting it to False when debugging under Wing.
Another thing to try is to raise an exception on purpose where the breakpoint is (like "assert 0, 'test exception'" and see if this exception is reported in Wing's debugger in the Exceptions tool and if so whether Wing also manages to open the source code. If bottle is running code in a way that doesn't make it possible to find the source code then this would still stop on the assertion (Wing's debugger stops on all assertions by default even if the host code handles the exception) but it would fail to show the debug file and would put up a message in the status area (at bottle of IDE screen and in the Messages tool) that indicates the file name the debug process specified. Depending on this it may be possible to fix the problem (but would require modifying Bottle if the file name is something like "".
BTW, to insert code that is only run under Wing's debugger us something like this:
import os
if 'WINGDB_ACTIVE' in os.environ:
# code here
If this doesn't help please email support at wingware dot com.

Executes Fine In Jail Shell but not in Browser

My python script executes fine in Jail shell, putting out html which I can pipe to an html file. When I look at the file, it's exactly what I want. However when I try to run the file from a browser I get a 500 error. According to the instructions at http://imgseekweb.sourceforge.net/install.html the cgi-bin should be in suEXEC mode. My hosting company changed the cgi-script handler to allow .py files and he made a little test script that works fine, but mine still does not.
I tried to make suEXEC a custom Apache file handler of .py files in cPanel, but this did not seem to help, it just made the Python script print out as text in the browser without executing.
Any ideas for resolution. I'm so close now that script at least works in Jail Shell. I even try to fake out Apache by making the test file launch a system execution of the python script but that also caused a 500 error, although it too spit out the correct html in Jail Shell even doing some Lynx like display of the html this time.
Whatever machinations I did also caused the test.py to stop working. It now gives a 500 error too even with all the code I added removed.
My hoster resolved the issue. It turns out I'm working in a Windows environment with Microsoft Expression 2.0 HTML editor. The code needed to be converted to a UNIX environment with dos2unix which is installed in the hoster environment and can be accessed from the shell...Thanks for reading this thread to any who read it.

Cherrypy server does not accept incoming http request on MS Windows if output (stdout) is not redirected

It is a rather strange 'bug'.
I have written a cherrypy based server. If I run it this way:
python simple_server.py > out.txt
It works as expected.
Without the the redirection at the end, however, the server will not accept any connection at all.
Anyone has any idea?
I am using python 2.4 on a Win XP professional machine.
Are you running the script in an XP "command window"? Otherwise (if there's neither redirection nor command window available), standard output might simply be closed, which might inhibit the script (or rather its underlying framework).
CherryPy runs in a "development" mode by default, which includes logging startup messages to stdout. If stdout is not available, I would assume the server is not able to start successfully.
You can change this by setting 'log.screen: False' in config (and replacing it with 'log.error_file: "/path/to/error.log"' if you know what's good for you ;) ). Note that the global config entry 'environment: production' will also turn off log.screen.

Categories