OSError errno 13 Permission Denied Python CGI os.listdir - python

I am executing os.listdir() from Python CGI script. Script executes from console with no problem but when it is called from the web interface I get OSError errno 13 Permission Denied. It is denying permission to files in a different directory /usr/local/bin/ I checked permission levels and even changed owner and group to apache. I Even added Apache Directory configurations to allow all in ssl.conf file. Any thoughts? Thank you all in advance for the guidance.

Related

Why am I getting PermissionError [Errno 13] when attempting to write to log in Flask hosted by Apache?

I am running Flask 1.1.4 via Python 3.5.3, hosted via an Apache 2 server on Debian Stretch. I am attempting to log various messages from the program, using the python logging module. This works normally. However, if I restart the Apache server using sudo service apache2 restart, the Flask application errors out with PermissionError: [Errno 13] Permission denied: (log file name). Is this an issue anyone else has run into?
If it is not a permissions issue, this usually happens (happened to me a few times) if you are using a relative path for the log file.
The current working directory for an app running with mod_wsgi is not the directory the the app is in. If you are using a relative path for the log file you can try to use absolute path instead.
More info here: https://modwsgi.readthedocs.io/en/master/user-guides/application-issues.html#application-working-directory
It turns out that there was a cron process that was editing the ownership of all files in my log folder to root. Once the program was restarted and needed to re-obtain the file reference, it was unable to do so.

"Permission denied" Error in Django Application Deployed on Amazon Elastic Beanstalk

I have deployed a Django project (MyProj) to Amazon beanstalk using the eb CLI. This project is physically located at ~/MyProj. It has an app, named myapp, which needs to execute external python scripts. For this purpose, I have my_launcher.py in ~/MyProj/myapp directory. A code snippet in my_launcher.py is:
import os
import importlib
PROJECTS_DIR = os.path.join(os.path.expanduser(os.environ.get('LOCATION')), 'app/python/')
MY_MODULE = os.path.join(PROJECTS_DIR, 'MyModule/my_script.py')
spec = importlib.util.spec_from_file_location('my_script', My_MODULE)
MyModule = importlib.util.module_from_spec(spec)
spec.loader.exec_module(MyModule)
The reason for using the environment variable LOCATION is to dynamically set the location of external python scripts. I have defined this environment variable and some others through the AWS admin console under the Elastic Beanstalk section. In my example, I have:
LOCATION = '/home/ec2-user/company/'
This script fails at the last line with the error:
PermissionError at /myapp
[Errno 13] Permission denied: '/home/ec2-user/company/app/python/MyModule/my_script.py'
The things I have done so far to no avail:
After reading many online pages (e.g., Django [Errno 13] Permission denied: '/var/www/media/animals/user_uploads'), I thought the issue is Linux permissions on the location where the executable python scripts are (i.e., /home/ec2-user/company/app/python/). For the sake of testing this hypothesis (even though I know it is a bad approach for production), I ran the command sudo chmod -R 777 company while being in /home/ec2-user directory. I redeployed the project and even rebooted the EC2 instance all together. This didn't help.
I moved the company directory inside ~/MyProj/myapp (and updated the environment variable LOCATION on the server) thinking that the wsgi user does not have execution permission on any directory outside ~/MyProj/myapp. This didn't help.
I highly appreciate any help.
By the way, I am using python 3.6 and Django 2.1.7.

Write File Python

I have a python script I am running with a post request. The script is located in my cgi-bin and at the end of the script I am trying to upload a file to the /var/www/html/ folder and I am doing it like this
myFile= open("/var/www/html/file.html","w")
myFile.write("<html><body><p>test</p></body></html>")
myFile.close()
But I keep getting
<type 'exceptions.IOError'>: [Errno 13] Permission denied: '/var/www/html/file.html'
What is going wrong?
The error itself is already clear, your don't have enough permission to write to /var/www/html. It might be related the owner of the directory. If the owner is another user, then your current user don't have the write permission to the directory, the error would occur.

python os.makedirs Error 13 permission denied

I have a server set up on apache.
So when i run it and i upload a few files from a different machine. The server crashes here..
os.makedirs('data/%d' % stat.id)
The server crashes and it gives me this error
[Errno 13] Permission denied: 'data'
I have googled it and looked at other posts, I tried, but nothing works.
I have tried changing the group and chmod, chown..
Apache was making a directory under root.
I had to change the directory and it works.
os.chdir('...')
And when i create directory, its fine.

PYTHON Copy Permissions Elevated

I am trying to copy files from one user's home directory to another user's home directory in PYTHON. Problem is I get access denied due to the user permissions. Is there a way to elevate the permissions in linux for the PYTHON user?
I tried copying files using distutils.file_util.copy_file() and shutil.copyfile() but I get: [Errno 13] Permission denied: '/home/testuser/test.txt' any ideas?
There is no PYTHON user, your Python process will be run with the permissions of the user who executed the script. If you need to run your script with different permissions use sudo to run the script as root or some other user that has read permission on the source directory and write permission on the destination.

Categories

Resources