473,478 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

How to tell whetehr Python script called as CGI or from command line?

I'm writing a Python script that can either be called as a Cron job or
as a web page (i.e. as a CGI in response to an HTTP request). This is
to process the mailboxes on my web server (to which I don't have
command line access) to remove old messages. How do I find out whether
the script has been called as a Cron job or as a CGI? I need to know
this so I can format the output correctly, e.g. if this is a web
request I need to start the output with "Content-type: text/html\n
\n<html>", to do newlines by "<p>" or "<br>" etc.

Can I just access some header line which will always have a value in a
web request, but which will be None if running from the command line
or as a Cron job, or something similar? How?

Thanks - Rowan

Apr 16 '07 #1
2 1559
ro***@sylvester-bradley.org wrote:
I'm writing a Python script that can either be called as a Cron job or
as a web page (i.e. as a CGI in response to an HTTP request). This is
to process the mailboxes on my web server (to which I don't have
command line access) to remove old messages. How do I find out whether
the script has been called as a Cron job or as a CGI? I need to know
this so I can format the output correctly, e.g. if this is a web
request I need to start the output with "Content-type: text/html\n
\n<html>", to do newlines by "<p>" or "<br>" etc.

Can I just access some header line which will always have a value in a
web request, but which will be None if running from the command line
or as a Cron job, or something similar? How?

Thanks - Rowan
The CGI standard requires that the calling server sets several
environment variables, so you could test for the presence of one or more
of those - this is only going to be indicative, though, since any shell
could have the same variables set in its environment.

import os
if "QUERY_STRING" in os.environ:
# CGI script

might work.

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Skype: holdenweb http://del.icio.us/steve.holden
Recent Ramblings http://holdenweb.blogspot.com

Apr 16 '07 #2
import os
if "QUERY_STRING" in os.environ:
# CGI script

might work.
Thanks Steve for this suggestion. I did something similar:
import os
req = os.getenv('REQUEST_URI')
if (not req is None) and len(req) 0:
web = True

which seemed to work for me.

Rowan

May 11 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2877
by: satish | last post by:
Hello all, I have a shared object executable viz. *cable* which I execute as follows : $ ansyscust71 -custom cable -p ANSYSRF **ansyscust71 is a shell script and is a part of a software...
21
13044
by: John Lin | last post by:
Howdy, I want to know how to tell if a forked process is done. Actually, my real question is that I want to run a shell script inside of a python script, and after the shell script has...
52
3693
by: Olivier Scalbert | last post by:
Hello , What is the python way of doing this : perl -pi -e 's/string1/string2/' file ? Thanks Olivier
0
1884
by: Fuzzyman | last post by:
Movable Python has just been released on sourceforge. Movable Python is a frozen distribution of Python. It will run python scripts without needing to be installed. ...
17
3837
by: Paul Rubin | last post by:
Dumb question from a Windows ignoramus: I find myself needing to write a Python app (call it myapp.py) that uses tkinter, which as it happens has to be used under (ugh) Windows. That's Windows...
2
1918
by: Alain Paschoud | last post by:
Hi all, I made a small dialog in WxPython. I can run the python script with a double-click or through command line, and everything goes fine (dialog appears, which means that wx module has been...
12
2979
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed...
9
2426
by: korean_dave | last post by:
From command Prompt, i type in a script, "tryme.py". This, instead, brings up PythonWin editor and Interactive Window. Path variable is "C:\Python24". (I need Python 2.4 installed, not 2.5) ...
0
6899
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7019
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7067
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6847
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4757
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4463
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2980
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2970
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1288
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.