Python CGIHTTPServer crashes with "OSError: [Errno 13] Permission denied" - python

I am running the following command from my home directory:
python -m CGIHTTPServer
This runs the server, but when I try to access a script in the cgi-bin directory I get:
Traceback (most recent call last):
File "/usr/lib/python2.7/CGIHTTPServer.py", line 251, in run_cgi
os.execve(scriptfile, args, env)
OSError: [Errno 13] Permission denied
Running as root does not make a difference. The files seem to have all the right permissions:
student#bandersnatch:~$ ls -lhR
.:
total 12K
drwxr-xr-x 2 student student 4.0K Jun 13 18:38 cgi-bin
drwxr--r-- 2 student student 4.0K Jun 10 2004 kalpy
-rwxrwxrwx 1 student student 2.0K Jun 13 12:37 test.html
./cgi-bin:
total 8.0K
-rwxr-xr-x 1 student student 31 Jun 13 18:38 test.py
Edit: The content of test.py is:
#!/usr/bin/python
print "test"
The shebang is valid:
~$ which python
/usr/bin/python

Are you, by any chance, running the process as root?
If you use the source, you will see in CGIHTTPServer.py, just before calling execve:
try:
os.setuid(nobody)
except os.error:
pass
That is, it will run the CGI script as nobody, if it is able to change the UID, that is if it is root. If it is not root, this call will most likely fail, and pass on.
So my guess is that you are running the server as root, so the script is run as nobody, but this user doesn't have access to the script. Which is expected, as you say that it is in your home dir.
Two solutions that I can think of:
The recommended: do not run the server as root!
The workaround: copy the script to a directory where nobody can read it (/tmp for example).

Personally, unless there's some reason I'm unaware of, I'd recommend using subprocess.Popen instead of os.execve. I have run into Errno 13 before, trying to start a .app with Popen(['open execName.app']). I had to use Popen(['execName.app/Contents/MacOS/execName', 'arg1', 'arg2'...]) instead. Don't know if that helps, but give it a shot.

I ran into the same problem from ubuntu Linux.
Followed the solution by "Mike", with modification.
Instead doing chmod of the "/usr" which has several folders, change the permissions of the folder containing executable file that was denied. (you can check that server would run fine when loading a static html file in the same location, and shows error only when script is run).
cd /pathto/folder/with/deniedscript
sudo chmod -R 755 ./
Now the script has permission, so should run fine.
Note that -R gives the permission to all files in this folder(and subfolders if any).

When running on Windows the files run right out of the command prompts.
For Linux and Windows users that's not the case!
I get the following error:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/http/server.py", line 1158, in run_cgi os.execve(scriptfile, args, env) PermissionError: [Errno 13] Permission denied:
You'll need to the following to resolve these issues:
For Linux Users:
1) Ensure shebang is adjusted for Python 3 running on Linux and Mac OSX systems:
#!/usr/bin/env python3
2) Since the original executable files were written on windows they'll have hidden '\r' in the files that must be removed. Here are three possible ways:
a) In terminal command line type: tr -d ‘\r’ < input file name > output file name (just rename the output file a new name --> erase old file --> then rechange output filename back to original)
b) In terminal command line type: cat inputfile | col -b > outputfile (just rename the output file a new name --> erase old file --> then rechange output filename back to original)
c) Download dos2unix, then type in terminal command line: dos2unix input file name
3) Make file executable:
In terminal command line type:
a) chmod 755 filename
or
b) chmod +x filename
or
chmod a+x filename
For Mac OSX users it's almost the same:
Repeat step 1) from Linux
Repeat step 2) from Linux
For step 3 things change:
Based on the apache.org wiki page: https://wiki.apache.org/httpd/13PermissionDenied
It says that you have to make every executable from file location traversing all the away up to the /Users root directory.
You'll have to do the following.
3) In terminal command line:
a) type command: `cd /Users`
b) type command: `sudo chmod -R 755`
Now you can run your server .py file via:
sudo webserver.py
and the input file via normal:
python3 inputfile.py
Now you should be all good to go with no more permission errors! You can make necessary adjustments to shebang and command line if running python 2.

