1. Technology

Testing CGI With Python

By

3 of 4

Using os.environ to Know Your Environment

After the output "Environmental variables", we use a for-loop to work through the dictionary os.environ. We bracket this with the "<ul>...</ul>" tags so the output can be in bulleted format.

The keys of the os.environ dictionary change with the environment into which you import the os module. If you use it in a terminal shell, you will get output like this (taken from my terminal session on an Ubuntu Linux system):

COLORFGBG: default;default;0
COLORTERM: rxvt-xpm
DBUS_SESSION_BUS_ADDRESS: unix:abstract=/tmp/dbus-KBLFrpYxUT,guid=29058646df034d11ca95e1b864f6df00
DESKTOP_SESSION: gnome
DISPLAY: :0.0
GDMSESSION: gnome
GDM_XSERVER_LOCATION: local
GNOME_DESKTOP_SESSION_ID: Default
GNOME_KEYRING_SOCKET: /tmp/keyring-PFIVd8/socket
GTK_RC_FILES: /etc/gtk/gtkrc:/home/al/.gtkrc-1.2-gnome2
HISTCONTROL: ignoredups
HOME: /home/al
LANG: en_GB.UTF-8
LANGUAGE: en_GB:en
LESSCLOSE: /usr/bin/lesspipe %s %s
LESSOPEN: | /usr/bin/lesspipe %s
LOGNAME: al
LS_COLORS:
PATH: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/bin/X11:/usr/games
PWD: /home/al
SESSION_MANAGER: local/BlackPhoenix:/tmp/.ICE-unix/5343
SHELL: /bin/bash
SHLVL: 2
SSH_AGENT_PID: 5386
SSH_AUTH_SOCK: /tmp/ssh-HdFRPy5343/agent.5343
TERM: rxvt-unicode
USER: al
USERNAME: al
WINDOWID: 77594629
XAUTHORITY: /home/al/.Xauthority
  1. About.com
  2. Technology
  3. Python
  4. Web Development
  5. Python CGI Testing: Using os.environ to Know Your Environment

©2014 About.com. All rights reserved.