是否可以区分我的Python脚本是从Web服务器(作为CGI脚本)还是从命令行执行?
我看过只有PHP和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?
答案 0 :(得分:2)
Perl answer也适用于Python;检查GATEWAY_INTERFACE
environment variable:
import os
if 'GATEWAY_INTERFACE' in os.environ:
print ('CGI')
else:
print ('Not CGI. CLI?')