Related

How do I restore my Python cron jobs after my Mac OS upgrade?

I recently upgraded to Mac Big Sur and have noticed my Python 3.8 cron jobs have stopped working. Under my own account in a bash shell, I can run this without issues ...
davea$ cd /Users/davea/Documents/workspace/article_project; source ./venv/bin/activate; python3 manage.py check_duplicates
In my crontab, I had this set up, which used to work before the upgrade ...
*/5 * * * * /bin/bash -l -c 'cd /Users/davea/Documents/workspace/article_project; source ./venv/bin/activate; python manage.py check_duplicates >> /Users/davea/logs/record2.txt 2>&1'
However, after the upgrade, I'm noticing my command is never run and I see this issue in my log file
/Library/Frameworks/Python.framework/Versions/3.8/bin/python3: can't open file 'manage.py': [Errno 1] Operation not permitted
These are the permissions/groups on my "manage.py" file ...
davea$ ls -al manage.py
-rwxrwxr-x 1 davea staff 866 Apr 15 2019 manage.py
What else do I need to do to get my cron job to run again?
Turns out with the new Mac OS there is an extra level of permissions that need to be enabled. In System Preferences, under Security and Privacy, I clicked the Privacy tab, and then added "cron" to the "Full disk Access" list
then the cron jobs ran without the permissions error.
I think in this case, python3 is considered as "any user" and with the -rwxrwxr permission it only have right to read the file, try to run chmod 775 manage.py in your folder to add permission to manage.py to be executed by "any user" (rigths should be set to -rwxrwxr), hope it can help.
EDIT: technically, read permission should be enough for python to run a file, but I can't see another reason why this error would appear, and I would be interested if you find one
This looks like a SIP or other mac-specific access permissions error, especially since it was right after an upgrade. Could be: https://osxdaily.com/2018/10/09/fix-operation-not-permitted-terminal-error-macos/
I've also had lots of problems working with venvs with cron, could be related to this: https://stackoverflow.com/a/7031758/13113166
It's also weird that the error comes from /Library/Frameworks/Python.framework/Versions/3.8/bin/python3 when i think it should come from your venv if it's activated correctly.

PermissionError: [Errno 1] Operation not permitted: '/Users/<local_path>/venv/pyvenv.cfg'

I am running a Django application with a few cron tasks. Whenever I run python manage.py crontab add to register the cron tasks, I get a "mail" in the Terminal showing the following traceback:
Traceback (most recent call last):
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 609, in <module>
main()
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 592, in main
known_paths = venv(known_paths)
File "/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/lib/python3.7/site.py", line 510, in venv
with open(virtual_conf, encoding='utf-8') as f:
PermissionError: [Errno 1] Operation not permitted: '/Users/<local_path>/venv/pyvenv.cfg'
I am running on macOS Catalina, python 3.7.3 in a virtual environment venv.
I have tried giving full disk permission to bash, crontab, terminal, xcode and pycharm.
I have also tried installing python versions 3.7.8 and 3.8.3
Partially solved
I disabled System Integrity Protection using csrutil disable in Recovery Mode and the cron ran successfully. However, I do not see it as full solution.
Final solution
I dockerized the app and ran it as a docker container.
I was facing the same problem and after a few searching i found a error with cron permissions.
This is how i solved:
Go to System Preferences -> Security & Privacy - Privacy -> Full Disk
Access
Click on the + icon to add an item
If the lock appears locked, click on him to unlock with your user password
Press Command + Shift + G to open a dialog
Type /usr/sbin/cron and press Enter
Search and select the cron file and click Open
Okay, now it should work.
The reason for the PermissionError is that the Python virtual environment is placed in the user directory.
If you put the virtual environment in a system directory, such as /opt, there will be no problem. See example below:
$ sudo mkdir /opt/myvenv
$ cd /opt/myvenv
$ sudo python3 -m venv .venv
$ mkdir ~/workspaces/myapp
$ cd ~/workspaces/myapp
$ cat <<EOF> demo.py
> with open('/Users/username/workspaces/myapp/demo.txt', 'a+') as f:
> f.write('Hello, cron.\n')
> EOF
$ crontab -e
$ crontab -l
* * * * * /opt/myvenv/.venv/bin/python /Users/username/workspaces/myapp/demo.py >> /Users/username/workspaces/myapp/demo.log 2>&1
$ ls -al
total 24
drwxr-xr-x 5 username staff 160B 6 20 09:24 .
drwxr-xr-x 136 username staff 4.3K 6 20 09:18 ..
-rw-r--r-- 1 username staff 0B 6 20 09:23 demo.log
-rw-r--r-- 1 username staff 117B 6 20 09:23 demo.py
-rw-r--r-- 1 username staff 11B 6 20 09:24 demo.txt

