Trying to understand installing a CGI script [duplicate] - python

This question already has answers here:
Why do people write #!/usr/bin/env python on the first line of a Python script?
(22 answers)
Closed 8 years ago.
http://www.tutorialspoint.com/python/python_cgi_programming.htm
#!/usr/bin/python
print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello Word - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello Word! This is my first CGI program</h2>'
print '</body>'
print '</html>'
What is the significance of #!/usr/bin/python
It says to save file as hello.py and save it to /var/www/cgi-bin directory but I don't have this directory, should I create one?
Before running your CGI program, make sure you have change mode of file using chmod 755 hello.py UNIX command to make file executable.
but where to (how to) execute this chmod command? should I include $chmod 755 "/location of hello(.py).../" in hello.py?

You have to include the #!/usr/bin/python line on *nix systems to tell the shell how to interpret this script and where your interpreter is in the system. This is called the shebang line.
The chmod 755 hello.py command is a unix shell command to change the permissionson of the file and make it executable. Doing this you can run the script without having to do python hello.py but instead ./hello.py .
From my understanding you are a windows user. In this case, you don't have to do all of this. Just save the file as hello.py and that's it.
This post on tutorialspoint.com implies that you already have an HTTP server running that supports cgi and has a default web directory of /var/www/

The line #!/usr/bin/python is a signal to the shell (the program that handles your keyboard input and runs commands for you) that this file is a script that can be handled by the program shown. It's called a 'shebang' or sometime 'hashbang'.
In this case the shebang tells your shel to run the program it finds at /usr/bin/python and submit this script to it. There's more here
chmod is a general command for setting permissions on files. You execute it from the command line like any other command. The numerical values for the permissions can be a bit cryptic, but they're made up of binary bits that indicate certain permissions. Changing a permission to 755 specifically indicates that the owner of the file can execute this as a program, rather than just treating it as a block of text. Personally I prefer the mor descriptive chmod u+x filename - add execute for the user.
You can find a fuller description of chmod here

Related

Run Python code without using python name.py and ./name [duplicate]

This question already has answers here:
how to run python script without typing 'python ...'
(5 answers)
Closed 2 years ago.
I have a basic problem where I don't know how to run a Python script from command line in Ubuntu without using python keyword. So, I put a shebang in my Python script so I could run it as nameofthescript from the command line, but I only could do it by using ./nameofthescript. I want to be able to run it by just typing the name of the script in the cmd. I searched and tried everything I could on the web, but none is working. Any help is appreciated. Below is a simple code I wrote to test it.
I already tried chmod +x this file. Also this file is saved with no extension.
#!/usr/bin/python
import sys
def main(argv):
print(argv)
print("Hello")
if __name__ == "__main__":
main(sys.argv[1:])
The problem is with your $PATH variable.
When you go to run a command (without the "./" in front of it) Ubuntu looks in all the folders listed in your $PATH variable. Your can see it by running:
echo $PATH
If Ubuntu doesn't see the command in any of those folders, it will say that it can't be found.
You can solve this problem by altering your $PATH variable in your profile. Go to your home directory and open the ".profile" file (note the period in front) and add the following to the end:
PATH = "/path/to/folder/with/file/:$PATH"
However, if it's a program you could see yourself using a lot in the future and your don't want to clutter up your $PATH, I'd recommend sticking the finished command in your "/usr/local/bin" folder instead. I find that folder gets used as an "odd sock drawer" of programs you create/compile yourself, so I usually end up putting my personal tools in there rather than modifying my $PATH.
That's how it's supposed to work, not only for Python scripts but for any executable. See: Why do you need ./ (dot-slash) before executable or script name to run it in bash?
Please Try this one
On unix systems, Python scripts can be made executable using the following process:
Add this line as the first line in the script:
#!/usr/bin/env python
At the unix command prompt, type the following to make myexe.py executable:
$ chmod +x myexe.py
Move myexe.py into your bin directory, and it will be runnable from anywhere.
$ cp myexe.py /usr/bin
OR
$ cp myexe.py /usr/local/bin
So myexe.py
#!/usr/bin/env python
print("Hello This is executable python script")
Now Go to terminal and type myexe.py
$ myexe.py
Hello This is excutable python script
If you want to run by double-clicking remove .py extention
source link
I have found a way to solve this. I still include the shebang #!/usr/bin/env python3.6 at the top of Python script. Then I would go to cd /etc->sudo nano bash.bashrc, and at the very last line, all I did was add a line (alias nameofscript = "./nameofscript"). From there I restarted my Ubuntu, and was able to run my Python script just by the name of the script. Thank you everyone for the help.

