PYTHON Copy Permissions Elevated - python

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.

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.

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.

OSError errno 13 Permission Denied Python CGI os.listdir

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.

Changing default permission in Flask application with Apache web server

I have a flask application that creates directory with this code:
if not os.path.exists(target_path):
os.makedirs(target_path)
With the created directory the default permission is 0755, and the owner and group is _www.
So, only the owner can write in the directory.
I always have to modify the permission manually to 0775 to make some files in it.
How to make the default directory permission as 0775? I use apache for web server.
This has something to do with Apache setup.
For Mac:
Open /System/Library/LaunchDaemons/org.apache.httpd.plist
Add <key>Umask</key> and <integer>002</integer>
Restart with sudo apachectl restart
I found the solution from this site, for linux I guess Setting the umask of the Apache user can give some hints.
Change the code to
if not os.path.exists(target_path):
os.makedirs(target_path, mode=0775)

Permission to access network drives when running Python CGI?

I have a Python script running on the default OSX webserver, stored in /Library/WebServer/CGI-Executables. That script spits out a list of files on a network drive using os.listdir.
If I just execute this from the terminal, it works as expected, but when I try to access it through a browser (computer.local/cgi-bin/test.py), I get a permissions error:
<type 'exceptions.OSError'>: [Errno 13] Permission denied: '/Volumes/code/code/_sendrender/queue/waiting'
Is there any way to give the script permission to access the network when accessed via CGI?
I don't know much about the default osx webserver, but the webserver process is probably being run as some user, that user needs to be able to access those files. To find out who the user is you can use the ps command. Then depending on the configuration of the network shared drive, you can add this user to the users allowed to access this data.

Categories

Resources