Permission to access network drives when running Python CGI? - python

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.

Related

Accessing the Host File In A Python Program

I created a program which modifies the hosts file in order to block some websites. But when I am running the programme, I am getting this error.
Btw I have created my Python file using Pycharm IDE and my intention is to run this script everytime open my PC using Task Scheduler. So please kindly do tell me what I should be running as the administrator. Is it like the Pycharm itself? Most importantly how do I give it Admin permissions permanently?
[Errno 13] Permission denied: 'C:\\Windows\\System32\\drivers\\etc\\hosts'
Please kindly tell me a way to fix this.
On Windows create a scheduled task on logon, and configure the task to run under a system account. The task could simply be run as a batch file and within the batch file you may run the below command. Ensure the task is set to run with administrator privileges in the task creation window
C:\PythonFolder\python.exe yourscript.py

Create symlink on a network drive to a file on same network drive (Win10)

Problem statement:
I have a python 3.8.5 script running on Windows 10 that processes large files from multiple locations on a network drive and creates .png files containing graphs of the analyzed results. The graphs are all stored in a single destination folder on the same network drive. It looks something like this
Source files:
\\drive\src1\src1.txt
\\drive\src2\src2.txt
\\drive\src3\src3.txt
Output folder:
\\drive\dest\out1.png
\\drive\dest\out2.png
\\drive\dest\out3.png
Occasionally we need to replot the original source file and examine a portion of the data trace in detail. This involves hunting for the source file in the right folder. The source file names are longish alphanumerical strings so this process is tedious. In order to make it less tedious I would like to creaty symlinks to the orignal source files and save them side by side with the .png files. The output folder would then look like this
Output files:
\\drive\dest\out1.png
\\drive\dest\out1_src.txt
\\drive\dest\out2.png
\\drive\dest\out2_src.txt
\\drive\dest\out3.png
\\drive\dest\out3_src.txt
where \\drive\dest\out1_src.txt is a symlink to \\drive\src1\src1.txt, etc.
I am attempting to accomplish this via
os.symlink('//drive/dest/out1_src.txt', '//drive/src1/src1.txt')
However no matter what I try I get
PermissionError: [WinError 5] Access is denied
I have tried running the script from an elevated shell, enabling Developer Mode, and running
fsutil behavior set SymlinkEvaluation R2R:1
fsutil behavior set SymlinkEvaluation R2L:1
but nothing seems to work. There is absolutely no problem creating the symlinks on a local drive, e.g.,
os.symlink('C:/dest/out1_src.txt', '//drive/src1/src1.txt')
but that does not accomplish my goals. I have also tried creading links on the local drive per above then then copying them to the network location with
shutil.copy(src, dest, follow_symlinks = False)
and it fails with the same error message. Attempts to accomplish the same thing directly in the shell from an elevated shell also fail with the same "Access is denied" error message
mklink \\drive\dest\out1_src.txt \\drive\src1\src1.txt
It seems to be some type of a windows permission error. However when I run fsutil behavior query SymlinkEvaluation in the shell I get
Local to local symbolic links are enabled.
Local to remote symbolic links are enabled.
Remote to local symbolic links are enabled.
Remote to remote symbolic links are enabled.
Any idea how to resolve this? I have been googling for hours and according to everything I am reading it should work, except that it does not.
Open secpol.msc on PC where the newtork share is hosted, navigate to Local Policies - User Rights Assignment - Create symbolic links and add account you use to connect to the network share. You need to logoff from shared folder (Control Panel - All Control Panel Items - Credential Manager or maybe you have to reboot both computers) and try again.

python fabric: put() results in permission denied

I use fabric and have:
put('/projects/configuration-management/prototype','/etc/nginx/sites-available')
The result is:
Underlying exception:
Permission denied
Aborting.
Other configuration files can be uploaded easily. How could I avoid my issue?
It looks like you need super user permission, run it using sudo and it will work just fine
In the docs (link here) says:
While the SFTP protocol (which put uses) has no direct ability to
upload files to locations not owned by the connecting user, you may
specify use_sudo=True to work around this. When set, this setting
causes put to upload the local files to a temporary location on the
remote end (defaults to remote user’s $HOME; this may be overridden
via temp_dir), and then use sudo to move them to remote_path.

eb.exe deploy": Errno 13 Permission denied: './pagefile.sys'

I am compiling a website using TeamCity on a server and need to deploy the compiled website to AWS.
As my last build step, I use the Elastic Beanstalk CLI to deploy: "C:\Python34...\eb.exe deploy".
eb init has already been run...but whenever I run "eb deploy", (even when I run it from the command line in an empty directory--which should deploy a default project to AWS), an error appears saying:
Error: PermissionError :: [Errno 13] Permission denied: './pagefile.sys'
I have already run the command on my local machine without any problems; I receive the error on the server regardless of whether I am running the command line as an administrator.
I am wondering if this is a permissions issue with the server, or something else? I haven't been able to achieve much insight from the other questions, because they seem to have been solved on a case-by-case basis.
pagefile.sys is the Windows swap file.
It is a special file which cannot be written or manipulated. Whatever your command is doing you need to fix your command so it that doesn't touch this file and ignores it.

Python script to change resolv.conf [Permission issue]

Hey everybody, I am just newbie at Python. I wanted to write a script in Python to change DNS.
But I learned that resolv.conf is read-only file, after writing that code. Because I took that error: IOError: [Errno 13] Permission denied: '/etc/resolv.conf'
myFile= open("/etc/resolv.conf", "w")
Then, I made a little search and found os.chmode() and I wrote a new line to remove all privileges of resolv.conf which is:
os.chmod("/etc/resolv.conf", 0777)
But now I'm taking that error: IOError: [Errno 13] Permission denied: '/etc/resolv.conf'
I can't get over this question and I'm waiting for your advices.
Thank you.
/etc/resolv.conf is typically owned by root. Unless your script is run in such a way that it has root privileges, it won't be able to change the file.
Chmod you must run as root before your script. And when you get permissions, your script will run without errors
You should never allow for a file like resolv.conf to be writable by all. It looks like you were chmod'ing it, or trying to anyway, to 777. That's really bad. There is a lot someone could do by changing a resolver on a host and making that host point to systems that were setup for malicious reasons. For example, one could have their own LDAP server, and by changing resolv.conf point a system at their resolver, and at their LDAP server, thereby possibly gaining privileged levels of access.
Keep this file locked down at all times.

Categories

Resources