LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-17-2011, 02:23 PM   #1
felix001
Member
 
Registered: Jan 2009
Posts: 101

Rep: Reputation: 15
Python CGI Issue


Does anyone know how to run a linux command from a Python CGI script. Im currently trying :

Code:
#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb
import os
from subprocess import call

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 "<p>",call(["ls", "-l"]),"</p>"
print '</body>'
print '</html>'
.. but I keep getting an error within my apache logs saying Malformed Header.

Has anyone any ideas ??
 
Old 07-17-2011, 04:10 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Just a shot: remove the CR's from your Content-type line.
--- rod.
 
Old 07-18-2011, 12:20 AM   #3
felix001
Member
 
Registered: Jan 2009
Posts: 101

Original Poster
Rep: Reputation: 15
Tried that but same issue. The stange thing is if I run this code but call an echo 123 it errors with malformed header 123. If I use a print "123" it works. It's like the system call is echo out the output before the header (???)
 
Old 07-18-2011, 09:06 AM   #4
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
Okay, then use telnet or netcat to issue an HTTP request from a URL that 'works', even a static page. Compare that to the output of your Python script when run from a shell as a stand-alone program. The difference(s) should be fairly obvious.
Perhaps Python is complaining about some of your module imports.
--- rod.
 
Old 07-18-2011, 02:11 PM   #5
felix001
Member
 
Registered: Jan 2009
Posts: 101

Original Poster
Rep: Reputation: 15
I found a way in which to get this to work and it was to use the -u switch within my shabang. Does anyone know what this actually does.

NOT WORKING

[
Code:
root@webserver1 ~]# python /var/www/cgi-bin/hello_get2.py
Content-type: text/html


<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body><p>
123
[Querying whois.arin.net]
[whois.arin.net]
WORKING
Code:
[root@webserver1 ~]# python /var/www/cgi-bin/hello_get.py
Content-type: text/html


<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body><p>
WORKING
Code:
[root@webserver1 ~]# curl http://127.0.0.1/cgi-bin/hello_get.py

<html>
<head>
<title>Hello Word - First CGI Program</title>
</head>
<body><p>
123
 
Old 07-18-2011, 04:03 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
According to the python man-page (very much worthwhile reading, btw!):
Code:
       -u     Force stdin, stdout and stderr to  be  totally  unbuffered.   On
              systems  where  it matters, also put stdin, stdout and stderr in
              binary mode.  Note that there is internal  buffering  in  xread-
              lines(),  readlines()  and  file-object  iterators ("for line in
              sys.stdin") which is not influenced by  this  option.   To  work
              around  this, you will want to use "sys.stdin.readline()" inside
              a "while 1:" loop.

Cheers,
Tink
 
Old 07-18-2011, 05:24 PM   #7
felix001
Member
 
Registered: Jan 2009
Posts: 101

Original Poster
Rep: Reputation: 15
I had read this before but im not really sure why I need to add it.
 
Old 07-18-2011, 08:18 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
Originally Posted by felix001 View Post
I had read this before but im not really sure why I need to add it.
Not sure how call works, but I could get it to play nice w/o -u
and the use of the following Popen:
Code:
#!/usr/bin/python

# Import modules for CGI handling
import cgi, cgitb
import os
import subprocess

child=subprocess.Popen(['ls','-l'], stdout=subprocess.PIPE )
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 "<p>","<br>".join(child.stdout.readlines()),"</p>"
print '</body>'
print '</html>'
Mind you, the spacing for the ls output is clobbered.


Cheers,
Tink
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
executing an external command with Python (CGI) laurapt Programming 1 07-08-2011 11:18 AM
C server & CGI scripts [python] meisterluv Programming 5 04-04-2010 11:23 AM
Python CGI-Bin Scripts chaney44145 Linux - Server 1 01-15-2008 11:44 AM
Python CGI - Batching in R larsenmtl Programming 1 09-29-2004 04:24 PM
CGI in Python indian Programming 2 08-18-2004 02:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration