Anda di halaman 1dari 13

pythonhtp1_01TOC.

fm Page viii Monday, January 14, 2002 12:20 PM

Contents

Preface xxxvii

1 Introduction to Computers, Internet and World Wide Web


1
1.1 Introduction 2
1.2 What Is a Computer? 3
1.3 Computer Organization 4
1.4 Evolution of Operating Systems 5
1.5 Personal Computing, Distributed Computing and Client/Server Computing 6
1.6 Machine Languages, Assembly Languages and High-Level Languages 6
1.7 Structured Programming 7
1.8 Object-Oriented Programming 8
1.9 Hardware Trends 9
1.10 History of the Internet and World Wide Web 10
1.11 World Wide Web Consortium (W3C) 12
1.12 Extensible Markup Language (XML) 12
1.13 Open-Source Software Revolution 13
1.14 History of Python 14
1.15 Python Modules 15
1.16 General Notes about Python and This Book 15
1.17 Tour of the Book 15
1.18 Internet and World Wide Web Resources 27

2 Introduction to Python Programming 34


2.1 Introduction 35
2.2 First Program in Python: Printing a Line of Text 35
2.3 Modifying our First Python Program 38

© Copyright 2002 by Prentice Hall. All Rights Reserved.


pythonhtp1_01TOC.fm Page ix Monday, January 14, 2002 12:20 PM

Contents IX

2.3.1 Displaying a Single Line of Text with Multiple Statements 38


2.3.2 Displaying Multiple Lines of Text with a Single Statement 39
2.4 Another Python Program: Adding Integers 40
2.5 Memory Concepts 42
2.6 Arithmetic 45
2.7 String Formatting 50
2.8 Decision Making: Equality and Relational Operators 54
2.9 Indentation 58
2.10 Thinking About Objects: Introduction to Object Technology 59

3 Control Structures 68
3.1 Introduction 69
3.2 Algorithms 69
3.3 Pseudocode 70
3.4 Control Structures 70
3.5 if Selection Structure 73
3.6 if/else and if/elif/else Selection Structures 74
3.7 while Repetition Structure 79
3.8 Formulating Algorithms: Case Study 1
(Counter-Controlled Repetition)81
3.9 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 2
(Sentinel-Controlled Repetition)83
3.10 Formulating Algorithms with Top-Down, Stepwise Refinement: Case Study 3
(Nested Control Structures)88
3.11 Augmented Assignment Symbols 92
3.12 Essentials of Counter-Controlled Repetition 93
3.13 for Repetition Structure 94
3.14 Using the for Repetition Structure 97
3.15 break and continue Statements 100
3.16 Logical Operators 102
3.17 Structured-Programming Summary 106

4 Functions 118
4.1 Introduction 119
4.2 Program Components in Python 119
4.3 Functions 121
4.4 Module math Functions 121
4.5 Function Definitions 123
4.6 Random-Number Generation 127
4.7 Example: A Game of Chance 129
4.8 Scope Rules 131
4.9 Keyword import and Namespaces 135
4.9.1 Importing One or More Modules 135
4.9.2 Importing Identifiers from a Module 136
4.9.3 Binding Names for Modules and Module Identifiers 138
4.10 Recursion 139
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page x Monday, January 14, 2002 12:20 PM

X Contents

4.11 Example Using Recursion: The Fibonacci Series 141


4.12 Recursion vs. Iteration 144
4.13 Default Arguments 145
4.14 Keyword Arguments 146

5 Lists, Tuples and Dictionaries 155


5.1 Introduction 156
5.2 Sequences 156
5.3 Creating Sequences 158
5.4 Using Lists and Tuples 159
5.4.1 Using Lists 160
5.4.2 Using Tuples 163
5.4.3 Sequence Unpacking 165
5.4.4 Sequence Slicing 166
5.5 Dictionaries 169
5.6 List and Dictionary Methods 171
5.7 References and Reference Parameters 177
5.8 Passing Lists to Functions 177
5.9 Sorting and Searching Lists 179
5.10 Multiple-Subscripted Sequences 181

6 Introduction to the Common Gateway Interface (CGI)


193
6.1 Introduction 194
6.2 Client and Web Server Interaction 195
6.2.1 System Architecture 195
6.2.2 Accessing Web Servers 196
6.2.3 HTTP Transactions 197
6.3 Simple CGI Script 199
6.4 Sending Input to a CGI Script 206
6.5 Using XHTML Forms to Send Input and Using Module cgi to Retrieve Form Data
208
6.6 Using cgi.FieldStorage to Read Input 213
6.7 Other HTTP Headers 214
6.8 Example: Interactive Portal 215
6.9 Internet and World Wide Web Resources 219

7 Object-Based Programming 225


7.1 Introduction 226
7.2 Implementing a Time Abstract Data Type with a Class 227
7.3 Special Attributes 231
7.4 Controlling Access to Attributes 233
7.4.1 Get and Set Methods 233
7.4.2 Private Attributes 240
7.5 Using Default Arguments With Constructors 242
7.6 Destructors 246
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xi Monday, January 14, 2002 12:20 PM

Contents XI

7.7 Class Attributes 246


7.8 Composition: Object References as Members of Classes 248
7.9 Data Abstraction and Information Hiding 251
7.10 Software Reusability 253

8 Customizing Classes 260


8.1 Introduction 261
8.2 Customizing String Representation: Method __str__ 262
8.3 Customizing Attribute Access 264
8.4 Operator Overloading 267
8.5 Restrictions on Operator Overloading 268
8.6 Overloading Unary Operators 270
8.7 Overloading Binary Operators 270
8.8 Overloading Built-in Functions 271
8.9 Converting Between Types 272
8.10 Case Study: A Rational Class 273
8.11 Overloading Sequence Operations 280
8.12 Case Study: A SingleList Class 281
8.13 Overloading Mapping Operations 286
8.14 Case Study: A SimpleDictionary Class 287

9 Object-Oriented Programming: Inheritance 296


9.1 Introduction 297
9.2 Inheritance: Base Classes and Derived Classes 298
9.3 Creating Base Classes and Derived Classes 301
9.4 Overriding Base-Class Methods in a Derived Class 304
9.5 Software Engineering with Inheritance 306
9.6 Composition vs. Inheritance 307
9.7 “Uses A” and “Knows A” Relationships 307
9.8 Case Study: Point, Circle, Cylinder 308
9.9 Abstract Base Classes and Concrete Classes 312
9.10 Case Study: Inheriting Interface and Implementation 313
9.11 Polymorphism 317
9.12 Classes and Python 2.2 319
9.12.1 Static Methods 319
9.12.2 Inheriting from Built-in Types 322
9.12.3 __getattribute__ Method 327
9.12.4 __slots__ Class Attribute 330
9.12.5 Properties 333

10 Graphical User Interface Components: Part 1 342


10.1 Introduction 343
10.2 Tkinter Overview 345
10.3 Simple Tkinter Example: Label Component 346
10.4 Event Handling Model 349
10.5 Entry Component 350
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xii Monday, January 14, 2002 12:20 PM

XII Contents

10.6 Button Component 354


10.7 Checkbutton and Radiobutton Components 356
10.8 Mouse Event Handling 361
10.9 Keyboard Event Handling 366
10.10 Layout Managers 368
10.10.1 Pack 369
10.10.2 Grid 372
10.10.3 Place 375
10.11 Card Shuffling and Dealing Simulation 376
10.12 Internet and World Wide Web Resources 379

11 Graphical User Interface Components: Part 2 388


11.1 Introduction 389
11.2 Overview of Pmw 389
11.3 ScrolledListbox Component 390
11.4 ScrolledText Component 392
11.5 MenuBar Component 395
11.6 Popup Menus 399
11.7 Canvas Component 401
11.8 Scale Component 403
11.9 Other GUI Toolkits 405

