Error while running python with xammp server - python

I am new in running python with xammp server. I have done all the required setup to run python in xammp server by following this link Running Python scripts with Xampp. While trying to running my code i always get an error.
**Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator at postmaster#localhost to
inform them of the time this error occurred, and the actions you
performed just before this error.
More information about this error may be available in the server error
log.**
My error log says this:
[Tue Jan 19 10:29:34.529875 2021] [cgi:error] [pid 11652:tid 1860] [client ::1:50570] malformed header from script 'python.py': Bad header: Hello World
i am attaching the code for my file too.
#! C:\Users\"UserName"\AppData\Local\Programs\Python\Python39\python.exe
print("Content-Type: text/html\n\n")
import cgi
print("Hello Python")
i have tried everything i could to search for the solution but nothing is helping. Please help me finding a solution. Thank you

Update the \n\n to use a carriage return (\r\n\r\n), and add another in.
This should make your HTTP headers valid, as they may not be terminating correctly.
Edit: You can also try multi-line print instructions such as
print "Content-type: text/html"
print ""
This is interpreting the body as the header (never terminated) so terminating this correctly should fix it.

Related

How to solve internal error 500 for python on Xampp

I am getting this error:
Server error!
The server encountered an internal error and was unable to complete your request.
Error message: End of script output before headers: python.py
If you think this is a server error, please contact the webmaster.
Error 500
192.168.64.2 Apache/2.4.41 (Unix) OpenSSL/1.1.1d PHP/7.4.1 mod_perl/2.0.8-dev Perl/v5.16.3
I have done the following:
Added .py to Addhandler in httpd.conf
I have also installed pip and pymysql.
I am using python 3.8.3
I have also have the permissions set to chmod 755
have also tried chmod 755, +x but no luck I have also checked the error logs but no clear problem
In the error logs it says: End of script output before headers: python.py
and
(2)No such file or directory: exec of '/opt/lampp/htdocs/11-python/python.py' failed: /opt/lampp/htdocs/11-python/python.py
btw I am using Mac
Heres my code:
#!/usr/bin/local/env python3
print ('Content-type: text/html\r\n')
print ('\r\n')
print ()
print ('<html>')
print ('<head>')
print ('</head>')
print ('<body>')
print ('Hello Python')
print ('</body>')
print ('</html>')

file sourcing within cgi failed

My cgi file:
#!/bin/sh -f
source /Users/sfma/sastbx/build/setpaths_all.sh
python processQuery.py
Then "Internal Server Error" arises. I checked the error_log, and it says:
[Thu Jun 23 15:02:39.809441 2016] [cgi:error] [pid 1327] [client ::1:52131] AH01215: /Users/sfma/sastbx/build/bin/libtbx.path_utility: line 63: /Users/sfma/sastbx/build/../../Library/Enthought/Canopy_64bit/User/bin/python: Permission denied: /Library/WebServer/CGI-Executables/web.cgi, referer: http://localhost/modelRetrieval/query.html
Remove the source statement, it works fine. But I need to source this file for further use.
Edit
According to the error message, I think the error may be due to the python permission.
#!/bin/sh -f
#python processQuery.py #this works fine
/Users/sfma/Library/Enthought/Canopy_64bit/User/bin/python processQuery.py #this does not work. Permission denied.
This really puzzles me because /Users/sfma/Library/Enthought/Canopy_64bit/User/bin/python is exactly the python I'm using:
$which python
/Users/sfma/Library/Enthought/Canopy_64bit/User/bin/python
I finally solved this problem.
Since python works fine while /Users/sfma/Library/Enthought/canopy_64bit/User/bin/python leads to a problem, I hard coded some variables in files libtbx.path_utility and sastbx.python to "python" instead of what they used to be: "/Users/sfma/sastbx/build/../../Library/Enthought/canopy_64bit/User/bin/python"
Update
The reason why /Users/sfma/Library/Enthought/Canopy_64bit/User/bin/python is permission denied is that _www is now allowed to visit my Library folder. Check this. After making that directory available to all, this problem is finally solved.

Permission Denied when Calling Shell Script from a Python Subprocess in Browser

I am calling a python script through an ajax call
$.ajax({
url: "http://localhost/username/test.py",
type: "post",
data: JSON.stringify({'param':{"hello":"world"}}),
dataType: "json",
success: function(response) {
console.log(response);
},
error: function(error) {
console.log(error);
}
});
The python script kicks off (have set up the conf files and have an apache server running) but then the python script is supposed to loop through a bunch of subprocess calls calling various shell files. Something like this:
for line in reader:
var=line["A"]
var2=var[:-4]
var3=line["B"]
print(var2)
subprocess.call(["sh","dummy.sh",var,var2,var3])
The same python scripts works fine in terminal. But when I run through the web browser, I get permission a 500 error in the browser. Going into the apache logs, shows me its running into a permission error
[Thu Jul 23 18:13:55.958161 2015] [cgi:error] [pid 19878] [client ::1:54132] AH01215: dummy.sh: line 37: inputFile.xml: Permission denied, referer: http://localhost/username/
How do I pass permissions? or am I doing something completely wrong here?
When you run the python script at the terminal, it runs as a process with your user ID, I assume?
If you can open the input file by doing this, then it is most likely a permission issue.
The apache process just isn't privileged enough to open the file.
I'd suggest:
Check the owner and permission of the file - make sure it is readable by the Apache process userID. This requires the file to at least have read permission, ie. -rw-r--r-- (chmod 644).
Also don't forget the directory path permission and/or ownership.
eg. if the file is in /home/foo/bar/inputFile.xml, you need to check that the whole path /home/foo/bar is accessible by the Apache user. This requires at least drwx--x--x (chmod 711) on the directories.
Thanks Maelstrom, Revolt and rici. inputFile.xml is actually created within dummy.sh and then other steps within dummy.sh operate on inputFile.xml. I also anticipate that since I have already given other files in the folder 755 access, its these new formed xml files that are restricted. How do I ensure that all files created by dummy.sh have enough permissions that they can be executed upon? Thanks a lot.

Error sending email by calling a bash script

My sendmail.sh script on Raspbian OS is able to successfully send an email. But when it's called from a python script, I get a "mail: can not send message: process exited with non zero status" error message. I have verified that ssmtp is configured correctly by running sendmail.sh by hand.
sendmail.sh
#!/bin/bash
echo "test" | mail -s "test msg" myemailaddress
permissions on sendmail.sh are 777. sendmail.sh and sendmail.py are in the same directory.
sendmail.py
import os
import subprocess
subprocess.call(['./sendmail.sh'])
The command I use to run the python - sudo python sendmail.py.
I don't understand why the error occurs. Clearly, python is calling sendmail.sh and the script has correct permissions set on it. If run sendmail.sh by hand, the mail is sent correctly.
The root cause is the error message given by ssmtp's mail, which is most unhelpful.
A quick googling it reveals http://www.raspberrypi.org/forums/viewtopic.php?t=46218&p=386393 which says the following:
Try running the command with an additional -d parameter to get some more debug information to help determine the cause of the issue:
echo "Test" | mail -d -s "Test" myemail#mydomain.co.uk
<...>
I checked my error logs, and noticed this:
<date time> raspberrypi sSMTP[3477]: <a bunch of messages, including the error showing the root cause>
You can try this command:
os.system('./sendmail.sh')

Make CGI-script work (Apache, Python, MySQL)

Could you help me read some strings from MySQL and show them in my web browser.
This is just for my localhost.
I receive the following:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, [no address given] and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
As for my file hello.py, it is situated here:
root#ubuntu:/var/www/test1.my/www/cgi-bin# ls -l
total 8
It's properties are as follows:
-rwxr-xr-x 1 root root 465 Nov 2 23:30 hello.py
-rwxr-xr-x 1 root root 186 Nov 2 15:59 hello.py~
Could you give me some hint what to fix?
These are my configuration files
From what I see, you need to output an empty line after writing your Content-Type-header in hello.py:
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
print "Content-Type: text/html\n"
# newline after headers! ^^
print "<html><head><title>Книги</title></head>"
...
Also, you should check the apache error logs (usually at /var/log/apache/error.log) to get more information about the error.

Categories

Resources