문제

I am running Python under WSGI on an Apache server using CentOS 6. The python script uses a wrapper for the NCAR library called PyNGL. The purpose of this library is to generate graphics from supplied data.

I am attempting to use my python script as a web service by hooking it up to web.py, but it has an entry point for direct execution as well.

Here is the weird thing:

When I run the script directly it works as intended and produces an output image in the directory of the script. However, when I attempt to invoke it through the web.py controller (with the exact same parameters) it fails.

My apache error log contains this:

warning:GKS:GCLRWK: -- cairo driver error: error opening output file

I'm guessing that this is probably a permissions problem, but I haven't the slightest idea where its trying to output.

Edit: I think I have confirmed that it is indeed a permissions error. I attempted to create file using relative paths and got a similar error:

<type 'exceptions.IOError'> at /plot
[Errno 13] Permission denied: 'Output.txt'

This error refers to this line here:

with open("Output.txt", "w") as text_file: 
    text_file.write(str(self.__dict__))

Now of course I can specify an absolute path for that text file, but not for the graphical output from PyNGL. Is there a way to determine where it is trying to output, or to change the default output directory?

도움이 되었습니까?

해결책

Usually your application would be running with the current working directory as '/'. The Apache user will not be able to write to that directory.

In any web application you should in general never rely on it being run in a specific directory as different web servers behave differently as to what the current working directory would be. If you assume it always runs in a specific directory, your application would be inherently unportable. Changing the working directory of an application to get around this is also in general bad practice because in a hosting mechanism that allows multiple application to run in the same process, they would all interfere with each other if they each tried to set their own working directory.

What you should do is always use absolute paths when reading and write files and not use relative paths. Why do you say you can't use absolute paths?

Also be aware that your application will run as a special user which would not have access to directories to create files. You would therefore need to open up access to the Apache user. Best practice would be to limit though what the Apache user can write to.

Now since you are using mod_wsgi, one viable option is to make sure you are using mod_wsgi daemon mode and when using the WSGIDaemonProcess directive set the 'home' option to override the current working directory for the single WSGI application delegated to that process. You can also set 'user' and 'group' options to have the process run as a different user that does have access to the directory.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top