python cgi script is not executing in XAMPP - python

i am a beginner in python. I've installed python34, and xampp. I've changed the http.config file and added the .py extension in handler block. then i put my python script into xamp/bin-cgi and set the first line of the python script as, "#!C:/Python34/python.exe". But when i opens the file through localhost/cgi-bin/test.py it doesn't showing anything only a blank screen, Below the content of the file.
#!C:/Python34/python.exe
print "Content-type: text/html"
print
print "<html>"
print "<head>"
print "<title>welcome cgi</title>"
print "</head>"
print "<body>"
print "<h1>first python page</h1>"
print "<p>heihei</p>"
print "</body>"
print "</html>"

You should rewrite the first line like this:
#!"C:\Python34\python.exe"

You are using Python 2.7 with your print statements. That is the first error. YOu are calling the Python 3.4 interpreters.
Also, you need to change your first line to
#!/Python34/python
# -*- coding: UTF-8 -*-
# enable debugging
Then, change your print statements to have parentheses
print("Content-type: text/html")
print()
print("""<html>
<head>
<title>welcome cgi</title>
</head>
<body>
<h1>first python page</h1>
<p>heihei</p>
</body>
</html>""")
ATTENTION:
If you notice, I changed up your code a bit and got rid of a bunch of print statements. Instead, I used what's called a multiline string (instead of writing "blah" I would do """blah"""). For example if I did
print("asdasdasd
asdasdasdasdasd")
This wouldn't work
But, if I changed it to
print("""asdasdasdasdasd
asdasdasdasdasd""")
This would be a perfectly acceptable command. Every new line would be registered as a "\n" so really, the string we are printing out is "asdasdasdasdasd\nasdasdasdasdasd" where \n marks a new line

Related

Run a .bat file from a Python CGI file with Apache Webserver

This might be really easy. But its just not working for me.
I have a .bat file I would like to run, which performs stuff on the Server, and should send an email with an Attachement.
The .bat file works fine, it sends the email with the log and everything.
Now I would like to run that file from a Webserver. So that I can click on an HTML form Button, and it executes.
I have installed Apache, Python 2.7 for it.
I have configured Apache to allow cgi files, and It works when I put a file as index.py with following code.
But when I press the Submit button it goes through, but the .bat files is not being executed. Help! :)
Is there another way I can run a .bat file to do stuff on my server from a Webserver maybe? thank you in beforehand.
I tried the action in the form to direct to a .py and .cgi file... don't get it to work
Below the code I a have been using.
#!/Python27/python
#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable()
print "Content-type: text/html"
print
print "<html><head>"
print "<form action='../cgi-bin/send_email.py'>"
print "<input type='submit' value='Submit'>"
print "</form>"
send_email.py looks like this.
#!/Python27/python
#!/usr/bin/env python
import cgi
import cgitb; cgitb.enable()
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
You can invoke the batch file with cmd.exe:
...
cmd = r'c:\Windows\System32\cmd.exe'
batDir = r'C:\Path\to\batchfolder'
batName = r'batch.bat'
p = Popen(r"{0} /C {1}\{2}".format(cmd,batDir,batName), cwd=batDir)
...

How to switch users for Python CGI Programming?

I have an HTML form which is handled by a Python script using CGI programming. From my Python script, I want to switch users from apache2 to monkey. The reason is because I'm using os.system to run another script from within my Python script.
The Python script works fine but I keep getting Permission errors when executing this command: os.system('python other_script.py'). What I realize is that when I am running the HTML form, I am apache2 instead of monkey. I'd like to know how to switch users (as monkey not root) while executing the Python script.
Here is what my Python script looks like:
#!/usr/bin/python -W
# Import modules for CGI handling
import cgi, cgitb
import pwd
import grp
import sys
import os
# Create instance of FieldStorage
form = cgi.FieldStorage()
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 '</body>'
print '</html>'
os.system('python other_script.py') # getting permission errors here

Print a header in python

Is there a python equivalent to this in perl?
use CGI;
my $IN = new CGI;
print $IN->header();
# $IN->header(-type=>'image/gif');
I see that this will print a header along with the ENV information:
import cgi
cgi.test()
Obviously I don't need the ENV information when not debugging. Otherwise do I need to type the print "Content-type: text/html" or print "Content-type: image/gif" everytime or is there a module already written that is similar?

run python script as cgi apache server

I am trying to make a python script run as cgi, using an Apache server. My script looks something like this:
#!/usr/bin/python
import cgi
if __name__ == "__main__":
print("Content-type: text/html")
print("<HTML>")
print("<HEAD>")
I have done the necessary configurations in httpd.conf(in my opinion):
<Directory "/opt/lampp/htdocs/xampp/python">
Options +ExecCGI
AddHandler cgi-script .cgi .py
Order allow,deny
Allow from all
</Directory>
I have set the execution permission for the script with chmod
However, when I try to access the script via localhost i get an Error 500:End of script output before headers:script.py
What could be the problem? The script is created in an Unix like environment so I think the problem of clrf vs lf doesn't stand. Thanks a lot.
I think you are missing a print statement after
print("Content-type: text/html")
The output of a CGI script should consist of two sections, separated by a blank line. The first section contains a number of headers, telling the client what kind of data is
following.
The second section is usually HTML, which allows the client software to display nicely formatted text with header, in-line images, etc.
It may look like
#!/usr/bin/env python
print "Content-Type: text/html"
print
print """
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""
For more details visit python-cgi
For python3
#!/usr/bin/env python3
print("Content-Type: text/html")
print()
print ("""
<TITLE>CGI script ! Python</TITLE>
<H1>This is my first CGI script</H1>
Hello, world!
"""
)

syntax error near unexpected token `<' from Applescript to Python

I am using the following command to pass a string to python from Applescript
String mytext contains the HTML body of an email starting with <...
Applescript
display dialog (do shell script "/Users/mymac/Documents/'Microsoft User Data'/'Outlook Script Menu Items'/ test.py" & mytext)
Python
#!/usr/bin/env python
import sys
import string
def main():
print sys.argv[1:]
if __name__ == "__main__":
main()
How can I rectify this error?
You don't want to pass the HTML as an argument to the Python script. Instead, do something like:
display dialog (do shell script "/Users/mymac/Documents/'Microsoft User Data'/'Outlook Script Menu Items'/ test.py < webpage.html")
print sys.stdin.read()
The problem is you are executing a shell script ... by constructing a long line of text.
"/Users/mymac/Documents/'Microsoft User Data'/'Outlook Script Menu Items'/ test.py" & " ... ")
but the "<" sign and the ">" sign have a special meaning for the shell.

Categories