views:

51

answers:

1

I have a python script and I wanna know if the request is from web or from command line. How can I do this?

Thanks

+8  A: 

When run as a CGI, environment variables such as REQUEST_METHOD will be present. If not, then you're not running in a CGI environment.

You can check this like this:

import os
if os.getenv("REQUEST_METHOD"):
    print("running as CGI")
else:
    print("not running as CGI")
Greg Hewgill
Thanks, it worked.
Luiz Fernando