pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

os.system() in cgi script

Wed Jul 20, 2016 10:32 am

I'm trying to get a simple cgi script running for a student (to call the example Pipsta printer programs from a web interface) and for the moment I don't see what's going wrong. In order to narrow it down I picked up an example cgi script from the net and modified it as follows:

Code: Select all

#!/usr/bin/python
import datetime
import os

print "Content-type:text/html\r\n\r\n"
print '<html>'
print '<head>'
print '<title>Hello World - First CGI Program</title>'
print '</head>'
print '<body>'
print '<h2>Hello World! This is my first CGI program</h2>'
print 'Todays date is '
print datetime.date.today()
print '<p>Or another way:<p>'
os.system("date")
print '</body>'
print '</html>'
(Actually, it's a lie. I wrote my first cgi script around 20 years ago.)

The date comes out as expected from the datetime.date.today() but nothing appears from the os.system line.

The answer has got to be incredibly simple but it escapes me for the moment.

Regards - Philip

User avatar
RaTTuS
Posts: 10548
Joined: Tue Nov 29, 2011 11:12 am
Location: North West UK
Contact: Twitter YouTube

Re: os.system() in cgi script

Wed Jul 20, 2016 10:36 am

full path to date ?
How To ask Questions :- http://www.catb.org/esr/faqs/smart-questions.html
WARNING - some parts of this post may be erroneous YMMV

1QC43qbL5FySu2Pi51vGqKqxy3UiJgukSX
Covfefe

pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Re: os.system() in cgi script

Wed Jul 20, 2016 10:51 am

RaTTuS wrote:full path to date ?
Good try - I'm sure that's caught me out before, but os.system("/bin/date") is the same.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: os.system() in cgi script

Wed Jul 20, 2016 11:24 am

Why should the os.system command output anything in your script?

it calls the command but you're not directing the output to your cgi script. If you run it manually from a python script you'd probably see the output as it's dumped to stdout but that's not what you want here.

Try using subprocess to capture the output in a variable and then print it to the cgi script. e.g.

Code: Select all

import subprocess
os_date = subprocess.check_output(["date"])
print os_date
In iPython

Code: Select all

In [1]: import subprocess
In [2]: os_date = subprocess.check_output(["date"])
In [3]: print os_date
Wed Jul 20 11:44:09 UTC 2016
In [4]: import os
In [5]: os.system("date")
Wed Jul 20 11:45:10 UTC 2016
Out[5]: 0
Notice that, while os.system does indeed show the output, this is because it is dumped to stdout. Furthermore, os.system returns a value equal to the return code of the command, not the output whereas the check_output command returns the output itself (and stores it in "os_date") which is what you want here.
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

pleriche
Posts: 90
Joined: Mon Oct 14, 2013 8:44 am

Re: os.system() in cgi script

Wed Jul 20, 2016 4:26 pm

Thank you - I think you have the answer. Neither os.system in the Python library reference nor the system(3) man page say what happens to stdout from the command, leaving the naive user to assume the command inherits stdout from the parent process. Evidently not, though I'm still trying to get my head around exactly what's going on in terms of file descriptors. But for the moment, that's academic.

User avatar
elParaguayo
Posts: 1943
Joined: Wed May 16, 2012 12:46 pm
Location: London, UK

Re: os.system() in cgi script

Wed Jul 20, 2016 4:36 pm

I wouldn't get bogged down in file descriptors for now (my understanding is somewhat hazy). Give my suggestion a go and let me know if it works for you.

Also, I hope my first question didn't come across as being rude, I meant to phrase it slightly better than I did!
RPi Information Screen: plugin based system for displaying weather, travel information, football scores etc.

Return to “Python”