Foundations of Python

Similar documents
Chapter 2. Application Layer

The 4D Web Companion. David Adams

Operating Systems. 16. Networking. Paul Krzyzanowski. Rutgers University. Spring /6/ Paul Krzyzanowski

Computer Networks SYLLABUS CHAPTER - 2 : NETWORK LAYER CHAPTER - 3 : INTERNETWORKING

Different Layers Lecture 21

Simple Network Management Protocol (SNMP)

Chapter 2 Application Layer. Lecture 4: principles of network applications. Computer Networking: A Top Down Approach

Application Level Protocols

APPLICATION LAYER APPLICATION LAYER : DNS, HTTP, , SMTP, Telnet, FTP, Security-PGP-SSH.

Review of Previous Lecture

BIRLA INSTITUTE OF TECHNOLOGY AND SCIENCE, Pilani Pilani Campus Instruction Division

Simple Network Management Protocol (SNMP)

Review for Internet Introduction

CSC 4900 Computer Networks:

Acknowledgments... xix

Chapter 10: Application Layer CCENT Routing and Switching Introduction to Networks v6.0

Appendix. Web Command Error Codes. Web Command Error Codes

EEC-682/782 Computer Networks I

Programming. Brandon Rhodes and John Goerzen SECOND EDITION THE EXPERT S VOICE IN OPEN SOURCE

The Application Layer: SMTP, FTP

Introduction to Networking

Hands-On Ethical Hacking and Network Defense

Microsoft Exchange Server 2013 and 2016 Deployment

Application Layer: OSI and TCP/IP Models

Computer Networks (Unit wise Questions)

Networking. Layered Model. DoD Model. Application Layer. ISO/OSI Model

CSCI 466 Midterm Networks Fall 2013

Electronic Mail. Three Components: SMTP SMTP. SMTP mail server. 1. User Agents. 2. Mail Servers. 3. SMTP protocol

COMPUTER NETWORKS AND COMMUNICATION PROTOCOLS. Web Access: HTTP Mehmet KORKMAZ

TH IRD EDITION. Python Cookbook. David Beazley and Brian K. Jones. O'REILLY. Beijing Cambridge Farnham Köln Sebastopol Tokyo

Network Applications Principles of Network Applications

WWW: the http protocol

Layered Model. DoD Model. ISO/OSI Model

Lecture 25. Tuesday, November 21 CS 475 Networks - Lecture 25 1

Application-layer Protocols and Internet Services

Asynchronous and Synchronous Messaging with Web Services and XML Ronald Schmelzer Senior Analyst ZapThink, LLC

Application Layer Network Layer

INTERNET & WORLD WIDE WEB (UNIT-1) MECHANISM OF INTERNET

CS321: Computer Networks FTP, TELNET, SSH

13. Internet Applications 최양희서울대학교컴퓨터공학부

Pro ASP.NET MVC 2 Framework

Unit 28 Website Production ASSIGNMENT 1

Configuring Health Monitoring

HP Load Balancing Module

Guide to TCP/IP, Third. Chapter 6: Basic TCP/IP Services

Electronic Mail Paradigm

Electronic Mail

Computer Networks. Wenzhong Li. Nanjing University

TELNET is short for Terminal Network Enables the establishment of a connection to a remote system, so that the local terminal appears to be the

Computer Network 1 1

Data Communication & Computer Networks MCQ S

The Application Layer: & SMTP

DATA COMMUNICATOIN NETWORKING

CCNA Exploration Network Fundamentals. Chapter 3 Application Layer Functionality and Protocols

Produced by. Mobile Application Development. Higher Diploma in Science in Computer Science. Eamonn de Leastar

SYLLABUS. osmania university CHAPTER - 3 : MEDIUM ACCESS CONTROL (MAC) SUBLAYER Standards, Bluetooth, Bridges and Routers.

Internet and Intranet Protocols and Applications

Application Layer: HTTP

Application Layer Introduction; HTTP; FTP

Transport Layer (TCP/UDP)

Internet Technology. 03r. Application layer protocols: . Paul Krzyzanowski. Rutgers University. Spring 2016

Electronic Mail. Prof. Indranil Sen Gupta. Professor, Dept. of Computer Science & Engineering Indian Institute of Technology Kharagpur

Distributed Systems. 02. Networking. Paul Krzyzanowski. Rutgers University. Fall 2017

SCS3004 Networking Technologies Application Layer Protocols

Internet Architecture. Web Programming - 2 (Ref: Chapter 2) IP Software. IP Addressing. TCP/IP Basics. Client Server Basics. URL and MIME Types HTTP

Chapter 2 Application Layer

Web Robots Platform. Web Robots Chrome Extension. Web Robots Portal. Web Robots Cloud

COMS3200/7201 Computer Networks 1 (Version 1.0)

BIG-IP Access Policy Manager : Portal Access. Version 13.0

