0

is it possible to distinguish whether my Python script is being executed from the web server (as CGI script) or from the command line?

I've seen some replies only for PHP and Perl:
What is the canonical way to determine commandline vs. http execution of a PHP script?
How can I determine if a script was called from the command line or as a cgi script?

Community
  • 1
  • 1
vl1131
  • 35
  • 1
  • 8

1 Answers1

2

The Perl answer applies to Python as well; check for the GATEWAY_INTERFACE environment variable:

import os
if 'GATEWAY_INTERFACE' in os.environ:
    print ('CGI')
else:
    print ('Not CGI. CLI?')
Community
  • 1
  • 1
phihag
  • 278,196
  • 72
  • 453
  • 469