I am trying to run this script on my server.
#!/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>'
I have that code in a python file. I uploaded the file to cgi-bin folder. I then set permission's so every box was ticked (Full Permission). I then attempted to view the page. But I get the error below.
500: Internal server error
This error is generated when a script running on the server could not be implemented or permissions are incorrectly assigned for files or directories
Troubleshooting suggestions:
Temporarily disable any rewrite rules by renaming your .htaccess file if it exists.
Ensure that any CGI or Perl scripts have at least .755. permissions.
If trying to run PHP and you get this error, you may have an invalid php.ini in your /cgi-bin, or may be missing your php.dat file in this folder.
How can I fix this so I can start writing scripts an get on with my website?
All the code does is "Hello World", the script work on other servers. What has a php.dat got to do with me an my python. How am I meant to understand a PHP file when it's half in machine language.... E.g.
Ü™ ì™ ð™ ô™ ø™ ü™ š
š
PHP is irrelevant in this case. It's mentioned in case you are using PHP, but you are not, so you can ignore it.
There are so many possible causes of this error so start at the beginning and work your way through.
First thing to check is the server error log files, if you have access to them they will likely reveal the problem straight away.
Next check whether your server is properly configured to run .py scripts as cgi. Also, check the cgi-bin directory permissions.
Verify that the python binary is located at /usr/bin/python on the server. BTW, a better alternative "shebang" is:
#!/usr/bin/env python
because it is less sensitive to path.
Errors in the code can also cause http 500. Your code works fine for Python 2. It's unlikely, but the server might be using Python 3?
From the other question you say that you are using Python 3.4. The script that you show will run in Python 2, but not in Python 3 because the syntax of the print statement has changed - it is now a function.
To fix the script so that it will run in Python 3, change all of your print statements into functions:
#!/usr/bin/env 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>')
I have also changed the "shebang" at the top of the script to run whichever python is on the path.
If this doesn't fix the problem, look at the suggestions that I made in my previous answer, i.e. look in the server logs for errors, check your file and directory permissions, make sure that you are running the correct Python version etc.
Finally, talk to your hosting provider - I am sure that they can advise you how to set it up properly and there probably will be instructions on their site - find them and follow them.
Related
I am new to both Python and Linux and as such request simple explanations with minimal assumed knowledge where possible please, however I am more than willing to invest time and effort to learn.
I have a Raspberry Pi 2 (Model B V1.1) that is running Linux. I interact with this pi via putty.
I am trying to create a simple competitive reflex game, consisting of 2 buttons and a single LED. My goal is to have the LED light up after a short interval, and the first player to press their button wins.
I am writing the script for this with python (specifically 2.7.3)
My issue is that i am unable to run ANY .py file from within putty, i always receive the same error:
Syntax error: word unexpected (expecting ")")
To determine if the issue was an error in my code, i created a very very simple .py file, to check if the same error occurs, and it did. So i currently believe even if my code was functional, something is stopping me from running ANY .py file.
The process I am using is as follows:
First I create a new python file from within putty:
sudo nano test.py
Next I enter my python code (very simple for now, as i cannot get ANY .py file to run)
for each in range(5):
print 'hello'
I then press CTRL + O to write the file, hit enter, then CTRL + X to exit
Finally, I make the file executable using
sudo chmod u+x test.py
and try to run it
sudo ./test.py
again, a similar error occurs
Syntax error: "(" unexpected
I then decided to enter the code directly into the python shell, using
sudo python
>>>for each in range(5):
... print 'hello'
This time the output is the desired outcome:
hello
hello
hello
hello
hello
So there is no problem in executing python code directly from the shell, I am just unable to execute any previously saved .py file
Any insight into what could be causing this is much appreciated, and I apologise if I have not provided enough information to be useful for you.
Thanks in advance!
Short answer: Either run these as python filename.py, or else add the line #!/usr/bin/python to the top of your Python scripts.
Long answer: When you run a file from the command line in Linux (which is what the Raspberry Pi is running), by default it assumes that the file is a shell script file (usually Bash script). So it uses the Bash shell (or some other shell, but it's usually Bash) to interpret the file, and Bash doesn't know Python syntax. If you want to run your file using a different interpreter (Python, in this case), you have to add a "magic line" at the top of the file starting with #! (usually pronounced "hash-bang", and sometimes pronounced "shebang" for short). Following the #! characters is the full path of the interpreter to use, e.g. /usr/bin/python for Python scripts. (You can also use /usr/bin/env python as another answer suggested; I prefer /usr/bin/python because it's impossible to get the wrong Python interpreter that way. But that's getting into advanced topics that may be more than you need right now.)
So when you put the line #!/usr/bin/python at the top of your Python scripts, you're telling the Linux system which interpreter to run the program with, and then it should All Just Work™.
Also, STOP using sudo to edit and run these! That's just asking for trouble.
If you wish to execute like this you need the following line as the first line
#!/usr/bin/env python
This will tell bash (or equivalent) to execute the file with the Python interpreter.
If you don't want to do that then you can execute the script like this:
$ python test.py
If you go this route then you don't need to grant execute permissions on the script itself.
Also, scripts shouldn't be executed with sudo unless absolutely necessary.
I have two python installations, one at /usr/bin/env and one through Enthought at /Library/Enthought/User/bin/python. The enthought installation has a library that I would like to use in a website that I've inherited from a former coworker.
I set up a local server through apache on my Mac OSX 10.10.5 (Yosemite). I configured httpd.conf and my user configuration file inside of the website folder to execute cgi and py files.
However, when I try to run my coworker's python scripts through the server, it seems to use the installation without the package I need (/usr/bin/env). I tried changing the shebang at the top of the script from:
'#!/usr/bin/env python'
to
'#!/Library/Enthought/User/bin/ python'
but that gives me an internal service error (500).
One thing I was able to do was to create a symlink using this command:
'sudo ln -s /Library/Enthought/User/bin/python /usr/bin/python'
Now his script can run and has no trouble finding the right python package. However, I'd like to understand why changing the shebang isn't enough. Why does the server still access the /usr/bin/env python distribution?
A subquestion:
Lastly, I created my own test script in the meantime, but I get an Internal Service Error (500) no matter what the shebang is. This script is in the same folder as his script. Why can't this script run?:
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cgi
import cgitb; cgitb.enable() # for troubleshooting
print "Content-type: text/html"
print
print "<html><head>"
print ""
print "</head><body>"
print "Hello."
print "</body></html>"
Thanks! I hope this question was clear. This is my first experience with web servers, so please correct me if I made any terminology mistakes!
The shebang life you quote doesn't work because of the space at the end. /usr/bin/env is a program, and python is the argument, the name of the program it's supposed to look for. Remove the space from your new shebang and you will be running the Python interpreter directly. There's no need to provide it with an argument.
As to why the other script isn't working, I can only guess that you haven't made it executable, since I can't find any other reason for its failure (but this is a guess).
Your second print statement does not have an argument. Try changing it from print to print "\n".
EDIT
Try the following script with the appropriate shebang line prepended and, as holdenweb says, make sure it has execute permissions for the right user. Another issue I have run into in the past is with line terminations. If you have the "od" utility, run "od -c <filename>" on the command line to do an octal dump for your new file and the existing, working file. That way, you can see if the files end with the same line termination("\r\n" or "\n").
import cgi
print "HTTP/1.0 Status: 200 OK"
print "Content-type: text/html"
print ""
print "<html><head>"
print "</head><body>"
print "Hello."
print "</body></html>"
Note the addition of print "HTTP/1.0 Status: 200 OK". Other than that...I am out of suggestions.
I want to run a python script every so often on my web hosting, using cpanel, so it looks like…
However, every single formatting and file path I've tried returns with it saying no such file exists, when in fact I know for sure that it does.
Any help?
If it means anything, I'm running on shared hosting with namecheap.
Your Python script might be missing permission to execute, or missing shebang on the first line to be interpreted by Python.
Easy solution is to change:
somepath/check_crack.py
into:
python somepath/check_crack.py
or even to:
/usr/bin/python somepath/check_crack.py
This way cron will know, that it shall be interpreted by Python and run it.
I'm completely new to working with Python (I have a PHP background)
I'm used to PHP's Error Handling and Logging. Yet, with Python I'm getting a 500 Error on a very simple script that should work.
Is there a way to turn on Error Handling with Python? As 500 Error doesn't tell me much of anything, except that something is not right.
I have looked on the net for an answer to this, but I'm not finding a solution to what should be very obvious.
Your question is asking how to see errors or exceptions (not how to handle then, though of course you need to handle these errors as well), and from the 500 error and PHP background it seems to imply you are doing web programming with Python.
There are many ways to do so in Python, here is some that I tend to use:
in production use logging module to log errors. There are lots of tools that can help you such as Sentry [source code here]. You can read more on logging here.
in development, run your script with python -m pdb myscript.py which will start your script in debugger mode, then enter c (continue) to continue the script until error occurs, and now you will be able to see what the states are in the interactive PDB (Python debugger) prompt.
in development, if you are using a framework (or your script) that relies on WSGI, you can use the graphical, interactive JavaScript based in-browser helper Werkzeug which allows you debug at every level of the call stack.
And, to point out the most obvious one, Python interpreter will print out the stack trace if the program crashes, for eg:
→ python -c 'pprint "hello"'
File "<string>", line 1
pprint "hello"
^
SyntaxError: invalid syntax
# print.py is just one line: print 'hello world'
→ python print.py
File "print.py", line 1
pprint 'hello world'
^
SyntaxError: invalid syntax
UPDATE:
It seems like you aren't using any frameworks, and you are behind a host which from the look of it, didn't tell you how exactly it is serving your Python script. Now, since all you want is to see the stack trace in the browser, you can do the following based on what your hosts uses:
If your script is running behind a server via CGI, all you need to do is to use the Python cgitb module to print the stack trace on the HTML page:
import cgitb
cgitb.enable()
However, it is very likely the shared hosting you signed up is using mod_python with Apache, so turning on PythonDebug in the Apache config file should print the stack trace in the browser:
PythonDebug On
For Apache with mod_wsgi, there's a better article written than I could summarize here: mod_wsgi Debugging Techniques
I'm trying to run a basic Python CGI script on my Apache server. I have my hola.py script in my /cgi-bin/ directory, I changed the file's permissions to make it executable, and I changed the owner of the file to the apache user. However, I can't get the Python script to actually run.
I experimented by making a Perl script, and surprisingly it worked fine with the same steps I had followed to make the python script run.
I don't think the problem is in the scripts because I have added the #!/usr/bin/python line at the top of the source code and I've successfully executed it from the command line (that is, ./hola.py), in which case it worked just fine.
It's very strange to me that the Perl script worked where the Python script didn't. I've looked in several places for solutions to this, but haven't found one yet.
I'd be happy to post parts of my httpd.conf if anyone needs to see them.
EDIT:
Before you tell me to take a look at the error_log of the server, there is a line there -
(13) Permission denied: exec of '/var/www/cgi-bin/hola.py' failed