pysftp can't create log file - permission denied - python

I have a small service written in Python 3 which uses pysftp:
with pysftp.Connection(
host=host,
username=connection_data["user"],
port=connection_data["port"], log=log_file, cnopts=cnopts
) as srv:
…
and when I run it (python3 pythonprog.py) I get the following error:
PermissionError: [Errno 13] Permission denied: '/mydisk/folder/logs/pysftp-20181127-231208.log'
Obviously, I don't get this error if I run it with sudo python3 pythonprog.py.
I checked the permissions for this folder:
ls -l
drwxrwxrwx+ 2 myuser myuser 4096 Nov 27 22:38 logs
I also changed ACL with setfacl. Basically, whatever I do the error is still there. How can I grant this permission?

The service is running as someone -- that someone needs permission to write to that file or folder. This is not a code problem, its a permission problem. sudo is not the solution to running the code. I'm taking it that it is running as you? Can you write to that file/folder?

Related

xmlrpc.py permission denied whole running the supervisor

I need to run the laravel jobs in the background and for that reason, I need to run the supervisor on the Linux server.Whenever I try this
supervisorctl reread
It returns this error.
error: <class 'PermissionError'>, [Errno 13] Permission denied: file: /usr/lib/python3/dist-packages/supervisor/xmlrpc.py line: 560
I tried to change the permission of this file
chmod u+X /usr/lib/python3/dist-packages/supervisor/xmlrpc.py
It returns this error
chmod: changing permissions of '/usr/lib/python3/dist-packages/supervisor/xmlrpc.py': Operation not permitted
Any reason why it is not working? I recently upgraded the PHP to 8.1. Is this why it causing the problem?
I used to have the same problem as you, I used sudo at the beginning of the command and it worked fine
sudo supervisorctl reread

python3 [Errno 13] Permission denied

I have a Laravel app running on ubuntu 22.0.4. Inside one of my controllers I try to run a python script using shell_exec():
$output = shell_exec("python3 /home/ubuntu/test.py 2>&1");
Log::debug($output);
But the script is not executed and inside my log file I get:
"python3: can't open file '/home/ubuntu/test.py': [Errno 13] Permission denied"
I have set the permission for test.py to 777, and I have tried:
sudo chown ubuntu:www-data test.py
and also,
sudo chown ubuntu:ubuntu test.py
But still the same error. I have other projects running on ubuntu 20.0.4 with the same Laravel structure and logic in which I can execute the same python script (test.py) without any problem. But, for this specific case which I am using ubuntu 22.0.4, I am facing this problem. Also, same as other projects, I used pip3 to install python3.

Server Error while saving panda dataframe to csv

part of my python script is as below
panda_dataframe.to_csv("myfile.csv")
The code is working fine in my Dev System.
I have deployed it in EC2 - Ubuntu. The above statement return Server Error (500)
I have also tried with
panda_dataframe.to_csv("/usr/share/myfile.csv")
But the same error.
Error.log has the below error.
PermissionError: [Errno 13] Permission denied: '/usr/share/myfile.csv'
I have tried with setting the below permisions
sudo setfacl -m u:www-data:rw /var/www/sitefolder/myfile.csv
sudo setfacl -m g:www-data:rw /var/www/sitefolder/myfile.csv
Guide me what is the permission I have to assign to write the dataframe into a csv file
Your working directory is /usr/share, and saving/deleting/copy/move actions in /user directory requires sudo privileges. so first check you user directory by executing echo $HOME. say the result is /home/amir. now save you data frame as:
panda_dataframe.to_csv("/home/amir/myfile.csv")

Chmod in Python - [Errno 1] Operation not permitted

I'm trying to modify an application in my /Applications folder on OS X. I need to get read and write permissions, so I currently have:
os.chmod(appDir, 0644)
where appDir is the directory of my app. However, whenever I run the program, I get the error:
OSError: [Errno 1] Operation not permitted: '/Applications/Simplenote.app'
I would try sudo, but how would you enter the password through Python? (this is a Python script by the way)
Any help is appreciated.
If you are trying to run the script from a web site, change the target folders/files to be owned by the web user, e.g. www-data. sudo chown www-data * -R That fixed it for me.

Fabric get( ) permission denied with use_sudo=True

With the fabric function:
def get_test():
get("/home/wagans/test.txt", "/wagans/test.txt", use_sudo=True)
I'm getting a 'Permission denied' error.
Full error:
Fatal error: get() encountered an exception while downloading '/home/wagans/test.txt'
Underlying exception:
Permission denied
Aborting.
Disconnecting from root##########... done.
get() encountered an exception while downloading '/home/wagans/test.txt'
Underlying exception:
Permission denied
I was connecting as a specific user, but tried connecting as root and still received the same result.
Output of "ls -l" on the remote_path are:
-rwxrwxrwx 1 root www-data 10 May 4 13:21 test.txt
Output for local path folder is:
drwxr-xr-x 9 user 306 3 May 17:56 wagans
The remote machine is Ubuntu 14.04, and local is OSX with fabric running in a virtualenv.
Can anyone help guide me to a solution?
Many thanks.
You're certainly trying to use the rights of sudo in a script launched from your simple-user-rights. So, you must launch the script using the sudo command.
I got a very similar error and found the solution:
"yourfile.py" [New File]
Fatal error: get() encountered an exception while
downloading '/home/youruser/foobar'
Underlying exception:
Operation not supported
Aborting.
The solution was not to store files in my home directory which has restricted permissions, you have to store and retrieve files in a open permissions directory like /tmp
Here is the code that works:
from fabric.operations import get
def fabric_ssh(ip_address, user, password, key_path, verbose=True):
return settings(
host_string=ip_address,
user=user,
key_filename=key_path,
parallel=False,
warn_only=False)
with fabric_ssh("10.0.0.3", "your_user",
"Yourpassword",
"yourkeypath",
True):
get("foobar","/tmp/")
Finally it prints a success message after downling:
[10.130.28.191] download: /tmp/foobar <- /home/youruser/foobar

Categories

Resources