Can apache run a python script which is NOT executable?

I setup Ubuntu server 18.04 LTS, LAMP, and mod_mono (which appears to be working fine alongside PHP now by the way.) Got python working too; at first it gave an HTTP "Internal Server Error" message. sudo chmod +x myfile.py fixed this error and the code python generates is displayed fine. But any time the execute permission is removed from the file (such as by uploading a new version of the file), the execute bit is stripped and it breaks again.
A work-around was implemented with incrontab, where the cgi-bin folder was monitored for changes and any new writes caused chmod +x %f to be ran on them. This worked for awhile, then stopped, and seems a hokey solution at best. Perl, PHP, even ASPX do not need to be marked executable - only python.
Is there any way Apache can "run" python without the file marked as executable?
The reason PHP works, is because the interpreter is loaded into Apache. So Apache interprets the code.
For your Python, it runs as a CGI, so the interpreter is outside of Apache.
In your Python script, you probably have a #!/usr/bin/python first line (or something similar). This tells the script to run using this interpreter. This requires executable permission on the .py file, and allows you to call myfile.py directectly.
Instead run it like this: /usr/bin/python myfile.py. This way the interpreter is the executable, and it will run myfile.py as the code.
Examples
You want to run the py file "alone":
file.py
#!/usr/bin/python
print("Hello")
Running it:
./file.py
You want to run it via the python executable, like you want via Apache:
file.py
print("Hello")
Running it:
/usr/bin/python file.py
I don't think Apache is capable of serving executed python scripts without the execute bit set on the .py file.
But here is a work-around: simply leave that file marked executable, but import a second python file. That second file does not need to be marked executable.
myfile.py (marked as executable and read-only - use this with apache):
#!/usr/bin/python3
# enable debugging
# helper to run the other, non-executable file
# do not add .py to the import "filename"
import myfile2
myfile2.py (marked RW only, edit this file freely):
# this is the code which can change frequently
# and does not need to be marked executable...
print("Content-type: text/html\n\n")
print("<html><head><title>Python</title></head>")
print("<body>Hello, World!</body></html>")

Raspberry pi Python shebang with cgi server

