Introduction


Credit: Donn Cave, University of Washington

In this chapter, we consider a class of programmerthe humble system administratorin contrast to other chapters' focus on functional domains. As a programmer, the system administrator faces most of the same problems that other programmers face and should find the rest of this book of at least equal interest.

Python's advantages in the system administration domain are also quite familiar to other Python programmers, but Python's competition is different. On Unix platforms, at any rate, the landscape is dominated by a handful of lightweight languages such as the Bourne shell and awk that aren't exactly made obsolete by Python. These little languages can often support a simpler, clearer, and more concise solution than Python, particularly for commands that you're typing interactively at the shell command prompt. But Python can do things these languages can't, and it's often more robust when dealing with issues such as unusually large data inputs. Another notable competitor, especially on Unix systems, is Perl (which isn't really a little language at all), with just about the same overall power as Python, and usable for typing a few commands interactively at the shell's command prompt. Python's strength here is readability and maintainability: when you dust off a script you wrote in a hurry eight months ago, because you need to make some changes to it, you don't spend an hour to figure out whatever exactly you had in mind when you wrote this or that subtle trick. You just don't use any tricks at all, subtle or gross, so that your Python scrips work just fine and you don't burn your time, months later, striving to reverse-engineer them for understanding.

One item that stands out in this chapter's solutions is the wrapper: the alternative, programmed interface to a software system. On Unix (including, these days, Mac OS X), this is usually a fairly prosaic matter of diversion and analysis of text I/O. Life is easy when the programs you're dealing with are able to just give clean textual output, without requiring complex interaction (see Eric Raymond, The Art of Unix Programming, http://www.faqs.org/docs/artu/, for an informative overview of how programs should be architected to make your life easy). However, even when you have to wrap a program that's necessarily interactive, all is far from lost. Python has very good support in this area, thanks, first of all, to the fact that it places C-level pseudo-TTY functions at your disposal (see the pty module of the Python Standard Library). The pseudo-TTY device is like a bidirectional pipe with TTY driver support, so it's essential for things such as password prompts that insist on a TTY. Because it appears to be a TTY, applications writing to a pseudo-TTY normally use line buffering, instead of the block buffering that gives problems with pipes. Pipes are more portable and less trouble to work with, but they don't work for interfacing to every application. Excellent third-party extensions exist that wrap pty into higher-level layers for ease of use, most notably Pexpect, http://pexpect.sourceforge.net/.

On Windows, the situation often is not as prosaic as on Unix-like platforms, since the information you need to do your system administration job may be somewhere in the registry, may be available via some Windows APIs, and/or may be available via COM. The standard Python library _winreg module, Mark Hammond's PyWin32 package, and Thomas Heller's ctypes, taken together, give the Windows administrator reasonably easy access to all of these sources, and you'll see more Windows administration recipes here than you will ones for Unix. The competition for Python as a system administration language on Windows is feeble compared to that on Unix, which is yet another reason for the platform's prominence here. The PyWin32 extensions are available for download at http://sourceforge.net/projects/pywin32/. PyWin32 also comes with ActiveState's ActivePython distribution of Python (http://www.activestate.com/ActivePython/). To use this rich and extremely useful package most effectively, you also need Mark Hammond and Andy Robinson, Python Programming on Win32 (O'Reilly). ctypes is available for download at http://sourceforge.net/projects/ctypes.

While it may sometimes be difficult to see what brought all the recipes together in this chapter, it isn't difficult to see why system administrators deserve their own chapter: Python would be nowhere without them! Who else, back when Python was still an obscure, fledgling language, could bring it into an organization and almost covertly infiltrate it into the working environment? If it weren't for the offices of these benevolent and pragmatic anarchists, Python might well have languished in obscurity despite its merits.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net