So basically, I’m a bit new to Python. I created a server and have it up and running just fine, have the files and everything showing up.
But the problem is, every time I try to open a file, in Python, for reading/writing, my terminal throws an “access denied” error.
You know, it’s a basic server:
#!/usr/bin/env python
import BaseHTTPServer
import CGIHTTPServer
import cgitb
cgitb.enable();
server = BaseHTTPServer.HTTPServer;
handler = CGIHTTPServer.CGIHTTPRequestHandler;
address = ("",80);
handler.cgi_directories = ["/home"];
httpd = server(address,handler);
print("Working . . . ");
httpd.serve_forever();
Nothing too fancy. So I start the server up, and then when the user navigates to the page, “/home/file.py” that contains the code:
#!/usr/bin/env python
f = open("asdf.txt","w");
Nothing at all happens, and when I check the terminal that’s running the server, it says,
localhost - - [27/Aug/2012 17:58:18] "GET /home/file.py HTTP/1.1" 200 -
Traceback (most recent call last):
File "/home/SERVER/home/file.py", line 4, in <module>
f = open("asdf.txt","w");
IOError: [Errno 13] Permission denied: 'asdf.txt'
I’ve been looking up this for awhile now, but I’ve yet to find a solution. It’s probably a very simple solution that would make professional Python users facepalm, but take into account that all self-taught programmers have to start somewhere. And for me, I’m a bit at the bottom right now, in Python terms at least.
I have tried a couple solutions I’ve found from the internet (and from the “Questions that may already have your answer”), like importing “os” and using that to specify the exact location to create file. I’ve tried using “sudo chmod g+w” on the SERVER folder and the folder that contains the Python scripts. But none of these interwebs solutions are helping me.
I’ll also remember to accept the answer.
I tried to make the question as precise as I could. So if there’s anything else you’d need to know, I can just update it.
Since it’s a “permission denied” error, I’m doubting it’s Python’s fault, and probably some permissions I didn’t setup somewhere. But I don’t know which or where they’d be.
If you read the python docs on CGIHTTPServer:
So this seems a little tricky unless you’re willing to leave all best practices behind and let
anyone do stuff in your folder.