Denied persmission to .py file on ev3dev, PuTTY

I have EV3 Lego Mindstorms and I instaled on it ev3dev operating system. I set the connection with the PC via SSH and using PuTTY I started to "programming". I used the cat > test2.py and wrote this code:
#!/usr/bin/env python3
import ev3dev.ev3 as ev3
motor = ev3.LargeMotor('outA')
motor.run_timed(time_sp = 1000, speed_sp = 500)
I saved the file and initialized it using ./test2.py. I got this output:
-bash: ./test2.py: Persmission denied
What caused it and what should I change?
try this:
sudo python3 test2.py
that will allows you to open almost anything in linux
Use ls -la ./test2.py in order to see the file permissions.
Look at the beginning of the output, you'll see something like this:
-rw-rw-r--
The first - means if is a directory or a file. In this case means that is a file.
Now If you observe the remaining chars there are 3 sets of 3 chars with means the permissions for the owner of the file, the owner group and the last set is for the rest of the users.
We have permissions to read, write and execute and in the example I showed there are read and write permissions for the owner user and the owner group but non permissions for the other users.
As Is said above you can just use sudo every time you execute the script but to run it with root privileges. However I would recommend you change your file permissions and using chmod
sudo chmod +x ./test2.py
This will let you execute the script. Take a look at chmod documentation to learn more: https://help.ubuntu.com/community/FilePermissions

where to save a file in cygwin