CCNA Exploration Network Fundamentals. Chapter 03 Application Functionality and Protocols

Chapter 2: Application Layer. Chapter 2 Application Layer. Some network apps. Application architectures. Chapter 2: Application layer

Internet Protocol Stack! Principles of Network Applications! Some Network Apps" (and Their Protocols)! Application-Layer Protocols! Our goals:!

Etanova Enterprise Solutions

Key Points for the Review

Lecture-4. TCP/IP-Overview:

COMS Introduction to Computers. Networking

CS348: Computer Networks (SMTP, POP3, IMAP4); FTP

PLEASE READ CAREFULLY BEFORE YOU START

Introduction to TCP/IP

Protocol Compliance Statements for the CSG2

Module 1: Understanding and Installing Internet Information Services

TSIN02 - Internetworking

CCNA R&S: Introduction to Networks. Chapter 10: The Application Layer

Special expressions, phrases, abbreviations and terms of Computer Networks

TRANSMISSION CONTROL PROTOCOL. ETI 2506 TELECOMMUNICATION SYSTEMS Monday, 7 November 2016

PROGRAMMING Kyriacou E. Frederick University Cyprus. Network communication examples

ECE697AA Lecture 2. Today s lecture

Pro ODP.NET for Oracle. Database 11 g. Edmund Zehoo. Apress

Java.. servlets and. murach's TRAINING & REFERENCE 2ND EDITION. Joel Murach Andrea Steelman. IlB MIKE MURACH & ASSOCIATES, INC.

HP Instant Support Enterprise Edition (ISEE) Security overview

Chapter 2 Application Layer

2. Introduction to Internet Applications

FTP. Mail. File Transfer Protocol (FTP) FTP commands, responses. Electronic Mail. TDTS06: Computer Networks

Transport Layer Security

CS 356 Internet Security Protocols. Fall 2013

Web as a Distributed System

Chapter 2 Application Layer

FTP. FTP offers many facilities :

Mobile MOUSe EXCHANGE SERVER 2010 CONFIGURATION ONLINE COURSE OUTLINE

Applications & Application-Layer Protocols: FTP and (SMTP & POP)

Transcription:

Foundations of Python Network Programming The comprehensive guide to building network applications with Python Second Edition Brandon Rhodes John Goerzen Apress

Contents Contents at a Glance About the Authors About the Technical Reviewer Acknowledgments Introduction iv xv xv xvi xvii Chapter 1: Introduction to Client/Server Networking 1 The Building Blocks: Stacks and Libraries 1 Application Layers 4 Speaking a Protocol 5 A Raw Network Conversation 6 Turtles All the Way Down 8 The Internet Protocol 9 IP Addresses 10 Routing 11 Packet Fragmentation 13 Learning More About IP 14 Chapter 2: UDP 15 Should You Read This Chapter? 16 Addresses and Port Numbers 16 Port Number Ranges 17 Sockets 19

CONTENTS Unreliability, Backoff, Blocking, Timeouts 22 Connecting UDP Sockets 25 Request IDs: A Good Idea 27 Binding to Interfaces 28 UDP Fragmentation 30 Socket Options 31 Broadcast 32 When to Use UDP 33 34 SlChapter 3: TCP 35 How TCP Works 35 When to Use TCP 36 What TCP Sockets Mean 37 A Simple TCP Client and Server 38 One Socket per Conversation 41 Address Already in Use 42 Binding to Interfaces 43 Deadlock 44 Closed Connections, Half-Open Connections 48 Using TCP Streams like Files 49 49 Chapter 4: Socket Names and DNS 51 Hostnames and Domain Names 51 Socket Names 52 Five Socket Coordinates 53 IPv6 54 Modern Address Resolution 55

m CONTENTS Asking getaddrinfoo Where to Bind 56 Asking getaddrinfoo About Services 56 Asking getaddrinfoo for Pretty Hostnames 57 Other getaddrinfoo Flags 68 Primitive Name Service Routines 59 Using getsockaddro in Your Own Code 60 Better Living Through Paranoia 61 A Sketch of How DNS Works 63 Why Not to Use DNS 65 Why to Use DNS 66 Resolving Mail Domains Zeroconf and Dynamic DNS 70 Chapter 5: etwork Data and Network Errors 71 Text and Encodings 71 Network Byte Order 73 Framing and Quoting 75 Pickles and Self-Delimiting Formats --79 XML, JSON, Etc 80 Compression 81 Network Exceptions 82 Handling Exceptions 83 85 Chapter 6: TLS and SSL 87 Computer Security 87 IP Access Rules 88 Cleartext on the Network 90 58 70 vii

