Back to description
This chapter provides a quick overview of the Python language. The goal in this chapter is not to teach you the Python languageexcellent... more
This chapter provides a quick overview of the Python language. The goal in this chapter is not to teach you the Python languageexcellent books have been written on that subject, such as Beginning Python (Wrox, 2005). This chapter describes Python’s lexical structure and programming conventions, so if you are familiar with other scripting languages such as Perl or Ruby, or with compiled programming languages such as Java or C#, you should easily be up to speed in no time.
After this chapter you?ll write and work with applications that show you how to create and import modules, how to create and call functions, getting user input and passing the value provided by the user to the program, and how to find information related to the Python Standard Library. You?ll work with, connect to, and query a database and add, modify, and delete records and output information in the database to a CSV file. You?ll work with Python?s urllib2 module, the mod_python Apache module to present an HTML form to a client user, and the Python smtplib module to connect to an SMTP mail server. As an alternative to a full database system you?ll see how to use XML to store and retrieve structured data in a Python script. Another application covered builds a Python script to connect to your machines and check version levels, you can automatically build a list of the version of installed applications on each computer. The remaining applications show you examples of a Python content management system (CMS), working with operating system features including Windows and Linux/Unix services, and debugging tools and testing frameworks for Python.
... less
Have you ever installed a program and wanted to know exactly what was installed? Programs typically include numerous files... more
Have you ever installed a program and wanted to know exactly what was installed? Programs typically include numerous files and directoriesin some cases hundreds. It can be difficult to determine what was put on the system. This can especially be important if, for instance, you are verifying an install to ensure that all the directories and files were placed on the system. Conversely, with an uninstall, you want to verify just the opposite––that everything that had been put on the system is now gone.
The File/DirectoryComparison program enables you to create a “snapshot” of your system based on a directory “base” you provide. It will also perform a comparison of two snapshots to show you the differences. This includes items missing from snapshot 1 but in snapshot 2, and items missing in snapshot 2 but present in snapshot 1.
Along the way, you’ll learn several valuable things about Python and its features:
How to create and import modules
How to create and call functions
Getting user input and passing the value provided by the user to the program
How to find information related to the Python Standard Library
There is a well-known saying that there are two types of programs: those that are toys and those that access databases. Well... more
There is a well-known saying that there are two types of programs: those that are toys and those that access databases. Well, the project in this chapter is not a toy, so that should narrow it down for you. This project will enable you to manage your DVD inventory. It takes advantage of MySQL, a powerful open-source relational database management system (RDMS). Along the way, you’ll learn how to do the following:
Work with a database
Connect to a database
Query the database
Add records
Modify records
Delete records
Output information in the database to a CSV file
Why All This Is Important
Basically, when you get right down to it, all a computer does is organize and manipulate chunks of data. Displaying a bitmap? Moving data around. Printing a spreadsheet? Sending bits of data from one location to another. Computers are most meaningful when they are able to sort and arrange that data in a way that aids the imperfect carbon-based life forms who operate them––namely, you and me.
This is where database management systems come in. They take the sets of data that you and I deal with all around us (sports statistics, medicine prescriptions, class rosters, military formations, whatever) and they arrange it in a way that makes sense to us.
In other words, understanding how to use databases is one of the most fundamental things you can learn as a developer.
So far, the programs we’ve written have been useful, but they’ve shared one main limitationthey are largely self contained... more
So far, the programs we’ve written have been useful, but they’ve shared one main limitationthey are largely self contained, intended to be run on a single computer (although the database program could certainly have a database housed on another computer). Our next program will change that.
The Web Performance Tester has two main functions (I use “function” in the generic sense here):
It will use Python’s urllib2 module to emulate a web browser, access several well-known web sites, and report the amount of time it took to get back the HTML from the page accessed. It will also write its results to a log file, which can be accessed later.
urllib2
It will communicate with a simple Python web server on another machine on the computer’s internal network, download a text file and a binary file (through HTTP), and report the results. It will record the results in a logfile.
Python
logfile
Why Is This Application Important?
Much of the work done by computers is done over networks. Whether it is browsing web pages, connecting to terminal servers via telnet, downloading files via FTP, or performing some other network task, connecting to other computers is one of the most vital tasks a computer performs today.
Using Python, you can act as a server for many of the most popular Internet protocols: HTTP (Web), FTP (File Transfer), Telnet (Remote login), SMTP (Mail service), and SNMP (Network Management), just to name a few.
This application demonstrates how to use Python and its rich collection of modules to interface with a network.
All the applications examined thus far have involved a character-based user interface, based on characters viewed and typed... more
All the applications examined thus far have involved a character-based user interface, based on characters viewed and typed in a terminal. In this chapter, you will explore using mod_python, an add-on module for the popular open-source Apache web server, to use Python to interact with client users through a web browser.
mod_python
The Customer Follow-up application will perform two main functions:
It will use the mod_python Apache module to present an HTML form to a client user, and enable users to type in their information (including comments) and submit it.
It will use the Python smtplib module to connect to an SMTP mail server and send an e-mail message to a predefined “webmaster” e-mail address.
smtplib
For each comment submitted, it will enter a log entry to a csv file stored on the web server machine. This log can then be queried and sorted like any other spreadsheet file.
csv
In Chapter 3, you learned how to access a database to store and retrieve structured data for use in a Python script, but... more
In Chapter 3, you learned how to access a database to store and retrieve structured data for use in a Python script, but what about situations where a database is overkill? It is precisely for these kinds of situations that XML exists, and Python is brimming with features to enable you to write to, query, and otherwise manipulate XML.
The test management and reporting system in this chapter shows how you can use XML to store and retrieve structured data in a Python script.
The application will perform the following functions:
Enable a user to run tests and report on the pass or fail results of the tests
Enable a user to list the test runs, by date
Enable a user to show test run results for any previous test run
Enable a user to output the results of any completed test run to an HTML file, so that results can be viewed in a web browser
What Are We Testing?
You’ll notice as you look at the application that the “program under test” is fairly trivial. That’s because the focus of the application is the test framework, not the program that’s being tested. It can easily be adapted to a more complex application under test.
Imagine you are the administrator of three computer labs. Some of the machines have had their versions of Java and Python... more
Imagine you are the administrator of three computer labs. Some of the machines have had their versions of Java and Python updated, but you’re not sure how many, or which ones. You could go to each computer individually and check.
But you’re not going to do that.
By building a Python script to connect to your machines and check version levels, you can automatically build a list of the version of installed applications on each computer.
The version management system in this chapter shows how you can use Telnet to retrieve version information for a list of applications (in this program, the applications checked will be Java, Python, and Perl). The program will then write the results of the check to a CSV log file.
Allow the user to identify an IP address and a list of applications to check for from (entered via command-line arguments)
Log in, using Telnet, to the machine and check the version numbers of each application
Write the results of the query to a CSV log file
What Is Telnet?
Telnet is an Internet protocol used on Internet or Local Area Network (LAN) networks. Basically, Telnet enables you to log on to remote systems to perform various tasks. Python has a telnetlib module that enables a script to emulate a Telnet client. That is how the program in this chapter will get application version information about each remote computer.
telnetlib
So far, we’ve created applications to do many different things––log in to servers, connect to databases, manipulate XML,... more
So far, we’ve created applications to do many different things––log in to servers, connect to databases, manipulate XML, and the like. However, every program has been created from scratch. What if you don't want to start from scratch? That is what frameworks were built for.
The application in this chapter uses Plone, an open-source Python-based content management system.
What Is a Content Management System?
A content management system (CMS) is a system used to manage content. Content management systems are deployed to enable multiple users to create and edit content for access by others, usually on a websitefor example, the website Wikipedia.
Whether a program helps a user balance a checkbook, play a game, write a letter, or connect to a website, all applications share one common feature––they all reside on top of an operating system. Be it Linux, Windows, or some other operating system, most applications (including Python scripts) must interface with the computer through its operating system. As with most scripting languages such as Perl or Ruby, Python has a vast collection of modules to enable the script programmer to interface with the operating system programmatically.
This chapter examines ways to communicate with the operating system through Python. Rather than a single, large application illustrating programming techniques, we will cover many topics with various snippets of code.
NOTE: The commands and modules covered in this chapter are a small subset of what is available. For complete coverage of operating system services, see the Python Reference Manual.
The chapter covers three main topic areas:
Generic operating system services––Many features of an operating system are shared among the systems. For example, all operating systems have some way of listing files. Python’s ability to access these generic operating system services makes programs more cross-platform.
Windows services––Python has many features to enable developers to access Windows services, such as the Windows Registry, the Windows Event Viewer, Windows services, and more. Techniques for accessing these services are explored in depth.
Unix/Linux services––As with Windows, with Unix or Linux there are services specific to the operating systems, such as syslog access, password and shadow password services, and others.
syslog
All of the projects from the previous chapterswhether it was a project that accessed files on the file system, interacted... more
All of the projects from the previous chapterswhether it was a project that accessed files on the file system, interacted with a database, or served pages on a web serverhave one thing in common: They didn’t work the first time.
It is inevitable as a programmer that you will run into errors in your programs. Fortunately, Python has built-in features to help you discover those “bugs” and take care of them:
The Python debugger (which is actually just another Python module itself) supports setting decision markers called breakpoints and allows you to “step” through code one line at a time. It supports very sophisticated debugging if needed, including providing a stack viewer.
There are several Python automated test frameworks that enable you to build automated tests to test your code. Having automated tests enables you to add functionality and run your tests to verify that you haven’t broken anything.
As indicated throughout the preceding chapters, the purpose of this book has been to provide you with the tools you need... more
As indicated throughout the preceding chapters, the purpose of this book has been to provide you with the tools you need to be productive with Python, and to avoid areas that might have less universal appeal. However, as you develop your knowledge and use of Python, you will no doubt want to delve into deeper waters.
This appendix suggests some resources I have found to be immensely useful.
This appendix contains the instructions for installing MySQL, to support the database application in Chapter 5, and Win32All... more
This appendix contains the instructions for installing MySQL, to support the database application in Chapter 5, and Win32All, to support some of the Python Windows integration described in Chapter 9.
Purchase Before purchasing this product, please be sure you have met all software and system requirements, and that you understand any limits placed upon its use.
Return Policy Wrox Chapters on Demand are non-returnable and non-refundable.
Reader Software Wrox Chapters on Demand are offered as PDFs, and they must be viewed using the Adobe Reader. If you do not have the Reader installed, it can be downloaded for free at Adobe.com.
Test Download As Wrox Chapters on Demand purchases are non-returnable, it is advisable that you test your system and software configurations with a free sample download before you place an order.
Usage Rights for a Wrox Chapter on Demand File Any Wrox Chapter on Demand product you purchase from this site will come with certain restrictions that allow Wiley to protect the copyrights of its products. After you purchase and download this title, you:
If you have any questions about these restrictions, you may contact Customer Care at (877) 762-2974 (8 a.m. - 5 p.m. EST, Monday - Friday). If you have any issues related to Technical Support, please contact us at 800-762-2974 (United States only) or 317-572-3994 (International) 8 a.m. - 8 p.m. EST, Monday - Friday).
Related Books
Open Source Forums