12 Exception Handling 411


12.1 Introduction 412
12.2 Raising an Exception 412
12.3 Exception-Handling Overview 413
12.4 Example: DivideByZeroError 416
12.5 Python Exception Hierarchy 419
12.6 finally Clause 422
12.7 Exception Objects and Tracebacks 425
12.8 Programmer-Defined Exception Classes 429

13 String Manipulation and Regular Expressions 436


13.1 Introduction 437
13.2 Fundamentals of Characters and Strings 437
13.3 String Presentation 441
13.4 Searching Strings 442
13.5 Joining and Splitting Strings 444
13.6 Regular Expressions 445
13.7 Compiling Regular Expressions and Manipulating Regular-Expression Objects
447
13.8 Regular-Expression Repetition and Placement Characters 448
13.9 Classes and Special Sequences 450
13.10 Regular Expression String-Manipulation Functions 453
13.11 Grouping 454
13.12 Internet and World Wide Web Resources 456
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xiii Monday, January 14, 2002 12:20 PM

Contents XIII

14 File Processing and Serialization 462


14.1 Introduction 463
14.2 Data Hierarchy 463
14.3 Files and Streams 465
14.4 Creating a Sequential-Access File 466
14.5 Reading Data from a Sequential-Access File 470
14.6 Updating Sequential-Access Files 475
14.7 Random-Access Files 475
14.8 Simulating a Random-Access File: The shelve Module 476
14.9 Writing Data to a shelve File 476
14.10 Retrieving Data from a shelve File 478
14.11 Example: A Transaction-Processing Program 479
14.12 Object Serialization 484

15 Extensible Markup Language (XML) 491


15.1 Introduction 492
15.2 XML Documents 492
15.3 XML Namespaces 497
15.4 Document Object Model (DOM) 500
15.5 Simple API for XML (SAX) 500
15.6 Document Type Definitions (DTDs), Schemas and Validation 501
15.6.1 Document Type Definition Documents 502
15.6.2 W3C XML Schema Documents 506
15.7 XML Vocabularies 509
15.7.1 MathML™ 510
15.7.2 Chemical Markup Language (CML) 514
15.7.3 Other XML Vocabularies 516
15.8 Extensible Stylesheet Language (XSL) 516
15.9 Internet and World Wide Web Resources 522

16 Python XML Processing 529


16.1 Introduction 530
16.2 Generating XML Content Dynamically 530
16.3 XML Processing Packages 533
16.4 Document Object Model (DOM) 534
16.5 Parsing XML with xml.sax 543
16.6 Case Study: Message Forums with Python and XML 546
16.6.1 Displaying the Forums 548
16.6.2 Adding Forums and Messages 551
16.6.3 Alterations for Browsers without XML and XSLT Support 559
16.7 Internet and World Wide Web Resources 564

17 Database Application Programming Interface (DB-API)


569
17.1 Introduction 570

© Copyright 2002 by Prentice Hall. All Rights Reserved.


pythonhtp1_01TOC.fm Page xiv Monday, January 14, 2002 12:20 PM

XIV Contents

17.2 Relational Database Model 571


17.3 Relational Database Overview: Books Database 572
17.4 Structured Query Language (SQL) 579
17.4.1 Basic SELECT Query 580
17.4.2 WHERE Clause 581
17.4.3 ORDER BY Clause 583
17.4.4 Merging Data from Multiple Tables: INNER JOIN 587
17.4.5 Joining Data from Tables Authors, AuthorISBN, Titles and
Publishers589
17.4.6 INSERT Statement 591
17.4.7 UPDATE Statement 592
17.4.8 DELETE Statement 593
17.5 Python DB-API Specification 594
17.6 Database Query Example 595
17.7 Querying the Books Database 598
17.8 Reading, Inserting and Updating a Database 602
17.9 Internet and World Wide Web Resources 607

18 Process Management 613


18.1 Introduction 614
18.2 os.fork Function 615
18.3 os.system Function and os.exec Family of Functions 623
18.4 Controlling Process Input and Output 628
18.5 Interprocess Communication 631
18.6 Signal Handling 635
18.7 Sending Signals 637

19 Multithreading 645
19.1 Introduction 646
19.2 Thread States: Life Cycle of a Thread 647
19.3 threading.Thread Example 650
19.4 Thread Synchronization 651
19.5 Producer/Consumer Relationship without Thread Synchronization 653
19.6 Producer/Consumer Relationship with Thread Synchronization 659
19.7 Producer/Consumer Relationship: Module Queue 665
19.8 Producer/Consumer Relationship: The Circular Buffer 669
19.9 Semaphores 676
19.10 Events 678

20 Networking 688
20.1 Introduction 689
20.2 Accessing URLs over HTTP 690
20.3 Establishing a Simple Server (Using Stream Sockets) 692
20.4 Establishing a Simple Client (Using Stream Sockets) 694
20.5 Client/Server Interaction with Stream Socket Connections 694
20.6 Connectionless Client/Server Interaction with Datagrams 699
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xv Monday, January 14, 2002 12:20 PM

Contents XV

20.7 Client/Server Tic-Tac-Toe Using a Multithreaded Server 701

21 Security 716
21.1 Introduction 717
21.2 Ancient Ciphers to Modern Cryptosystems 718
21.3 Secret-Key Cryptography 722
21.4 Public-Key Cryptography 724
21.5 Cryptanalysis 727
21.6 Key-Agreement Protocols 727
21.7 Key Management 727
21.8 Digital Signatures 728
21.9 Public-Key Infrastructure, Certificates and Certificate Authorities 730
21.9.1 Smart Cards 733
21.10 Security Protocols 734
21.10.1 Secure Sockets Layer (SSL) 734
21.10.2 IPSec and Virtual Private Networks (VPN) 735
21.11 Authentication 736
21.11.1 Kerberos 737
21.11.2 Biometrics 737
21.11.3 Single Sign-On 738
21.11.4 Microsoft® Passport 739
21.12 Security Attacks 739
21.12.1 Denial-of-Service (DoS) Attacks 740
21.12.2 Viruses and Worms 741
21.12.3 Software Exploitation, Web Defacing and Cybercrime 743
21.13 Running Restricted Python Code 744
21.13.1 Module rexec 745
21.13.2 Module Bastion 745
21.13.3 Restricted Web Browser 745
21.14 Network Security 749
21.14.1 Firewalls 749
21.14.2 Intrusion-Detection Systems 750
21.15 Steganography 752
21.16 Internet and World Wide Web Resources 754

22 Data Structures 768


22.1 Introduction 769
22.2 Self-Referential Classes 769
22.3 Linked Lists 770
22.4 Stacks 780
22.5 Queues 782
22.6 Trees 784

23 Case Study: Online Bookstore 797


23.1 Introduction 798
23.2 HTTP Sessions and Session-Tracking Technologies 798
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xvi Monday, January 14, 2002 12:20 PM

XVI Contents

23.3 Tracking Sessions in the Bookstore 800


23.4 Bookstore Architecture 805
23.5 Configuring the Bookstore 807
23.6 Entering the Bookstore 809
23.7 Obtaining the Book List from the Database 810
23.8 Viewing a Book’s Details 817
23.9 Adding an Item to the Shopping Cart 821
23.10 Viewing the Shopping Cart 823
23.11 Checking Out 828
23.12 Processing the Order 831
23.13 Error Handling 833
23.14 Handling Wireless Clients (XHTML Basic and WML) 835
23.14.1 Introduction to XHTML Basic 836
23.14.2 Introduction to WML 847
23.15 Internet and World Wide Web Resources 860

24 Multimedia 867
24.1 Introduction 868
24.2 Introduction to PyOpenGL 868
24.3 PyOpenGL Examples 869
24.4 Introduction to Alice 876
24.5 Fox, Chicken and Seed Problem 877
24.6 Introduction to pygame 883
24.7 Python CD Player 883
24.8 Python Movie Player 890
24.9 Pygame Space Cruiser 893
24.10 Internet and World Wide Web Resources 909

25 Python Server Pages (PSP) 917


25.1 Introduction 918
25.2 Python Servlets 919
25.3 Python Server Pages Overview 920
25.4 First Python Server Page Example 921
25.5 Implicit Objects 924
25.6 Scripting 924
25.6.1 Scripting Components 925
25.6.2 Scripting Example 925
25.7 Standard Actions 928
25.7.1 <psp:include> Action 929
25.7.2 <psp:insert> Action 933
25.7.3 <psp:method> Action 936
25.8 Directives 940
25.8.1 page Directive 940
25.8.2 include Directive 944
25.9 Case Study: Message Forums with Python and XML 947
25.9.1 Displaying the Forums 949
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xvii Monday, January 14, 2002 12:20 PM

Contents XVII

25.9.2 Adding Forums and Messages 952


25.9.3 Alterations for Browsers Without XML and XSLT Support 961
25.10 Internet and World Wide Web Resources 966

A Operator Precedence Chart 971

B ASCII Character Set 973

C Number Systems 974


C.1 Introduction 975
C.2 Abbreviating Binary Numbers as Octal Numbers and Hexadecimal Numbers 978
C.3 Converting Octal Numbers and Hexadecimal Numbers to Binary Numbers 980
C.4 Converting from Binary, Octal or Hexadecimal to Decimal 980
C.5 Converting from Decimal to Binary, Octal or Hexadecimal 981
C.6 Negative Binary Numbers: Two’s Complement Notation 982

D Python Development Environments 988


D.1 Introduction 989
D.2 Integrated Development Environment: IDLE 989
D.2.1 Installing and Launching IDLE 990
D.2.2 Features 991
D.2.3 Text Editor 992
D.2.4 Debugger 994
D.3 Other Integrated Development Environments 995
D.3.1 BlackAdder 995
D.3.2 PythonWorks™ 996
D.3.3 Wing IDE™ 996
D.3.4 Pythonwin 997
D.3.5 Komodo 997
D.4 Internet and World Wide Web Resources 997

E Career Opportunities 998


E.1 Introduction 999
E.2 Resources for the Job Seeker 1000
E.3 Online Opportunities for Employers 1001
E.3.1 Posting Jobs Online 1003
E.3.2 Problems with Recruiting on the Web 1005
E.3.3 Diversity in the Workplace 1005
E.4 Recruiting Services 1006
E.5 Career Sites 1007
E.5.1 Comprehensive Career Sites 1007
E.5.2 Technical Positions 1008
E.5.3 Wireless Positions 1008
E.5.4 Contracting Online 1009
E.5.5 Executive Positions 1010

© Copyright 2002 by Prentice Hall. All Rights Reserved.


pythonhtp1_01TOC.fm Page xviii Monday, January 14, 2002 12:20 PM

XVIII Contents

E.5.6 Students and Young Professionals 1011


E.5.7 Other Online Career Services 1011
E.6 Internet and World Wide Web Resources 1012

F Unicode® 1020
F.1 Introduction 1021
F.2 Unicode Transformation Format (UTF) 1022
F.3 Characters and Glyphs 1023
F.4 Advantages and Disadvantages of Unicode 1024
F.5 Unicode Consortium’s Web Site 1024
F.6 Using Unicode 1025
F.7 Character Ranges 1027

G Introduction to HyperText Markup Language 4: Part 1


1032
G.1 Introduction 1033
G.2 Markup Languages 1033
G.3 Editing HTML 1034
G.4 Common Elements 1034
G.5 Headers 1037
G.6 Linking 1038
G.7 Images 1040
G.8 Special Characters and More Line Breaks 1044
G.9 Unordered Lists 1046
G.10 Nested and Ordered Lists 1047
G.11 Internet and World Wide Web Resources 1050

H Introduction to HyperText Markup Language 4: Part 2


1055
H.1 Introduction 1056
H.2 Basic HTML Tables 1056
H.3 Intermediate HTML Tables and Formatting 1058
H.4 Basic HTML Forms 1061
H.5 More Complex HTML Forms 1064
H.6 Internal Linking 1071
H.7 Creating and Using Image Maps 1074
H.8 meta Elements 1076
H.9 frameset Element 1078
H.10 Nested framesets 1080
H.11 Internet and World Wide Web Resources 1082

I Introduction to XHTML: Part 1 1088


I.1 Introduction 1089
I.2 Editing XHTML 1089
I.3 First XHTML Example 1090

© Copyright 2002 by Prentice Hall. All Rights Reserved.


pythonhtp1_01TOC.fm Page xix Monday, January 14, 2002 12:20 PM

Contents XIX

I.4 W3C XHTML Validation Service 1093


I.5 Headers 1095
I.6 Linking 1097
I.7 Images 1099
I.8 Special Characters and More Line Breaks 1103
I.9 Unordered Lists 1105
I.10 Nested and Ordered Lists 1106
I.11 Internet and World Wide Web Resources 1109

J Introduction to XHTML: Part 2 1114


J.1 Introduction 1115
J.2 Basic XHTML Tables 1115
J.3 Intermediate XHTML Tables and Formatting 1118
J.4 Basic XHTML Forms 1120
J.5 More Complex XHTML Forms 1123
J.6 Internal Linking 1130
J.7 Creating and Using Image Maps 1133
J.8 meta Elements 1135
J.9 frameset Element 1137
J.10 Nested framesets 1140
J.11 Internet and World Wide Web Resources 1142

K Cascading Style Sheets™ (CSS) 1148


K.1 Introduction 1149
K.2 Inline Styles 1149
K.3 Embedded Style Sheets 1150
K.4 Conflicting Styles 1153
K.5 Linking External Style Sheets 1156
K.6 W3C CSS Validation Service 1159
K.7 Positioning Elements 1160
K.8 Backgrounds 1163
K.9 Element Dimensions 1165
K.10 Text Flow and the Box Model 1167
K.11 User Style Sheets 1172
K.12 Internet and World Wide Web Resources 1176

L Accessibility 1181
L.1 Introduction 1182
L.2 Web Accessibility 1182
L.3 Web Accessibility Initiative 1183
L.4 Providing Alternatives for Images 1185
L.5 Maximizing Readability by Focusing on Structure 1186
L.6 Accessibility in XHTML Tables 1186
L.7 Accessibility in XHTML Frames 1190
L.8 Accessibility in XML 1191
L.9 Using Voice Synthesis and Recognition with VoiceXML™ 1191
© Copyright 2002 by Prentice Hall. All Rights Reserved.
pythonhtp1_01TOC.fm Page xx Monday, January 14, 2002 12:20 PM

XX Contents

L.10 CallXML™ 1198


L.11 JAWS® for Windows 1204
L.12 Other Accessibility Tools 1205
L.13 Accessibility in Microsoft® Windows® 2000 1206
L.13.1 Tools for People with Visual Impairments 1208
L.13.2 Tools for People with Hearing Impairments 1210
L.13.3 Tools for Users Who Have Difficulty Using the Keyboard 1210
L.13.4 Microsoft Narrator 1216
L.13.5 Microsoft On-Screen Keyboard 1217
L.13.6 Accessibility Features in Microsoft Internet Explorer 5.5 1218
L.14 Internet and World Wide Web Resources 1220

M HTML/XHTML Special Characters 1227

N HTML/XHTML Colors 1228

O Additional Python 2.2 Features 1231


O.1 Introduction 1232
O.2 Iterators 1232
O.3 Generators 1242
O.4 Nested Scopes 1247
O.5 Internet and World Wide Web Resources 1249

1251

Bibliography 1251

Index 1253

© Copyright 2002 by Prentice Hall. All Rights Reserved.

Anda mungkin juga menyukai