Serving up Python CGI files on a Synology NAS - python

I cannot get python files to be served up with Apache 2.2 or 2.4 without a 500 error. I have WebStation installed, python, perl, php, and Apache 2.2 and 2.4 installed.
I can serve up static files just fine with apache. When I try to serve up a most basic "hello world" cgi, I get a 500 error. The error is
[cgid:error] [pid 10076:tid 140542621480832] (2)No such file or directory: AH01241: exec of ['/volume2/Development/WebRepo/cgi-bin/test.py' failed.
Tried to execute both a perl script and a python script. Both run successfully from a command line, but not from served up with Apache (same errors of "no such file..") Also note this is a 500 error, not a 404, so it's seeing the file. I can serve up static HTML files just fine.
The python script couldn't be simpler:
#!/usr/bin/python
print "Content-type: text/html\n\n";
print "Hello, World.";
All files have 755 permissions. The path to python is correct. I'm at a loss as to what to do next.

Python can serve CGI scripts out of the box, using http.server.CGIHTTPRequestHandler.
On my Synology NAS I have official Python3 package installed (version 3.8.2-0150).
I can SSH into NAS as admin and add a script:
mkdir -p app/cgi-bin
cat << EOF > app/cgi-bin/hello.py
#!/usr/bin/env python3
print('Content-Type: text/html')
print()
print('<html><body><h2>Hello World!</h2></body></html>')
EOF
After that I can run it like this (note that --directory doesn't have effect for --cgi so I cd there):
cd app && python3 -m http.server --cgi
Then on my machine, I can curl http://nas:8000/cgi-bin/hello.py.
Running on boot
You can run this automatically on boot via the task scheduler.
Control Panel → Task Scheduler → Create → Triggered Task → User-defined script. Fill these on General tab:
Task: Python CGI
User: admin
Enabled: [x]
And User-defined script on Task Settings tab:
cd /var/services/homes/admin/app
python3 -m http.server --cgi
Then you can run it manually. It should also run on reboot.
Permissions
If you want to run the task as root, make sure file permissions are correct from root's point of view. In my case there's discrepancy by some reason.
$ ls -l app/cgi-bin/hello.py
-rwxrwxrwx+ 1 admin users 122 Nov 29 14:50 app/cgi-bin/hello.py
$ sudo ls -l app/cgi-bin/hello.py
Password:
-rwx--x--x+ 1 admin users 122 Nov 29 14:50 app/cgi-bin/hello.py

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.

one python3 script shoud start a second python3 script

i´ve a few problems with my python3 scripts.
an php script start an python3 script:
$comando = 'python3 /var/www/html/tmp/' . $usersession . '-newtenent-vcenter1.py';
shell_exec("/usr/bin/nohup ".$comando." >/dev/null 2>&1 &");
the python3 script write a few strings to an new created text file.
After all thinks are done, the script sould be start the next python3 script:
os.system('python3 /var/www/html/tmp/' + usersession + '-newtenent-cucm1.py')
BUT, python3 start the script "cucm1.py" and close it imeadlety! The script shoud be open an ssh session with paramiko.
The OS is an Ubuntu 18.x. I´ve added the www-data user to the script directory (so all scripts can be executed by the user www-data):
www-data ALL=(ALL) NOPASSWD: /usr/bin/python3 /var/www/html/
BUT, when we execute the first python3 script from the linux shell (as root) it work´s fine (the second script working fine).
any idea? THANK YOU!

How to properly run virtualenv via .sh run script (django)

I am having an issue via an apache Nearly Free Speech server (I have been following the NFS guide via this link: https://blog.nearlyfreespeech.net/2014/11/17/how-to-django-on-nearlyfreespeech-net/. I have created a run-django.sh file to properly run the application which also opens a virtualenv (I have named the folder 'venv'). These are the contents of my run-django.sh file:
#!/bin/sh
. venv/bin/activate
exec python3 manage.py runserver
On the current step of the guide I am attempting to run the run-django.sh as follows:
[questionanswer /home/protected]$ ls
question_answer run-django.sh venv
[questionanswer /home/protected]$ cd question_answer/
[questionanswer /home/protected/question_answer]$ ../run-django.sh
.: cannot open bin/activate: No such file or directory
How is this not detecting my directory of 'venv/bin/activate' ?

Python CGI script running from web URL creates directory with www-data user

I am trying to run Python CGI script. The issue I am facing is, I need to create a directory and clone repo from git with my username.
#!/usr/bin/python2.7
import commands, os
print "Content-type: text/html\n"
print "\n\n"
print "<html><body>"
commands.getoutput("rm -rf fresh-cloned")
commands.getoutput("mkdir fresh-cloned")
os.chdir("fresh-cloned")
print commands.getoutput("pwd")
commands.getoutput("git clone <> -b <>")
But somehow, when I run the script from web URL : "http://ip_adr/webtest/webgui.py", I see the directory being created with www-data user rather than with my username. Due to that git clone won't work as ssh key is added in Stash for my username only.
drwxr-xr-x 2 www-data www-data 4.0K Nov 28 16:59 fresh-cloned
How can I resolve this issue? Is it possible to change the user to my username while running script from web URL?
If you want your script ran as your user from Apache HTTPD you need to load mod_suexec and set the directive SuexecUserGroup accordingly, such as:
SuexecUserGroup youruser yourgroup
I was able to resolve the issue by changing following line in "/etc/passwd"
www-data:x:33:33:www-data:/var/www:/bin/bash
I created/updated user, gave required permissions, created ssh key and added in stash, it is working now. Thanks!

How to host python cgi script with `python -m SimpleHTTPServer 8000` or `python -m CGIHTTPServer 8000`?

When I run python -m SimpleHTTPServer 8000 or python -m CGIHTTPServer 8000 in my shell I am hosting the content of my current directory to the internet.
I would like to make the following cgi_script.py work correctly using the above command in the command line when I browse to 192.xxx.x.xx:8000/cgi_script.py
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """\
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
"""
But this script is displayed literally and not only the "Hello World!" part.
Btw I changed the file permissions to 755 for cgi_script.py as well as the folder I am hosting it from.
Try with python -m CGIHTTPServer 8000.
Note that you have to move the script to a cgi-bin or htbin directory in order to be runnable.
SO doesn't allow me to comment so I'm adding this as a separate answer, addition to rodrigo's.
You can use another parameter cgi_directories which defaults to ['/cgi-bin', '/htbin']. More info here
In Python3 the command line is simply
python3 -m http.server --cgi 8000
When I ran into this issue I found that depending on which directory you are in when you run the python -m CGIHTTPServer 8000 command yields different results. When attempting to run the command while in the cgi-bin directory the browser continued to return the raw script code. once I cd'ed one level higher and ran the python -m CGIHTTPServer 8000 command again my script began executing.
#Bentley4 -ifyou are still not able to do,
try importing cgi.
#!C:\Python34\python.exe -u
import cgi
print ("Content-type:text/html")
HTH
This work for me, run the python -m CGIHTTPServer 8000 command same menu level with cgi-bin,and move cgi_script.py into cgi-bin folder.In browser type http://localhost:8000/cgi-bin/cgi_script.py

Categories

Resources