* CONTENTS TLS Encrypts Your Conversations 92 TLS Verifies Identities 93 Supporting TLS in Python 94 The Standard SSL Module 95 Loose Ends 98 98 Chapter 7: Server Architecture 99 Daemons and Logging 99 Our Example: Sir Launcelot 100 An Elementary Client 102 The Waiting Game 103 Running a Benchmark 106 Event-Driven Servers 109 Poll vs. Select 112 The Semantics of Non-blocking 113 Event-Driven Servers Are Blocking and Synchronous 114 Twisted Python 114 Load Balancing and Proxies 117 Threading and Multi-processing 117 Threading and Multi-processing Frameworks 120 Process and Thread Coordination 122 Running Inside inetd 123 124 Chapter 8: Caches, Message Queues, and Map-Reduce 125 Using Memcached 126 Memcached and Sharding 128 Message Queues 130 viii

If CONTENTS Using Message Queues from Python 131 How Message Queues Change Programming 133 Map-Reduce 134 136 Chapter 9: HTTP 137 URL Anatomy 138 Relative URLs 141 Instrumenting urllib2 141 The GET Method 142 The Host Header 144 Codes, Errors, and Redirection 144 Payloads and Persistent Connections, 147 POST And Forms 148 Successful Form POSTs Should Always Redirect 150 POST And APIs 151 REST And More HTTP Methods 151 Identifying User Agents and Web Servers 152 Content Type Negotiation 153 Compression 154 HTTP Caching 155 The HEAD Method 156 HTTPS Encryption 156 HTTP Authentication 157 Cookies 158 HTTP Session Hijacking 160 Cross-Site Scripting Attacks 160 WebOb 161

i CONTENTS 161 Chapter 10: Screen Scraping 163 Fetching Web Pages 163 Downloading Pages Through Form Submission 164 The Structure of Web Pages 167 Three Axes 168 Diving into an HTML Document 169 Selectors 173 I77 Chapter 11: Web Applications 179 Web Servers and Python 180 Two Tiers 18 Choosing a Web Server 182 WSGI 183 WSGI Middleware 185 Python Web Frameworks 187 URL Dispatch Techniques 189 Templates 190 Final Considerations 191 Pure-Python Web Servers 192 CGI 193 mod_python 194 Chapter 12: E-mail Composition and > Decoding E-mail Messages 195.197 198 Composing Traditional Messages 200 Parsing Traditional Messages 202 X

it CONTENTS Parsing Dates 203 Understanding MIME 205 How MIME Works 206 Composing MIME Attachments 206 MIME Alternative Parts 208 Composing Non-English Headers 210 Composing Nested Multiparts 211 Parsing MIME Messages 213 Decoding Headers 215 216 IChapter13: SMTP 217 E-mail Clients, Webmail Services 217 In the Beginning Was the Command Line 218 The Rise of Clients 218 The Move to Webmail 220 How SMTP Is Used 221 Sending E-Mail 221 Headers and the Envelope Recipient 222 Multiple Hops 223 Introducing the SMTP Library 224 Error Handling and Conversation Debugging 225 Getting Information from EHLO 228 Using Secure Sockets Layer and Transport Layer Security 230 Authenticated SMTP 232 SMTP Tips 233 234 Chapter 14: POP 235 Compatibility Between POP Servers 235

CONTENTS Connecting and Authenticating 235 Obtaining Mailbox Information 238 Downloading and Deleting Messages 239 241 Chapter 15: IMAP 243 Understanding IMAP in Python 244 IMAPCIient 246 Examining Folders 248 Message Numbers vs. UIDs 248 Message Ranges 249 Information 249 Downloading an Entire Mailbox 250 Downloading Messages Individually 252 Flagging and Deleting Messages 257 Deleting Messages 258 Searching 259 Manipulating Folders and Messages 260 Asynchrony 261 261 Chapter 16: Telnet and SSH 263 Command-Line Automation 263 Command-Line Expansion and Quoting 265 Unix Has No Special Characters 266 Quoting Characters for Protection 268 The Terrible Windows Command Line 269 Things Are Different in a Terminal 270 Terminals Do Buffering 273 xii

11 CONTENTS Telnet 274 SSH:The Secure Shell 278 An Overview of SSH 279 SSH Host Keys 280 SSH Authentication 282 Shell Sessions and Individual Commands 283 SFTP: File Transfer Over SSH 286 Other Features 289 290 Chapter 17: FTP 291 What to Use Instead of FTP 291 Communication Channels 292 Using FTP in Python 293 ASCII and Binary Files 294 Advanced Binary Downloading 295 Uploading Data 297 Advanced Binary Uploading 298 Handling Errors 299 Detecting Directories and Recursive Download 301 Creating Directories, Deleting Things 302 Doing FTP Securely 303 303 Chapter 18: RPC 305 Features of RPC 306 XML-RPC 307 JSON-RPC 313 Self-documenting Data 315 xiii

II CONTENTS Talking About Objects: Pyro and RPyC 316 An RPyC Example 317 RPC, Web Frameworks, Message Queues 319 Recovering From Network Errors 320 Binary Options: Thrift and Protocol Buffers 320 321 Index 323 xiv