I'm new to mod_python and Apache, and I'm having trouble returning a file to a user after a GET request. I've got a very simple setup right now, and was hoping to simply open the file and write it to the response:
from mod_python import apache
def handler(req):
req.content_type = 'application/octet-stream'
fIn = open('response.bin', 'rb')
req.write(fIn.read())
fIn.close()
return apache.OK
However, I'm getting errors when I use open(), saying that the file doesn't exist (even though I've checked a dozen times that it does). This happens when using relative and absolute filepaths.
I've got two questions:
- Why isn't open() finding the right files?
- What is the best way to return a file from the filesystem? (I ask to make sure I'm not missing some better way to use mod_python to return a file.)
Thanks
Edit: After finding this thread: http://www.programmingforums.org/thread12384.html I discovered that open() works for me if I move the file to another directory outside of home (I was aliasing out of /home/myname/httpdocs, but it works if I use /data). Any ideas why that works?
Edit 2: Part of my debug error, as requested:
MOD_PYTHON ERROR
ProcessId: 13642
Interpreter: '127.0.1.1'
ServerName: '127.0.1.1'
DocumentRoot: '/var/www'
URI: '/test/mptest.py'
Location: None
Directory: '/home/myname/httpdocs/'
Filename: '/home/myname/httpdocs/mptest.py'
PathInfo: ''
Phase: 'PythonHandler'
Handler: 'mptest'
Traceback (most recent call last):
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1537, in HandlerDispatch
default=default_handler, arg=req, silent=hlist.silent)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1229, in _process_target
result = _execute_target(config, req, object, arg)
File "/usr/lib/python2.5/site-packages/mod_python/importer.py", line 1128, in _execute_target
result = object(arg)
File "/home/myname/httpdocs/mptest.py", line 13, in handler
fIn = open('/home/myname/httpdocs/files/response.bin', 'rb')
IOError: [Errno 2] No such file or directory: '/home/myname/httpdocs/files/response.bin'