I am trying to run a local CGI server on my raspberry pi to host a webpage with a single link, that link is to a CGI script which is supposed to trigger another script and then print HTML code to redirect back to the starting page (so that it doesn't hang)
in the servers root directory i have:
index.html
favicon.ico
Server.py
cgi-bin
my server is set up to use the cgi-bin folder for cgi-scripts.
the issue i am having is i cannot seem to make the scripts callable, is so instead of typing "python Server.py" i should be able to type "Server.py"
in order to do this i have tried multiple shebangs:
#!/usr/bin/env python
#!/usr/bin/python
and then called chmod a+x Server.py to mark it as executable, to no avail.
to clarify i am using:
python 2.7.3rc2
standard raspi linux distro "wheezy"
i read in some of the help docs that if the file has DOS style newlines it interferes with the shebang, so i have ensured that they are now MAC style newlines, this still did not work.
to test further i have made a simple python file which contains:
#!/usr/bin/python
print "Hello World!"
saved it as test.py, marked it as executable, and tried:
/test.py
from the command line and i get:
print: bad interpreter: No such file or directory
can someone please tell me where i'm going wrong?
Thanks
James
Try to remove windows line endings in the script. This made it work for me.
E.g. see How to convert Windows end of line in Unix end of line (CR/LF to LF)
For more possible causes of this problem have look at my answer here https://stackoverflow.com/a/65249192/1150303

Permission denied for Python script using Bash?

I am new to Ubuntu... I am trying to run my first simple python program "Hello World" ...
After running following commands in terminal
1. chmod +x filename.py
2. ./filename.py
terminal is showing following error "bash: ./filename.py: Permission denied"
what can I do for solve about problem?
Do you have the appropriate incantation at the top of your python file? e.g.,
#!/usr/bin/python (or alternatively #!/usr/bin/env python)
Just to clarify, chmod +x only makes a file executable, it doesn't run it.
And I'm assuming your script looks like nothing more complex than this:
#!/usr/bin/env python
print 'hello world'
Some possibilities:
What does it say if you type umask? chmod +x will only make a file executable for you if your umask doesn't block the user executable bit. A typical umask such as 0022 will not block the user execute bit, but a umask like 0122 can. (See the Description section of chmod(1) for more info.)
To execute a script such as a Python script, you also need read permission. Try chmod u+rx filename.py and execute the script again.
It's also remotely possible that whatever interpreter you've specified in the file with the "hashbang" line at the beginning of your file (e.g. #!/usr/bin/env python) isn't executable, although that in my experience yields a different error message.
I deal with the same problem on my new system.
It is the third time I tried to solve this, and your post is the first one appearing on google results. My post is late, but think that it will help another users with the same problem.
In my case, it was about partition table setup.
Check in your /etc/mtab file how python script is being stored. Check if there is a clause: noexec
noexec is a flag that forbid executing under the partition. By default, it is set with exec. But, sometimes, this kind of things happen.
Now, it is working fine here.

Auto executable python file without opening from terminal?

Sorry if this is on the wrong site ( maybe superuser ) but I'm trying to make my python.py file executable so I can click on it and it automatically does its thing, without me specifying it to open in the terminal by that default prompt, and I already have 'chmod +x' for its permissions.
Clarification:
I want to run it by clicking on it, not through the terminal ( I meant that when I said 'can click on it and it automatically does its thing ' )
Already have a shebang line
When I click it right now, it prompts me with do you want to open it in a text file, terminal - can I make it always default to opening in the terminal or is this just an oddball request?
On the first line in your python file, add this:
#!/usr/bin/env python
So if you have:
print "Hello World"
You should then have:
#!/usr/bin/env python
print "Hello World"
First, pick a file extension you want for files you want to have this behavior. pyw is probably a good choice.
Name your file that, and in your file browser associate that file type with python. In GNOME, you'd open its Properties window, go to the Open With tab, and enter python as a custom command.
Now here's the important part: That little dialog you've been getting asking you what you'd like to do with the file is because it is marked as executable. Remove the executable bit with chmod -x. Now when you double click it, it will simply be opened with the associated program.
Of course, if you want to run it from the command line, you'll now have to start it with python explicitly since it isn't marked executable. The shebang line doesn't matter anymore, but I'd leave it in anyway in case someone else marks it executable and expects it to work.
http://supervisord.org is better choice.
Have you placed this at the beginning of the file:
#!/usr/bin/python
?
As others have said, you need put the "shebang" at the start of the file, to say which interpreter to use to execute the file.
As mentioned in the above link, the most portable way is to use the env command (instead of a fixed path to python) - put this as the first line in the file:
#!/usr/bin/env python
The shell will look in $PATH for your python, rather than looking for /usr/local/bin/python then failing. This means it will work if Python is installed in a non-standard location.
For example:
$ cat example.py
print "Test"
$ file example.py # it is treated as an ASCII file
example.py: ASCII text
$ chmod +x example.py
$ ./example.py # when executed, it defaults to being executed as a shell script
./example.py: line 1: print: command not found
Now, if I add the "shebang" line...
$ cat example.py
#!/usr/bin/env python
print "Test"
$ file example.py # it is recognised as a Python script
example.py: a python script text executable
$ ./example.py # and executes correctly
Test
I have anaconda installed and
#!/usr/bin/env python
did not work for me, however:
#!/home/geoff/miniconda3/bin/python
did work. So, check which python your terminal normally uses to execute your .py files with
which python
in a terminal and use that as your shebang.

Categories