简体   繁体   中英

Unable to write to file programmatically on linux server

I have a python CGI script which takes form input x and y (integers) and passes it to a C++ executable using subprocess in which the program writes the sum of the two values to a text file.

The code works fine on my local machine. However, after much testing ,I found that whenever I run this program on my server (in /var/www ) and attempt to write the file some kind of error occurs because I get the "Internal Server Error" page.

The server is not mine and so I do not actually have sudo privileges. But from putty, I can manually run the executable, and it indeed writes the file.

My guess is I have to somehow run the executable from the python script with some amount of permission, but I'm not sure. I would appreciate any suggestions!

EDIT:
@gnibbler: Interesting, I was able to write the file to the /tmp directory with no problem.

So I think your suggestions are correct and the server simply won't allow me to write when calling the script from the browser.

Also, I cannot access the directory with the error logs, so I will have try to get permission for that.

Either the environment is different (maybe it's trying to write to the wrong dir) or more likely, the cgi isn't running as the same user that you are logging in as.

For example, it's fairly common for cgi scripts to be executed as "nobody" or "www" etc.

You could try getting the cgi to write a file into /tmp . That should at least confirm the user the cgi is running as

You are probably running into permissions issues, as others have said. But you can't tell for sure what's happening unless you look at the error log—internal server errors typically mask the actual error.

To figure out what the actual error is, you can do one of two things. You said you're doing CGI, so I recommend looking into cgitb while you're testing; it'll show you Python tracebacks right in your browser (though that may not help in your subprocess scenario.)

Otherwise, you'll want to look at the web server's error log, which can contain the Python traceback, and hopefully show you what's going on. A typical place where these are kept is /var/log/httpd . If you don't have access, or can't find logs there, ask your server administrator where you can find them.

Try chmod 0666 filethatyouarewritingto.txt If that doesn't work then make sure your c++ file has executable permissions in the group 'others', try chmod 0755 yourprogram .

Source

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM