Home Page Icon
Home Page
Table of Contents for
18. Advanced Internet Topics
Close
18. Advanced Internet Topics
by Mark Lutz
Programming Python, 3rd Edition
Programming Python, 3rd Edition
A Note Regarding Supplemental Files
Foreword
How Time Flies!
Coming Attractions
Concluding Remarks
Foreword to the Second Edition (2001)
Foreword from the First Edition (1996)
Preface
“And Now for Something Completely Different . . . Again”
About This Book
About This Edition
Python Changes
Example Changes
Topic Changes
Focus Unchanged
This Book’s Motivation
This Book’s Structure
This Edition’s Design
It’s Been Updated for Python 2.4 (and 2.5)
It’s Been Reorganized
It Covers New Topics
It’s Still Mostly Platform-Neutral
C integration code platform issues
It’s Still Focused for a More Advanced Audience
It’s Still Example-Oriented
Using the Book’s Examples
The Book Examples Tree
The Book Examples Distribution Package
Running Examples: The Short Story
Running Examples: The Details
Demos and gadgets
Setup requirements
Web-based examples
Top-level programs
Conventions Used in This Book
Safari® Enabled
Where to Look for Updates
Contacting O’Reilly
Using Code Examples
Acknowledgments
I. The Beginning
1. Introducing Python
1.1. “And Now for Something Completely Different”
1.2. Python Philosophy 101
1.3. The Life of Python
1.4. Signs of the Python Times
1.4.1. Recent Growth (As of 2005, at Least)
1.5. The Compulsory Features List
1.6. What’s Python Good For?
1.7. What’s Python Not Good For?
1.8. Truth in Advertising
2. A Sneak Preview
2.1. “Programming Python: The Short Story”
2.2. The Task
2.3. Step 1: Representing Records
2.3.1. Using Lists
2.3.1.1. A database list
2.3.1.2. Field labels
2.3.2. Using Dictionaries
2.3.2.1. Other ways to make dictionaries
2.3.2.2. Lists of dictionaries
2.3.2.3. Nested structures
2.3.2.4. Dictionaries of dictionaries
2.4. Step 2: Storing Records Persistently
2.4.1. Using Formatted Files
2.4.1.1. Test data script
2.4.1.2. Data format script
2.4.1.3. Utility scripts
2.4.2. Using Pickle Files
2.4.3. Using Per-Record Pickle Files
2.4.4. Using Shelves
2.5. Step 3: Stepping Up to OOP
2.5.1. Using Classes
2.5.2. Adding Behavior
2.5.3. Adding Inheritance
2.5.4. Refactoring Code
2.5.4.1. Augmenting methods
2.5.4.2. Display format
2.5.4.3. Constructor customization
2.5.4.4. Alternative classes
2.5.5. Adding Persistence
2.5.6. Other Database Options
2.6. Step 4: Adding Console Interaction
2.6.1. A Console Shelve Interface
2.7. Step 5: Adding a GUI
2.7.1. GUI Basics
2.7.2. Using OOP for GUIs
2.7.3. Getting Input from a User
2.7.4. A GUI Shelve Interface
2.7.4.1. Coding the GUI
2.7.4.2. Using the GUI
2.7.4.3. Future directions
2.8. Step 6: Adding a Web Interface
2.8.1. CGI Basics
2.8.2. Running a Web Server
2.8.3. Using Query Strings and urllib
2.8.4. Formatting Reply Text
2.8.5. A Web-Based Shelve Interface
2.8.5.1. Coding the web site
2.8.5.2. Using the web site
2.8.5.3. Future directions
2.9. The End of the Demo
II. System Programming
3. System Tools
3.1. “The os.path to Knowledge”
3.1.1. Why Python Here?
3.1.2. The Next Five Chapters
3.2. System Scripting Overview
3.2.1. Python System Modules
3.2.2. Module Documentation Sources
3.2.3. Paging Documentation Strings
3.2.4. Introducing String Methods
3.2.5. File Operation Basics
3.2.6. Using Programs in Two Ways
3.2.7. Python Library Manuals
3.2.8. Commercially Published References
3.3. Introducing the sys Module
3.3.1. Platforms and Versions
3.3.2. The Module Search Path
3.3.3. The Loaded Modules Table
3.3.4. Exception Details
3.3.5. Other sys Module Exports
3.4. Introducing the os Module
3.4.1. The Big os Lists
3.4.2. Administrative Tools
3.4.3. Portability Constants
3.4.4. Basic os.path Tools
3.4.5. Running Shell Commands from Scripts
3.4.5.1. What’s a shell command?
3.4.5.2. Running shell commands
3.4.5.3. Communicating with shell commands
3.4.5.4. Shell command limitations
3.4.6. Other os Module Exports
3.5. Script Execution Context
3.6. Current Working Directory
3.6.1. CWD, Files, and Import Paths
3.6.2. CWD and Command Lines
3.7. Command-Line Arguments
3.8. Shell Environment Variables
3.8.1. Fetching Shell Variables
3.8.2. Changing Shell Variables
3.8.3. Shell Variable Details
3.9. Standard Streams
3.9.1. Redirecting Streams to Files and Programs
3.9.1.1. Redirecting streams to files
3.9.1.2. Chaining programs with pipes
3.9.1.3. Coding alternatives
3.9.1.4. Redirected streams and user interaction
3.9.1.5. Reading keyboard input
3.9.2. Redirecting Streams to Python Objects
3.9.3. The StringIO Module
3.9.4. Capturing the stderr Stream
3.9.5. Redirection Syntax in Print Statements
3.9.6. Other Redirection Options
4. File and Directory Tools
4.1. “Erase Your Hard Drive in Five Easy Steps!”
4.2. File Tools
4.2.1. Built-In File Objects
4.2.1.1. Output files
4.2.1.1.1. Opening
4.2.1.1.2. Writing
4.2.1.1.3. Closing
4.2.1.2. Input files
4.2.1.3. Reading lines with file iterators
4.2.1.4. Other file object modes
4.2.1.5. Binary datafiles
4.2.1.6. End-of-line translations on Windows
4.2.1.7. Parsing packed binary data with the struct module
4.2.2. File Tools in the os Module
4.2.2.1. Open mode flags
4.2.2.2. Other os file tools
4.2.3. File Scanners
4.3. Directory Tools
4.3.1. Walking One Directory
4.3.1.1. Running shell listing commands with os.popen
4.3.1.2. The glob module
4.3.1.3. The os.listdir call
4.3.1.4. Splitting and joining listing results
4.3.2. Walking Directory Trees
4.3.2.1. The os.path.walk visitor
4.3.2.2. The os.walk generator
4.3.2.3. Recursive os.listdir traversals
4.3.3. Rolling Your Own find Module
5. Parallel System Tools
5.1. “Telling the Monkeys What to Do”
5.2. Forking Processes
5.2.1. The fork/exec Combination
5.2.1.1. os.exec call formats
5.2.1.2. Spawned child program
5.3. Threads
5.3.1. The thread Module
5.3.1.1. Synchronizing access to global objects
5.3.1.2. Waiting for spawned thread exits
5.3.2. The threading Module
5.3.3. The Queue Module
5.3.4. The Global Interpreter Lock and Threads
5.3.4.1. The thread switch interval
5.3.4.2. Atomic operations
5.3.4.3. C API thread considerations
5.4. Program Exits
5.4.1. os Module Exits
5.4.2. Exit Status Codes
5.4.3. Process Exit Status
5.4.4. Thread Exits
5.5. Interprocess Communication
5.6. Pipes
5.6.1. Anonymous Pipe Basics
5.6.2. Bidirectional IPC with Pipes
5.6.2.1. Deadlocks, flushes, and unbuffered streams
5.6.3. Named Pipes (Fifos)
5.7. Signals
5.8. Other Ways to Start Programs
5.8.1. The os.spawn Calls
5.8.2. Launching Programs on Windows
5.8.2.1. Using the DOS start command
5.8.2.2. Using start in Python scripts
5.8.2.3. The os.startfile call
5.8.3. Other Program Launch Options
5.9. A Portable Program-Launch Framework
5.10. Other System Tools
6. System Examples: Utilities
6.1. “Splits and Joins and Alien Invasions”
6.2. Splitting and Joining Files
6.2.1. Splitting Files Portably
6.2.1.1. Operation modes
6.2.1.2. Binary file access
6.2.1.3. Manually closing files
6.2.2. Joining Files Portably
6.2.2.1. Reading by blocks or files
6.2.2.2. Sorting filenames
6.2.3. Usage Variations
6.3. Generating Forward-Link Web Pages
6.3.1. Page Template File
6.3.2. Page Generator Script
6.4. A Regression Test Script
6.5. Packing and Unpacking Files
6.5.1. Packing Files “++”
6.5.2. Application Hierarchy Superclasses
6.5.2.1. StreamApp: adding stream redirection
6.5.2.2. App: the root class
6.5.2.3. Why use classes here?
6.6. Automated Program Launchers
6.6.1. Launcher Module Clients
6.6.2. Launching Programs Without Environment Settings
6.6.3. Launching Web Browsers Portably
6.6.3.1. Launching browsers with command lines
6.6.3.2. Launching browsers with function calls
6.6.3.3. Viewing multimedia in browsers
6.6.4. A Portable Media File Player Tool
6.6.4.1. The Python webbrowser module
6.6.4.2. The Python mimetypes module
7. System Examples: Directories
7.1. “The Greps of Wrath”
7.2. Fixing DOS Line Ends
7.2.1. Converting Line Ends in One File
7.2.1.1. Slinging bytes and verifying results
7.2.1.2. Nonintrusive conversions
7.2.1.3. Slicing strings out of bounds
7.2.1.4. Binary file mode revisited
7.2.2. Converting Line Ends in One Directory
7.2.3. Converting Line Ends in an Entire Tree
7.2.3.1. The view from the top
7.3. Fixing DOS Filenames
7.3.1. Rewriting with os.path.walk
7.4. Searching Directory Trees
7.4.1. Greps and Globs in Shells and Python
7.4.1.1. Cleaning up bytecode files
7.4.2. A Python Tree Searcher
7.5. Visitor: Walking Trees Generically
7.5.1. Editing Files in Directory Trees
7.5.2. Global Replacements in Directory Trees
7.5.3. Collecting Matched Files in Trees
7.5.3.1. Suppressing status messages
7.5.4. Recoding Fixers with Visitors
7.5.5. Fixing File Permissions in Trees
7.5.6. Changing Unix Executable Path Lines
7.5.7. Summary: Counting Source Lines Four Ways
7.6. Copying Directory Trees
7.6.1. A Python Tree Copy Script
7.6.2. Recoding Copies with a Visitor-Based Class
7.7. Deleting Directory Trees
7.7.1. Recoding Deletions for Generality
7.8. Comparing Directory Trees
7.8.1. Finding Directory Differences
7.8.2. Finding Tree Differences
7.8.3. Running the Script
7.8.3.1. Verifying CD backups
7.8.4. Reporting Differences
III. GUI Programming
8. Graphical User Interfaces
8.1. “Here’s Looking at You, Kid”
8.1.1. GUI Programming Topics
8.2. Python GUI Development Options
8.3. Tkinter Overview
8.3.1. Tkinter Pragmatics
8.3.2. Tkinter Documentation
8.3.3. Tkinter Extensions
8.3.4. Tkinter Structure
8.4. Climbing the GUI Learning Curve
8.4.1. “Hello World” in Four Lines (or Less)
8.5. Tkinter Coding Basics
8.5.1. Making Widgets
8.5.2. Geometry Managers
8.5.3. Running GUI Programs
8.5.3.1. Avoiding DOS consoles on Windows
8.6. Tkinter Coding Alternatives
8.6.1. Widget Resizing Basics
8.6.2. Configuring Widget Options and Window Titles
8.6.3. One More for Old Times’ Sake
8.6.4. Packing Widgets Without Saving Them
8.7. Adding Buttons and Callbacks
8.7.1. Widget Resizing Revisited: Expansion
8.8. Adding User-Defined Callback Handlers
8.8.1. Lambda Callback Handlers
8.8.1.1. Passing in values with default arguments
8.8.1.2. Passing in values with enclosing scope references
8.8.1.3. Enclosing scopes versus defaults
8.8.2. Bound Method Callback Handlers
8.8.3. Callable Class Object Callback Handlers
8.8.4. Other Tkinter Callback Protocols
8.8.5. Binding Events
8.9. Adding Multiple Widgets
8.9.1. Widget Resizing Revisited: Clipping
8.9.2. Attaching Widgets to Frames
8.9.3. Layout: Packing Order and Side Attachments
8.9.4. The Packer’s Expand and Fill Revisited
8.9.5. Using Anchor to Position Instead of Stretch
8.10. Customizing Widgets with Classes
8.11. Reusable GUI Components with Classes
8.11.1. Attaching Class Components
8.11.2. Extending Class Components
8.11.3. Standalone Container Classes
8.12. The End of the Tutorial
8.13. Python/Tkinter for Tcl/Tk Converts
9. A Tkinter Tour, Part 1
9.1. “Widgets and Gadgets and GUIs, Oh My!”
9.1.1. This Chapter’s Topics
9.2. Configuring Widget Appearance
9.3. Top-Level Windows
9.3.1. Toplevel and Tk Widgets
9.3.2. Top-Level Window Protocols
9.4. Dialogs
9.4.1. Standard (Common) Dialogs
9.4.1.1. A “smart” and reusable Quit button
9.4.1.2. A dialog demo launcher bar
9.4.1.3. Printing dialog results (and passing callback data with lambdas)
9.4.1.4. Letting users select colors on the fly
9.4.1.5. Other standard dialog calls
9.4.2. The Old-Style Dialog Module
9.4.3. Custom Dialogs
9.4.3.1. Making custom dialogs modal
9.4.3.2. Other ways to be modal
9.5. Binding Events
9.6. Message and Entry
9.6.1. Message
9.6.2. Entry
9.6.2.1. Programming Entry widgets
9.6.2.2. Laying out input forms
9.6.2.3. Going modal again
9.6.2.4. Tkinter “variables”
9.7. Checkbutton, Radiobutton, and Scale
9.7.1. Checkbuttons
9.7.1.1. Check buttons and variables
9.7.2. Radio Buttons
9.7.2.1. Radio buttons and variables
9.7.2.2. Radio buttons without variables
9.7.2.3. Hold onto your variables
9.7.3. Scales (Sliders)
9.7.3.1. Scales and variables
9.8. Running GUI Code Three Ways
9.8.1. Attaching Frames
9.8.2. Independent Windows
9.8.3. Running Programs
9.8.3.1. Cross-program communication
9.8.3.2. Coding for reusability
9.9. Images
9.9.1. Fun with Buttons and Pictures
9.10. Viewing and Processing Images with PIL
9.10.1. PIL Basics
9.10.2. Displaying Other Image Types with PIL
9.10.2.1. Displaying all images in a directory
9.10.3. Creating Image Thumbnails with PIL
9.10.3.1. Performance: saving thumbnail files
9.10.3.2. Layout: gridding and fixed-size widgets
9.10.3.3. Scrolling and canvases
10. A Tkinter Tour, Part 2
10.1. “On Today’s Menu: Spam, Spam, and Spam”
10.2. Menus
10.2.1. Top-Level Window Menus
10.2.2. Frame- and Menubutton-Based Menus
10.2.2.1. Using Menubuttons and Optionmenus
10.2.3. Windows with Both Menus and Toolbars
10.2.3.1. Automating menu construction
10.3. Listboxes and Scrollbars
10.3.1. Programming Listboxes
10.3.2. Programming Scroll Bars
10.3.3. Packing Scroll Bars
10.4. Text
10.4.1. Programming the Text Widget
10.4.1.1. Text is a Python string
10.4.1.2. String positions
10.4.1.2.1. Text indexes
10.4.1.2.2. Text marks
10.4.1.2.3. Text tags
10.4.2. Adding Text-Editing Operations
10.4.2.1. Using the clipboard
10.4.2.2. Composition versus inheritance
10.4.2.3. It’s called “Simple” for a reason
10.4.3. Advanced Text and Tag Operations
10.5. Canvas
10.5.1. Basic Canvas Operations
10.5.2. Programming the Canvas Widget
10.5.2.1. Coordinates
10.5.2.2. Object construction
10.5.2.3. Object identifiers and operations
10.5.2.4. Canvas object tags
10.5.3. Scrolling Canvases
10.5.4. Scrollable Canvases and Image Thumbnails
10.5.4.1. Scrolling images too: PyPhoto (ahead)
10.5.5. Using Canvas Events
10.5.5.1. Binding events on specific items
10.6. Grids
10.6.1. Grid Basics
10.6.2. grid Versus pack
10.6.3. Combining grid and pack
10.6.4. Making Gridded Widgets Expandable
10.6.4.1. Resizing in grids
10.6.4.2. Spanning columns and rows
10.6.5. Laying Out Larger Tables with grid
10.7. Time Tools, Threads, and Animation
10.7.1. Using Threads with GUIs
10.7.2. Using the after Method
10.7.2.1. Hiding and redrawing widgets and windows
10.7.3. Simple Animation Techniques
10.7.3.1. Using time.sleep loops
10.7.3.2. Using widget.after events
10.7.3.3. Using multiple time.sleep loop threads
10.7.4. Other Animation Concepts: Threads and Toolkits
10.8. The End of the Tour
10.9. The PyDemos and PyGadgets Launchers
10.9.1. PyDemos Launcher Bar
10.9.2. PyGadgets Launcher Bar
11. GUI Coding Techniques
11.1. “Building a Better Mouse Trap”
11.2. GuiMixin: Common Tool Mixin Classes
11.3. GuiMaker: Automating Menus and Toolbars
11.3.1. Subclass Protocols
11.3.2. GuiMaker Classes
11.3.3. GuiMaker Self-Test
11.3.4. BigGui: A Client Demo Program
11.4. ShellGui: GUIs for Command-Line Tools
11.4.1. A Generic Shell-Tools Display
11.4.2. Application-Specific Tool Set Classes
11.4.3. Adding GUI Frontends to Command Lines
11.5. GuiStreams: Redirecting Streams to Widgets
11.5.1. Using Redirection for the Packing Scripts
11.6. Reloading Callback Handlers Dynamically
11.7. Wrapping Up Top-Level Window Interfaces
11.8. GUIs, Threads, and Queues
11.8.1. Placing Callbacks on Queues
11.9. More Ways to Add GUIs to Non-GUI Code
11.9.1. Popping up GUI Windows on Demand
11.9.2. Adding a GUI As a Separate Program: Sockets
11.9.3. Adding a GUI As a Separate Program: Pipes
12. Complete GUI Programs
12.1. “Python, Open Source, and Camaros”
12.1.1. Examples in Other Chapters
12.1.2. This Chapter’s Strategy
12.2. PyEdit: A Text Editor Program/Object
12.2.1. Running PyEdit
12.2.1.1. Menus and toolbars
12.2.1.2. Dialogs
12.2.1.3. Running program code
12.2.1.4. New features in version 2.0
12.2.2. PyEdit Source Code
12.3. PyPhoto: An Image Viewer and Resizer
12.3.1. Running PyPhoto
12.3.2. PyPhoto Source Code
12.4. PyView: An Image and Notes Slideshow
12.4.1. Running PyView
12.4.2. PyView Source Code
12.5. PyDraw: Painting and Moving Graphics
12.5.1. Running PyDraw
12.5.2. PyDraw Source Code
12.6. PyClock: An Analog/Digital Clock Widget
12.6.1. A Quick Geometry Lesson
12.6.2. Running PyClock
12.6.3. PyClock Source Code
12.7. PyToe: A Tic-Tac-Toe Game Widget
12.7.1. Running PyToe
12.7.2. PyToe Source Code (Book Examples Distribution)
12.8. Where to Go from Here
IV. Internet Programming
13. Network Scripting
13.1. “Tune In, Log On, and Drop Out”
13.1.1. Internet Scripting Topics
13.1.1.1. What we will cover
13.1.1.2. What we won’t cover
13.1.1.3. Running examples in this part of the book
13.2. Plumbing the Internet
13.2.1. The Socket Layer
13.2.1.1. Machine identifiers
13.2.2. The Protocol Layer
13.2.2.1. Port number rules
13.2.2.2. Clients and servers
13.2.2.3. Protocol structures
13.2.3. Python’s Internet Library Modules
13.3. Socket Programming
13.3.1. Socket Basics
13.3.1.1. Server socket calls
13.3.1.2. Client socket calls
13.3.1.3. Running socket programs locally
13.3.1.4. Running socket programs remotely
13.3.1.5. Socket pragmatics
13.3.1.6. Spawning clients in parallel
13.3.1.7. Talking to reserved ports
13.4. Handling Multiple Clients
13.4.1. Forking Servers
13.4.1.1. Running the forking server
13.4.1.2. Forking processes
13.4.1.3. Exiting from children
13.4.1.4. Killing the zombies
13.4.1.5. Preventing zombies with signal handlers
13.4.2. Threading Servers
13.4.3. Standard Library Server Classes
13.4.4. Third-Party Server Tools: Twisted
13.4.5. Multiplexing Servers with select
13.4.5.1. A select-based echo server
13.4.5.2. Running the select server
13.4.6. Choosing a Server Scheme
13.5. A Simple Python File Server
13.5.1. Running the File Server and Clients
13.5.2. Adding a User-Interface Frontend
13.5.2.1. Using Frames and command lines
13.5.2.2. Using grids and function calls
13.5.2.3. Using a reusable form-layout class
14. Client-Side Scripting
14.1. “Socket to Me!”
14.2. FTP: Transferring Files over the Net
14.2.1. Fetching Files with ftplib
14.2.2. Using urllib to FTP Files
14.2.3. FTP get and put Utilities
14.2.3.1. Download utility
14.2.3.2. Upload utility
14.2.3.3. Playing the Monty Python theme song
14.2.3.4. Adding user interfaces
14.2.4. Downloading Web Sites (Mirrors)
14.2.5. Uploading Web Sites
14.2.6. Refactoring Uploads and Downloads for Reuse
14.2.6.1. Refactoring with functions
14.2.6.2. Refactoring with classes
14.2.7. Uploads and Deletes with Subdirectories
14.2.7.1. Uploading local trees
14.2.7.2. Deleting remote trees
14.3. Processing Internet Email
14.4. POP: Fetching Email
14.4.1. Mail Configuration Module
14.4.2. POP Mail Reader Script
14.4.3. Fetching Messages
14.4.4. Fetching Email at the Interactive Prompt
14.5. SMTP: Sending Email
14.5.1. SMTP Mail Sender Script
14.5.2. Sending Messages
14.5.3. More Ways to Abuse the Net
14.5.4. Back to the Big Internet Picture
14.5.5. Sending Email from the Interactive Prompt
14.6. email: Parsing and Composing Mails
14.6.1. Message Objects
14.6.2. Basic email Interfaces in Action
14.7. pymail: A Console-Based Email Client
14.7.1. Running the pymail Console Client
14.8. The mailtools Utility Package
14.8.1. Initialization File
14.8.2. MailTool Class
14.8.3. MailSender Class
14.8.4. MailFetcher Class
14.8.4.1. General usage
14.8.4.2. Inbox synchronization tools
14.8.5. MailParser Class
14.8.6. Self-Test Script
14.8.7. Updating the pymail Console Client
14.9. NNTP: Accessing Newsgroups
14.10. HTTP: Accessing Web Sites
14.11. Module urllib Revisited
14.11.1. Other urllib Interfaces
14.12. Other Client-Side Scripting Options
15. The PyMailGUI Client
15.1. “Use the Source, Luke”
15.1.1. Source Code Modules
15.1.2. Why PyMailGUI?
15.1.3. Running PyMailGUI
15.1.4. Presentation Strategy
15.1.5. New in This Edition
15.2. A PyMailGUI Demo
15.2.1. Getting Started
15.2.2. Loading Mail
15.2.3. Threading Model
15.2.4. Load Server Interface
15.2.5. Offline Processing with Save and Open
15.2.6. Sending Email and Attachments
15.2.7. Viewing Email and Attachments
15.2.8. Email Replies and Forwards
15.2.9. Deleting Email
15.2.10. POP Message Numbers and Synchronization
15.2.11. Multiple Windows and Status Messages
15.3. PyMailGUI Implementation
15.3.1. Code Reuse
15.3.2. Code Structure
15.3.3. PyMailGui2: The Main Module
15.3.4. SharedNames: Program-Wide Globals
15.3.5. ListWindows: Message List Windows
15.3.6. ViewWindows: Message View Windows
15.3.7. messagecache: Message Cache Manager
15.3.8. popuputil: General-Purpose GUI Pop Ups
15.3.9. wraplines: Line Split Tools
15.3.10. mailconfig: User Configurations
15.3.11. PyMailGuiHelp: User Help Text
15.3.12. Ideas for Improvement
16. Server-Side Scripting
16.1. “Oh What a Tangled Web We Weave”
16.2. What’s a Server-Side CGI Script?
16.2.1. The Script Behind the Curtain
16.2.2. Writing CGI Scripts in Python
16.3. Running Server-Side Examples
16.3.1. Web Server Options
16.3.2. Running a Local Web Server
16.3.3. The Server-Side Examples Root Page
16.3.4. Viewing Server-Side Examples and Output
16.4. Climbing the CGI Learning Curve
16.4.1. A First Web Page
16.4.1.1. HTML basics
16.4.1.2. Internet addresses (URLs)
16.4.1.3. Using minimal URLs
16.4.1.4. HTML file permission constraints
16.4.2. A First CGI Script
16.4.2.1. Installing CGI scripts
16.4.2.2. Finding Python on remote servers
16.4.3. Adding Pictures and Generating Tables
16.4.3.1. Table tags
16.4.4. Adding User Interaction
16.4.4.1. Submission page
16.4.4.2. More on form tags
16.4.4.3. Response script
16.4.4.4. Passing parameters in URLs
16.4.4.5. Testing outside browsers with the module urllib
16.4.5. Using Tables to Lay Out Forms
16.4.5.1. Converting strings in CGI scripts
16.4.5.2. Debugging CGI scripts
16.4.6. Adding Common Input Devices
16.4.7. Changing Input Layouts
16.4.7.1. Keeping display and logic separate
16.4.8. Passing Parameters in Hardcoded URLs
16.4.9. Passing Parameters in Hidden Form Fields
16.5. Saving State Information in CGI Scripts
16.5.1. URL Query Parameters
16.5.2. Hidden Form Input Fields
16.5.3. HTTP “Cookies”
16.5.3.1. Creating a cookie
16.5.3.2. Receiving a cookie
16.5.3.3. Using cookies in CGI scripts
16.5.3.4. Handling cookies with the module urllib2
16.5.4. Server-Side Databases
16.5.5. Extensions to the CGI Model
16.5.6. Combining Techniques
16.6. The Hello World Selector
16.6.1. Checking for Missing and Invalid Inputs
16.7. Refactoring Code for Maintainability
16.7.1. Step 1: Sharing Objects Between Pages—A New Input Form
16.7.2. Step 2: A Reusable Form Mock-Up Utility
16.7.3. Step 3: Putting It All Together—A New Reply Script
16.8. More on HTML and URL Escapes
16.8.1. URL Escape Code Conventions
16.8.2. Python HTML and URL Escape Tools
16.8.3. Escaping HTML Code
16.8.4. Escaping URLs
16.8.5. Escaping URLs Embedded in HTML Code
16.8.5.1. HTML and URL conflicts: &
16.8.5.2. Avoiding conflicts
16.9. Transferring Files to Clients and Servers
16.9.1. Displaying Arbitrary Server Files on the Client
16.9.1.1. Handling private files and errors
16.9.2. Uploading Client Files to the Server
16.9.2.1. Handling client path formats
16.9.3. More Than One Way to Push Bits over the Net
17. The PyMailCGI Server
17.1. “Things to Do When Visiting Chicago”
17.2. The PyMailCGI Web Site
17.2.1. Implementation Overview
17.2.2. New in This Edition
17.2.3. Presentation Overview
17.2.4. Running This Chapter’s Examples
17.3. The Root Page
17.3.1. Configuring PyMailCGI
17.4. Sending Mail by SMTP
17.4.1. The Message Composition Page
17.4.2. The Send Mail Script
17.4.3. Error Pages
17.4.4. Common Look-and-Feel
17.4.5. Using the Send Mail Script Outside a Browser
17.5. Reading POP Email
17.5.1. The POP Password Page
17.5.2. The Mail Selection List Page
17.5.3. Passing State Information in URL Link Parameters
17.5.4. Security Protocols
17.5.4.1. Reading mail with direct URLs
17.5.5. The Message View Page
17.5.6. Passing State Information in HTML Hidden Input Fields
17.5.7. Escaping Mail Text and Passwords in HTML
17.6. Processing Fetched Mail
17.6.1. Reply and Forward
17.6.2. Delete
17.6.3. Deletions and POP Message Numbers
17.6.3.1. Inbox synchronization error potential
17.6.3.2. Passing header text in hidden input fields (PyMailCGI_2.1)
17.6.3.3. Server-side files for headers
17.6.3.4. Delete on load
17.7. Utility Modules
17.7.1. External Components and Configuration
17.7.2. POP Mail Interface
17.7.3. POP Password Encryption
17.7.3.1. Manual data encryption: rotor (defunct)
17.7.3.2. Manual data encryption: PyCrypto
17.7.3.3. Secure HTTP transmissions
17.7.3.4. Secure cookies
17.7.3.5. The secret.py module
17.7.3.6. Rolling your own encryptor
17.7.4. Common Utilities Module
17.8. CGI Script Trade-Offs
17.8.1. Other Approaches
18. Advanced Internet Topics
18.1. “Surfing on the Shoulders of Giants”
18.2. Zope: A Web Application Framework
18.2.1. Zope Overview
18.2.1.1. Zope hierarchy model
18.2.1.2. Zope scripting
18.2.1.3. Zope components
18.2.2. Zope Object Publishing
18.2.3. A Zope External Method
18.2.3.1. Calling through the Web
18.2.3.2. Calling from other objects
18.2.4. A Simple Zope Interactive Web Site
18.3. HTMLgen: Web Pages from Objects
18.3.1. A Brief HTMLgen Tutorial
18.4. Jython: Python for Java
18.4.1. A Quick Introduction to Jython
18.4.2. Why Jython?
18.4.3. A Simple Jython Example
18.4.4. Interface Automation Tricks
18.4.5. Writing Java Applets in Jython
18.4.6. Jython Trade-Offs
18.4.7. Picking Your Python
18.5. Grail: A Python-Based Web Browser
18.5.1. A Simple Grail Applet Example
18.6. XML Processing Tools
18.6.1. A Brief Introduction to XML Parsing
18.7. Windows Web Scripting Extensions
18.7.1. Active Scripting: Client-Side Embedding
18.7.1.1. Active Scripting basics
18.7.1.2. Embedding Python in HTML
18.7.2. Active Server Pages: Server-Side Embedding
18.7.2.1. A short ASP example
18.7.3. The COM Connection
18.7.3.1. A brief introduction to COM
18.7.3.2. Python COM clients
18.7.3.3. Python COM servers
18.7.3.3.1. Using the Python server from a Python client
18.7.3.3.2. Using the Python server from a Visual Basic client
18.7.3.3.3. Using the Python server with client-side Active Scripting
18.7.3.4. The bigger COM picture: DCOM
18.8. Python Server Pages
18.8.1. PSP in Webware and mod_python
18.9. Rolling Your Own Servers in Python
18.9.1. Standard Library Socket Servers
18.9.2. Standard Library Web Servers
18.9.3. Third-Party Solutions
18.10. And Other Cool Stuff
V. Tools and Techniques
19. Databases and Persistence
19.1. “Give Me an Order of Persistence, but Hold the Pickles”
19.2. Persistence Options in Python
19.3. DBM Files
19.3.1. Using DBM Files
19.4. Pickled Objects
19.4.1. Using Object Pickling
19.4.2. Picking in Action
19.4.3. Pickler Protocols and cPickle
19.5. Shelve Files
19.5.1. Using Shelves
19.5.2. Storing Built-In Object Types in Shelves
19.5.3. Storing Class Instances in Shelves
19.5.4. Changing Classes of Objects Stored in Shelves
19.5.5. Shelve Constraints
19.5.5.1. Keys must be strings
19.5.5.2. Objects are unique only within a key
19.5.5.3. Updates must treat shelves as fetch-modify-store mappings
19.5.5.4. Concurrent updates are not directly supported
19.5.5.5. Underlying DBM format portability
19.5.6. Pickled Class Constraints
19.5.7. Other Shelve Limitations
19.6. The ZODB Object-Oriented Database
19.6.1. A ZODB Tutorial
19.6.1.1. Installing ZODB
19.6.1.2. The ZEO distributed object server
19.6.1.3. Creating a ZODB database
19.6.1.4. Fetching and changing
19.6.2. Using Classes with ZODB
19.6.3. A ZODB People Database
19.6.4. ZODB Resources
19.7. SQL Database Interfaces
19.7.1. SQL Interface Overview
19.7.2. An SQL Database API Tutorial
19.7.2.1. The MySQL system
19.7.2.2. Installation
19.7.2.3. Getting started
19.7.2.4. Making databases and tables
19.7.2.5. Adding records
19.7.2.6. Running queries
19.7.2.7. Running updates
19.7.3. Building Record Dictionaries
19.7.3.1. Using table descriptions
19.7.3.2. Record dictionaries
19.7.3.3. Automating with scripts and modules
19.7.4. Tying the Pieces Together
19.7.5. Loading Database Tables from Files
19.7.5.1. Loading with SQL and Python
19.7.5.2. Python versus SQL
19.7.6. SQL Utility Scripts
19.7.6.1. Table load scripts
19.7.6.2. Table display script
19.7.6.3. Using the scripts
19.7.7. SQL Resources
19.8. PyForm: A Persistent Object Viewer
19.8.1. Processing Shelves with Code
19.8.2. Adding a Graphical Interface
19.8.3. PyForm GUI Implementation
19.8.4. PyForm Table Wrappers
19.8.5. PyForm Creation and View Utility Scripts
19.8.5.1. Creating and browsing custom databases
19.8.6. Data as Code
19.8.7. Browsing Other Kinds of Objects with PyForm
19.8.8. Browsing Other Kinds of Databases with PyForm
19.8.9. PyForm Limitations
20. Data Structures
20.1. “Roses Are Red, Violets Are Blue; Lists Are Mutable, and So Is Set Foo”
20.2. Implementing Stacks
20.2.1. A Stack Module
20.2.2. A Stack Class
20.2.3. Customization: Performance Monitors
20.2.4. Optimization: Tuple Tree Stacks
20.2.5. Optimization: In-Place List Modifications
20.2.6. Timing the Improvements
20.2.6.1. Results under Python 1.5.2
20.2.6.2. Results under Python 2.4
20.3. Implementing Sets
20.3.1. Set Functions
20.3.1.1. Supporting multiple operands
20.3.2. Set Classes
20.3.3. Optimization: Moving Sets to Dictionaries
20.3.3.1. Timing the results under Python 2.4
20.3.3.2. Timing results under Python 1.5.2: version skew
20.3.3.3. Using the Python profiler
20.3.4. Optimizing fastset by Coding Techniques (or Not)
20.3.5. Adding Relational Algebra to Sets (External)
20.4. Subclassing Built-In Types
20.5. Binary Search Trees
20.6. Graph Searching
20.6.1. Moving Graphs to Classes
20.7. Reversing Sequences
20.8. Permuting Sequences
20.9. Sorting Sequences
20.9.1. Adding Comparison Functions
20.10. Data Structures Versus Python Built-Ins
20.11. PyTree: A Generic Tree Object Viewer
20.11.1. Running PyTree
20.11.2. PyTree Source Code
20.11.2.1. Tree-independent GUI implementation
20.11.2.2. Tree wrappers and test widgets
20.11.3. PyTree Does Parse Trees Too
21. Text and Language
21.1. “See Jack Hack. Hack, Jack, Hack”
21.2. Strategies for Parsing Text in Python
21.3. String Method Utilities
21.3.1. Templating with Replacements and Formats
21.3.2. Parsing with Splits and Joins
21.3.3. Summing Columns in a File
21.3.4. Parsing and Unparsing Rule Strings
21.3.5. More on the holmes Expert System Shell
21.4. Regular Expression Pattern Matching
21.4.1. First Examples
21.4.2. Using the re Module
21.4.2.1. Module functions
21.4.2.2. Compiled pattern objects
21.4.2.3. Match objects
21.4.2.4. Regular expression patterns
21.4.3. Basic Patterns
21.4.4. Scanning C Header Files for Patterns
21.4.5. A File Pattern Search Utility
21.5. Advanced Language Tools
21.6. Handcoded Parsers
21.6.1. The Expression Grammar
21.6.2. The Parser’s Code
21.6.3. Adding a Parse Tree Interpreter
21.6.4. Parse Tree Structure
21.6.5. Exploring Parse Trees with PyTree
21.6.6. Parsers Versus Python
21.7. PyCalc: A Calculator Program/Object
21.7.1. A Simple Calculator GUI
21.7.1.1. Building the GUI
21.7.1.2. Running code strings
21.7.1.3. Extending and attaching
21.7.2. PyCalc—A Real Calculator GUI
21.7.2.1. Running PyCalc
21.7.2.2. Evaluating expressions with stacks
21.7.2.3. PyCalc source code
21.7.2.4. Using PyCalc as a component
21.7.2.5. Adding new buttons in new components
VI. Integration
22. Extending Python
22.1. “I Am Lost at C”
22.2. Integration Modes
22.2.1. Presentation Notes
22.3. C Extensions Overview
22.4. A Simple C Extension Module
22.5. Extension Module Details
22.5.1. Compilation and Linking
22.5.1.1. Dynamic binding
22.5.1.2. Static binding
22.5.1.3. Static versus dynamic binding
22.5.2. Compiling with the Distutils System
22.5.3. Anatomy of a C Extension Module
22.5.4. Data Conversions
22.5.4.1. Python to C: using Python argument lists
22.5.4.2. Python to C: using Python return values
22.5.4.3. C to Python: returning values to Python
22.5.4.4. Common conversion codes
22.5.5. Error Handling
22.5.5.1. Raising Python exceptions in C
22.5.5.2. Detecting errors that occur in Python
22.5.6. Reference Counts
22.5.7. Other Extension Tasks: Threads
22.6. The SWIG Integration Code Generator
22.6.1. A Simple SWIG Example
22.6.2. SWIG Details
22.7. Wrapping C Environment Calls
22.7.1. Adding Wrapper Classes to Flat Libraries
22.7.2. But Don’t Do That Either—SWIG
22.8. A C Extension Module String Stack
22.8.1. But Don’t Do That Either—SWIG
22.9. A C Extension Type String Stack
22.9.1. Anatomy of a C Extension Type
22.9.2. Compiling and Running
22.9.3. Timing the C Implementations
22.9.4. Older Timing Results
22.9.5. But Don’t Do That Either—SWIG
22.10. Wrapping C++ Classes with SWIG
22.10.1. A Simple C++ Extension Class
22.10.2. Wrapping the C++ Class with SWIG
22.10.3. Using the C++ Class in Python
22.10.3.1. Using the low-level extension module
22.10.3.2. Subclassing the C++ class in Python
22.10.3.3. Exploring the wrappers interactively
22.11. Other Extending Tools
23. Embedding Python
23.1. “Add Python. Mix Well. Repeat.”
23.2. C Embedding API Overview
23.2.1. What Is Embedded Code?
23.3. Basic Embedding Techniques
23.3.1. Running Simple Code Strings
23.3.1.1. Compiling and running
23.3.2. Running Code Strings with Results and Namespaces
23.3.3. Calling Python Objects
23.3.4. Running Strings in Dictionaries
23.3.5. Precompiling Strings to Bytecode
23.4. Registering Callback Handler Objects
23.5. Using Python Classes in C
23.6. A High-Level Embedding API: ppembed
23.6.1. Running Objects with ppembed
23.6.2. Running Code Strings with ppembed
23.6.3. Running Customizable Validations
23.6.3.1. Running function-based validations
23.6.3.2. Other validation components
23.6.4. ppembed Implementation
23.6.5. Other Integration Examples (External)
23.7. Other Integration Topics
23.7.1. Jython: Java Integration
23.7.2. IronPython: C#/.NET Integration
23.7.3. COM Integration on Windows
23.7.4. CORBA Integration
23.7.5. Other Languages
23.7.6. Network-Based Integration Protocols
23.7.7. Integration Versus Optimization
23.7.7.1. Framework roles
23.7.7.2. Extension module roles
23.7.7.3. Picking an integration technology
VIII. The End
24. Conclusion: Python and the Development Cycle
24.1. “That’s the End of the Book, Now Here’s the Meaning of Life”
24.2. “Something’s Wrong with the Way We Program Computers”
24.3. The “Gilligan Factor”
24.4. Doing the Right Thing
24.4.1. The Static Language Build Cycle
24.4.2. Artificial Complexities
24.4.3. One Language Does Not Fit All
24.5. Enter Python
24.6. But What About That Bottleneck?
24.6.1. Python Provides Immediate Turnaround
24.6.2. Python Is “Executable Pseudocode”
24.6.3. Python Is OOP Done Right
24.6.4. Python Fosters Hybrid Applications
24.7. On Sinking the Titanic
24.8. So What’s “Python: The Sequel”?
24.9. In the Final Analysis . . .
24.10. Postscript to the Second Edition (2000)
24.10.1. Integration Isn’t Everything
24.10.2. The End of the Java Wars
24.10.3. We’re Not Off That Island Yet
24.11. Postscript to the Third Edition (2006)
24.11.1. Proof of Concept
24.11.2. Integration Today
24.11.3. Quality Counts
Index
Colophon
Copyright
Search in book...
Toggle Font Controls
Playlists
Add To
Create new playlist
Name your new playlist
Playlist description (optional)
Cancel
Create playlist
Sign In
Email address
Password
Forgot Password?
Create account
Login
or
Continue with Facebook
Continue with Google
Sign Up
Full Name
Email address
Confirm Email Address
Password
Login
Create account
or
Continue with Facebook
Continue with Google
Prev
Previous Chapter
17.8. CGI Script Trade-Offs
Next
Next Chapter
18.1. “Surfing on the Shoulders of Giants”
Chapter 18. Advanced Internet Topics
Add Highlight
No Comment
..................Content has been hidden....................
You can't read the all page of ebook, please click
here
login for view all page.
Day Mode
Cloud Mode
Night Mode
Reset