Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   Python (http://www.programmingforums.org/forum43.html)
-   -   Cannot open a file with mod_python (http://www.programmingforums.org/showthread.php?t=12384)

somehollis Jan 15th, 2007 1:10 PM

Cannot open a file with mod_python
 
My apologies if this should have gone in the web programming forum. Since it pertains to python, I was uncertain.

I'm trying to finish up a small web app that I've been working on (written in python). The gist of it is that it allows me (and my fellow students) to easily track our billing for patients that we have seen. The back end is finished and functional, and I've been working on integrating the web GUI.

The problem arises, however, when I move all my files over to my apache directory (/var/www). Once my files are there, the program can't seem to find them. I've tried using the full path (/var/www/data/cptcodes2007), the path relative to the webserver (/data/cptcodes2007), and simply having the files in the same dir as my .py files.

The file can be read by browsing (in firefox) to localhost/data/cptcodes, and some independent scripts I wrote to check for mod_python functionality work correctly, so I don't believe that it is an apache configuration issue (though I will readily admit I know very little about apache as of yet).

Am I simply trying to look for the file in the wrong place? Am I using os.path.isfile(filename) incorrectly? Relevant code from my test script and the actual routine to read the file posted below. The red section is the error that is being raised.
Test script:
:

from mod_python import apache, util, psp
import audiclock
import sha
import os

def index(req):
        # The publisher will call this function as default,
        req.write('running _get_vars()<br />')
        _get_vars(req)
       
def _get_vars(req):
        formData = req.form
        req.write('grabbed form data; here is what I am seeing:<br />')
        for item in formData.keys():
                req.write('%s:\t%s<br />' % (item, formData[item]))
        req.write('running _set_page()<br />')
        _set_page(req, formData)


def _set_page(req, formData):
        audiclock.read_all()
        if formData.has_key('currentPage') and formData['currentPage'] == 'process_me':
                if formData.has_key('luser') and formData.has_key('lapp') and formData.has_key('lpatient'):
                        for item in formData.keys():
                                if item in audiclock.cptCode.keys() and formData['item'] == "on":
                                        audiclock.final_result(formData['lapp'], formData['lpatient'], item, formDatap['luser'])
                                        req.write('Item %s recorded.<br />' % (item))
                                else:
                                        req.write('%s did not match<br />' % (item))
                else:
                        req.write('formData was missing either luser, lapp, or lpatient<br />')
        else:
                req.write("didn't find 'currentPage' or value wasn't 'process_me'<br />")

Main program's import function (the first one called by audiclock.read_all() ) The filename being passed to this function is any of what I have tried above (e.g. /var/www/data/cptcodes2007)
:

def read_cptfile(filename):
        '''This function will import cpt codes into cptBase var.'''
        if not os.path.isfile(filename):
                raise IOError, "No cpt data found.  Please ensure that the  path for file (%s) is correct" % (filename)

        else:
                try:
                        try:                        # Open the data file and import it
                                inFile = open(filename, "r")
                                for line in inFile:
                                        if line == "": break
                                        if not re.match(line, '^#') and not re.match(line, '^$'):
                                                line = line.strip()
                                                cptCode[line.split(':')[0]],cptDescription[line.split(':')[0]],physicianRVU[line.split(':')[0]],nonFacilityRVU[line.split(':')[0]],malpracticeRVU[line.split(':')[0]],nonFacilityRVU[line.split(':')[0]],cptFee[line.split(':')[0]] = line.split(':')[:]
                        except IOError:            # If  file can't be read, we have a problem
                                print "File '%s' could not be read.  Check file permissions." % (filename)
                                sys.exit()
                finally:
                        inFile.close()


Arevos Jan 15th, 2007 1:22 PM

It may be a permissions problem. By default, Apache executes using a low-privilege user (like 'nobody'), so Apache (and by extension your Python program), may not be able to get access to your files or directories. What permissions have you set up on them?

Other than that, perhaps it's a problem with mod_python. Try experimenting with different file operations and see what happens, and what exceptions they raise.

somehollis Jan 15th, 2007 1:36 PM

For permissions, /var/www and everything inside is owned by nobody / nogroup. Directories are set up as 775 and all files are 664. I'll try some other operations and report what happens.

somehollis Jan 15th, 2007 3:27 PM

Making progress: In apache 2, the owner of the files has to be www-data (nobody is for apache 1 - my fault for not giving versions of everything). So I can now perform write operations on the dirs. Still can't seem to read the files, but I'm betting it's a configuration error that I've made along the way. I'll post the solution when I find it. Thanks for pointing me in the right direction!

Arevos Jan 15th, 2007 9:12 PM

Does the apache error log have anything useful to say?

somehollis Jan 17th, 2007 10:50 PM

Well, I found a solution, though I'm not sure I understand it yet.

For some reason, apache doesn't like it when those files are inside my webdir (/var/www). When I moved them outside to their own location (/data at the moment), it works just fine.

Now to figure out how to use mod_python's publisher handler and I should have a usable program.


All times are GMT -5. The time now is 1:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC