While doing this program (Expression Evaluation script,(Python,html, cgi)) in lab, I faced a problem with server internal error.My friends and me try to solve the issue, but can’t make it runnable.The same code (Same Syntax structure) is working properly with another sample of programs.So I googled about the error message.They suggest some option to fix it , but it won’t match the current situation.Even now am not completely sure with the solution, the blankline after “Content-type: text/html\n” is the problem. By editing that, the program run successfully. Here I share the details.
Download the package xampp, say xampp-linux-1.7.4.tar.gz
Steps to run the server:
1) copy the package file to /opt
sudo cp folder/xampp-linux-1.7.4.tar.gz /opt
(Give password for the permision)
2) Extract it :/opt
sudo tar xzvf xampp-linux-1.7.4.tar.gz
One folder named lampp is created in /opt.
3) We can save the python script file in /opt/lampp/cgi-bin.
After saving the file, it should make executable.
sudo chmod 755 filename.py
4) Also save the html file in /opt/lampp/htdocs
5) Start the server
sudo ./lampp start
6) Open the web-browser
type address as : 127.0.0.1/
You will get xampp server page, now start with the exercises.
After saving the html file in /opt/lampp/htdocs, you can access it from the address : 127.0.0.1/filename.html
One of the error I faced is that,
The server encountered an internal error and was unable to complete your request.
Premature end of script headers: expression.py
If you think this is a server error, please contact the webmaster. ”
To overcome this issue check the following, may be useful to fix it.
1) The firstline should be #!/usr/bin/python
2) Provide a space between Content-type and text
3) There should be atleast one blankline after the Content-type line
print “Content-Type: text/html\n”
Sample program is attached here:
//Program to evaluate an expression//
Enter the values to evaluate the Expression (a*b)-(c*d)
value a: 5
value b: 4
value c: 3
value d: 2
Expression : (5 * 4)-(3 * 2)
The Result is : 14
To stop the server:
sudo ./lampp stop