noobiest question ever:
I'm trying to work with python via cygwin on my pc laptop - I have a file (foo.py) but python can't find it. The error it's giving me is:
$ chmod +x foo.py
chmod: cannot access `foo.py': No such file or directory
Is there a special location within the Cygwin folder that I need to save my foo.py?
Thanks!
AP
It's not python that can't find your file, it's the chmod command. C drive is mapped to /cygdrive/c in Cygwin, and D drive is mapped to /cygdrive/d and so on and so forth.
Are you in the same directory as the file when you are running chmod?
If your file is at C:\mycode\python\foo.py then you should either change to that directory first -
$ cd c:
$ cd mycode/python/
or as #Ahmed mentioned above, you could also run the command as
$ chmod +x /cygdrive/c/mycode/python/foo.py
But you only need chmod if your python script starts with
#!/bin/python
To execute such a file, you'd say
$ /cygdrive/c/mycode/python/foo.py
Or if you are in the same directory
./foo.py
If the first line of the python script isn't "#!/bin/python" then you can skip the chmod and just type
python /cygdrive/c/mycode/python/foo.py
You go right click on foo.py figure out its full path then do this:
chmod +x foos-full-directory/foo.py
And this should work for you and btw its not Python problem it's your pwd other than the foo.py working directory and you even didn't use python word in your command.

Altering my python path: helloworld.py returns command not found—

Massive apologies for this embarrassing question—
I'm using my MacBook Pro, running snow leopard, and using Python 2.7.1. Trying to run my first script and all the first pages of all my tutorials are laughing at me:
Let me preface with:
$ whereis python
/usr/bin/python
$ which python
/Library/Frameworks/Python.framework/Versions/2.7/bin/python
(Is this my issue?)
I wrote helloworld.py to /users/charles in vim:
$ vim helloworld.py
#!/usr/bin/python
# Hello World Python Program
print "Hello World!";
When trying to run it from terminal:
$ helloworld.py
-bash: helloworld.py: command not found
When trying to run it from python:
$ python
>>> helloworld.py
Traceback (most recent call last):
File :<stdin>", line 1, in <module>
NameError: name 'helloworld' is not defined
From Dive Into Python (not sure if this is pertinent):
$ python
>>> import sys,os
>>> print 'sys.argv[0] =',sys.argv[0]
sys.argv[0]=
>>> pathname=os.path.dirname(sys.argv[0])
>>> print 'path=',pathname
path=
>>> print 'full path=',os.path.abspath(pathname)
full path= /Users/charles
I'm befuddled! Do I need to alter one of my paths so it finds my script?
I'm absolutely new to programming, I actually just found out that terminal was something you could use.
Thanks!
Let's start with the first error you received. Understanding error messages is important.
-bash: helloworld.py: command not found
This indicates that helloworld.py is not a command that can be executed. To run the file, you then have two options:
Run it using the python interpreter. python helloworld.py
Make the file executable and then run it directly. ./helloworld.py
To make files executable in a *nix environment, you have to change their mode to allow execution. In order to do this, you use the chmod command (man chmod for more info).
chmod +x helloworld.py
This assumes that you are in the directory containing the helloworld.py file. If not, cd there first or use the full path.
The ./ is necessary because it tells the shell to run the file located here, not by looking in $PATH. $PATH is a list of possible executable locations. When you try to run helloworld.py directly, the shell tries to look for it in $PATH. You want to run the local file, so you have to prefix it with ./, which means "from here".
As an aside, note the first line of your python script:
#!/usr/bin/python
This is called a shebang line and tells system to use the /usr/bin/python executable to load the file. Internally, that means that the program loader will be doing /user/bin/python helloworld.py.
Finally, when you called python with no arguments, you were dropped into an interactive Python interpreter session. >>> helloworld.py in this environment is not referencing the file of that name, it's just interpreted as python code. Invalid python code. This is why you get your second error, NameError: name 'helloworld' is not defined.
To turn a Python module or script into a standalone program on a UNIX system you have to do two things:
1.) Make sure you have the "shebang" in the top of your script:
#!/usr/bin/python
2.) Make sure the script file is executable. This is done using the chmod command:
chmod +x /path/to/helloworld.py
/path/to/ being the fully qualified file path to your script. If it's in the current directory, then you can omit the path.
% ls -l
total 0
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rw-r--r-- 1 jathan jathan 0 2011-04-13 15:28 helloworld.py
It's in my current directory, so let's make it executable!
% chmod +x helloworld.py
% ls -l
drwxr-xr-x 2 jathan jathan 60 2011-04-13 15:28 ./
drwxrwxrwt 12 root root 6.5K 2011-04-13 15:28 ../
-rwxr-xr-x 1 jathan jathan 0 2011-04-13 15:28 helloworld.py*
See the "x"s in the permission bits on the left? You've done it! Now we can run it:
% ./helloworld.py
Hello World!
Lastly, never use semicolons as line-endings in Python. It's not required and it's ugly!
Wanted to add my 2 cents: Apart from permissions and path answers above, there is one more situation where you may still face the same error.
In-spite of correct permissions and the shebang header, you may still get the same "Command not found" error if you've originally written the file in Windows and copied it over to Linux. Due to differing line-ending characters, there will be extra '\r' characters on the lines.
This happens because there are non-printable characters in the file. Examing it by doing:
cat -v <filename>:
#!/usr/intel/bin/python^M
The extra "^M" is the problem. Use 'dos2unix' to convert the file and then it'll run fine.
as others said you should chmod +x your file to make it executable and if you don't want to put "./" in your coomand line you should add your current place as system path:
export PATH=$PATH:.
If you're already within python, the syntax to load your script is not helloworld.py :
import helloworld
or
from helloworld import *
you only use the extension .py when you're running python with a script as a command line argument.
No need to apologize, have to start somewhere, and the error messages can be cryptic when you're having basic syntax problems.
Make sure your terminal's current working directory is where your .py file is.
EDITED:
try doing /usr/bin/python helloworld.py on commmand line

Categories

Resources