IOError: sys.stdout access restricted by mod_wsgi
I've been using simple print statements to help with debugging my app
and as a result of this, I guess I'll have to hunt down every little
print statement in my code. PITA.
K
---
# This will elicit a HTTP 500 from Apache
def application(environ, start_response):
print 'Something to console?'
# Ieeeeee!!!
status = '200 OK'
output = 'WSGI is running!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
Well, using print statements isn't really compatible with the WSGI
protocol, so you shouldn't use print statements anymore (I raise
Exceptions instead). But, since this is a PITA for your "legacy" code
which does contain print statements, you could also use the
WSGIRestrictStdout directive, see [1].
Regards,
Manuzhai
[1] http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIRedirectStdout
Also read section "Writing To Standard Output" in:
http://code.google.com/p/modwsgi/wiki/ApplicationIssues
The other alternative that document provides is that at the start up
your script file you add:
import sys
sys.stdout = sys.stderr
This is preferred over WSGIRestrictStdout as the latter applies
globally to the web server and not just you application.
The reasons why writing to sys.stdout is bad in a WSGI application are
explained there also.
The best thing to do is that your code should actually use:
import sys
print >> sys.stderr, 'hello world'
if needing to do this at global scope in a script file or Python module.
If within a WSGI application handler, it should use:
print >> environ['wsgi.errors'], 'hello world'
The reason for only ever logging via the 'wsgi.errors' object in the
WSGI environment, is that that is the only way one would be guaranteed
that the messages would go to a virtual host specific error log file
under Apache. If you just use sys.stderr and you are running your WSGI
application in mod_wsgi default embedded mode, it will go to the main
Apache error log file, which is a shared hosting system you may not
have access to.
Also read notes about error log files and debugging in:
http://code.google.com/p/modwsgi/wiki/DebuggingTechniques
Graham
---
# Send anything going to stdout to stderr instead
import sys
sys.stdout = sys.stderr
def application(environ, start_response):
print 'Something to stdout'
status = '200 OK'
output = 'WSGI is running!'
response_headers = [('Content-type', 'text/plain'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
---
And this is what was logged to the main Apache error log file.
---
[Sat Jun 30 22:29:48 2007] [notice] Apache/2.2.3 (Debian) PHP/
5.2.0-8+etch1 mod_wsgi/1.0-TRUNK Python/2.4.4 mod_perl/2.0.2 Perl/
v5.8.8 configured -- ...
[Sat Jun 30 22:29:54 2007] [notice] mod_wsgi (pid=13478): Create
interpreter 'beta.domain.org|'.
[Sat Jun 30 22:29:54 2007] [notice] mod_wsgi (pid=13478, daemon='',
group='beta.domain.org|'): Loading WSGI script '/home/beta/wsgi/
myapp.wsgi'.
[Sat Jun 30 22:29:54 2007] [error] Something to stdout
[Sat Jun 30 22:29:54 2007] [notice] mod_wsgi (pid=13483): Attach
interpreter ''.
---
So it all appears to work. I'll take your advice though and get rid of
the print statements anyway. Cheers.
K
On Jun 30, 4:44 pm, "Graham Dumpleton" <graham.dumple...@gmail.com>
wrote: