Home > Articles > Open Source > Python

Python Libraries

  • Print
  • + Share This

UNIX Specific

This group of modules exposes interfaces to features that are specific to the UNIX environment.

posix

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

pwd

The pwd module provides access to the UNIX passwd (password database) file routines.

pwd.getpwnam()

Returns the password of a given user.

basic syntax: password = getpwnam(username)[1]

>>> import pwd, getpass
>>> pw = pwd.getpwnam(getpass.getuser())[1]

grp

The grp module provides access to the UNIX group database.

crypt

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.

To encrypt:

newpwd = crypt.crypt(passwordstring, salt)

salt consists of a two-random character seed used to initialize the algorithm.

To verify:

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)[1] # 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."

dlmodule

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.

dbm

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.

gdbm

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.

termios

The termios module provides an interface to the POSIX calls for managing the behavior of the POSIX tty.

TERMIOS

The TERMIOS module stores constants required while using the termios module.

tty

The tty module implements terminal controlling functions for switching the tty into cbreak and raw modes.

pty

The pty module offers utilities to handle the pseudo-terminal concept.

fcntl

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.

pipes

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.

posixfile

The posixfile module provides file-like objects with support for locking. It seems that this module will become obsolete soon.

resource

The resource module offers mechanisms for measuring and controlling system resources used by a program.

nis

The nis module is a thin wrapper around Sun's NIS library.

syslog

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.

popen2

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

Note that as of release 2.0, functions popen2, popen3, popen4 are supported on the Windows Platform.

commands

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.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.