- Python Libraries
- Python Services
- The String Group
- Generic Operational System
- Optional Operational System
- Internet Protocol and Support
- Internet Data Handling
- Restricted Execution
- UNIX Specific
- SGI IRIX Specific
- Sun OS Specific
- MS Windows Specific
- Macintosh Specific
- Undocumented Modules
This group of modules exposes interfaces to features that are specific to the UNIX environment.
The posix module provides access to the most common POSIX system calls. Do not import this module directly; instead, I suggest that you import the os module.
>>> uid = posix.getuid() # returns the user id
The pwd module provides access to the UNIX passwd (password database) file routines.
Returns the password of a given user.
basic syntax: password = getpwnam(username)
>>> import pwd, getpass >>> pw = pwd.getpwnam(getpass.getuser())
The grp module provides access to the UNIX group database.
The crypt module offers an interface to the UNIX crypt routine. This module has a hash function based on a modified DES algorithm that is used to check UNIX passwords.
newpwd = crypt.crypt(passwordstring, salt)
salt consists of a two-random character seed used to initialize the algorithm.
If newpwd == crypt.crypt(passwordstring, newpwd[:2]) import getpass import pwd import crypt uname = getpass.getuser() # get username from environment pw = getpass.getpass() # get entered password realpw = pwd.getpwnam(uname) # get real password entrpw = crypt.crypt(pw, realpw[:2]) # returns an encrypted password if realpw == entrpw: # compare passwords print "Password Accepted" else: print "Get lost."
The dlmodule module exposes an interface to call C functions in shared objects that handle dynamically linked libraries. Note that this module is not needed for dynamic loading of Python modules. The documentation says that it is a highly experimental and dangerous device for calling arbitrary C functions in arbitrary shared libraries.
The dbm module is a database interface that implements a simple UNIX (n)dbm library access method. dbm objects behave like dictionaries in which keys and values must contain string objects. This module allows strings, which might encode any python objects, to be archived in indexed files.
See Chapter 8 for details.
The gdbm module is similar to the dbm module. However, their files are incompatible. This module provides a reinterpretation of the GNU dbm library.
See Chapter 8 for details.
The termios module provides an interface to the POSIX calls for managing the behavior of the POSIX tty.
The TERMIOS module stores constants required while using the termios module.
The tty module implements terminal controlling functions for switching the tty into cbreak and raw modes.
The pty module offers utilities to handle the pseudo-terminal concept.
The fcntl module performs file and I/O control on UNIX file descriptors. This module implements The fcntl() and ioctl() system calls, which can be used for file locking.
The pipes module offers an interface to UNIX shell pipelines. By abstracting the pipeline concept, it enables you to create and use your own pipelines.
The posixfile module provides file-like objects with support for locking. It seems that this module will become obsolete soon.
The resource module offers mechanisms for measuring and controlling system resources used by a program.
The nis module is a thin wrapper around Sun's NIS library.
The syslog module implements an interface to the UNIX syslog library routines. This module allows you to trace the activity of your programs in a way similar to many daemons running on a typical GNU/Linux system.
import syslog syslog.syslog('This script was activated' ) print "I am a lumberjack, and I am OK!" syslog.syslog('Shutting down script' )
Use the command tail -f /var/log/messages to read what your script is writing to the log.
The popen2 module allows you to create processes by running external commands and to connect their accessible streams (stdin, stdout, and stderr) using pipes.
import os,popen2 str1 = os.popen('ls','r').read() print str1 out1,in1 = popen2.popen2('cat') in1.write(str1) in1.close() str2 = out1.read() out1.close() print str2
Note that as of release 2.0, functions popen2, popen3, popen4 are supported on the Windows Platform.
The commands module provides functions that execute external commands under UNIX by implementing wrapping functions for the os.popen() function. Those functions get a system command as a string argument and return any output generated by that command.