Full Download Foundations of Python Network Programming Third Edition Brandon Rhodes PDF DOCX
Full Download Foundations of Python Network Programming Third Edition Brandon Rhodes PDF DOCX
https://ebookgate.com
https://ebookgate.com/product/foundations-of-
python-network-programming-third-edition-brandon-
rhodes/
https://ebookgate.com/product/foundations-of-python-network-
programming-3rd-edition-brandon-rhodes/
ebookgate.com
https://ebookgate.com/product/java-network-programming-third-edition-
elliotte-rusty-harold/
ebookgate.com
https://ebookgate.com/product/expert-python-programming-2nd-edition-
jaworski/
ebookgate.com
https://ebookgate.com/product/python-programming-4th-edition-singh-a/
ebookgate.com
Learning BeagleBone Python Programming Unleash the
potential of BeagleBone using Python 1st Edition Hiam
https://ebookgate.com/product/learning-beaglebone-python-programming-
unleash-the-potential-of-beaglebone-using-python-1st-edition-hiam/
ebookgate.com
https://ebookgate.com/product/basics-of-python-programming-2nd-
edition-dr-pratiyush-guleria/
ebookgate.com
https://ebookgate.com/product/core-python-programming-2nd-edition-
wesley-chun/
ebookgate.com
https://ebookgate.com/product/programming-python-3rd-ed-edition-mark-
lutz/
ebookgate.com
https://ebookgate.com/product/ultimate-python-programming-1st-edition-
deepali-srivastava/
ebookgate.com
Foundations of Python Network
Programming
Third Edition
Brandon Rhodes
John Goerzen
Foundations of Python Network Programming, Third Edition
Copyright © 2014 by Brandon Rhodes and John Goerzen
This work is subject to copyright. All rights are reserved by the Publisher,
whether the whole or part of the material is concerned, specifically the rights
of translation, reprinting, reuse of illustrations, recitation, broadcasting,
reproduction on microfilms or in any other physical way, and transmission or
information storage and retrieval, electronic adaptation, computer software,
or by similar or dissimilar methodology now known or hereafter developed.
Exempted from this legal reservation are brief excerpts in connection with
reviews or scholarly analysis or material supplied specifically for the purpose
of being entered and executed on a computer system, for exclusive use by
the purchaser of the work. Duplication of this publication or parts thereof is
permitted only under the provisions of the Copyright Law of the Publisher’s
location, in its current version, and permission for use must always be
obtained from Springer. Permissions for use may be obtained through
RightsLink at the Copyright Clearance Center. Violations are liable to
prosecution under the respective Copyright Law.
ISBN-13 (pbk): 978-1-4302-5854-4
ISBN-13 (electronic): 978-1-4302-5855-1
Trademarked names, logos, and images may appear in this book. Rather
than use a trademark symbol with every occurrence of a trademarked name,
logo, or image we use the names, logos, and images only in an editorial
fashion and to the benefit of the trademark owner, with no intention of
infringement of the trademark.
The use in this publication of trade names, trademarks, service marks, and
similar terms, even if they are not identified as such, is not to be taken as an
expression of opinion as to whether or not they are subject to proprietary
rights.
While the advice and information in this book are believed to be true and
accurate at the date of publication, neither the authors nor the editors nor the
publisher can accept any legal responsibility for any errors or omissions that
may be made. The publisher makes no warranty, express or implied, with
respect to the material contained herein.
Managing Director: Welmoed Spahr
Publisher: Heinz Weinheimer
Lead Editor: Michelle Lowman
Technical Reviewers: Alexandre Beaulne and Peter Membrey
Editorial Board: Steve Anglin, Mark Beckner, Ewan Buckingham,
Gary Cornell, Louise Corrigan, Jim DeWolf, Jonathan Gennick,
Robert Hutchinson, Michelle Lowman, James Markham,
Matthew Moodie, Jeff Olson, Jeffrey Pepper, Douglas Pundick,
Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing,
Matt Wade, Steve Weiss
Coordinating Editor: Kevin Walter
Developmental Editor: Gary Schwartz
Copy Editor: Kim Wimpsett
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media
New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-
SPRINGER, fax (201) 348-4505, e-mail orders-ny@springer-sbm.com,
or visit www.springeronline.com. Apress Media, LLC is a California LLC
and the sole member (owner) is Springer Science + Business Media Finance
Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.
For information on translations, please e-mail rights@apress.com, or visit
www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic,
corporate, or promotional use. eBook versions and licenses are also
available for most titles. For more information, reference our Special Bulk
Sales–eBook Licensing web page at www.apress.com/bulk-sales.
Any source code or other supplementary material referenced by the author in
this text is available to readers at www.apress.com. For detailed
information about how to locate your book’s source code, go to
www.apress.com/source-code/.
To my wonderful nieces, Avery, Savannah, and Aila,
remembering our bicycle ride and the wild abandon
with which they always round the corner and plunge
downhill.
Index
Contents
Binding to Interfaces
UDP Fragmentation
Socket Options
Broadcast
When to Use UDP
Summary
Chapter 3: TCP
How TCP Works
When to Use TCP
What TCP Sockets Mean
A Simple TCP Client and Server
One Socket per Conversation
Address Already in Use
Binding to Interfaces
Deadlock
Closed Connections, Half-Open Connections
Using TCP Streams Like Files
Summary
Chapter 4: Socket Names and DNS
Hostnames and Sockets
Five Socket Coordinates
IPv6
Summary
Chapter 5: Network Data and Network Errors
Bytes and Strings
Character Strings
Binary Numbers and Network Byte Order
Summary
Chapter 6: TLS/SSL
What TLS Fails to Protect
What Could Possibly Go Wrong?
Generating Certificates
Offloading TLS
Python 3.4 Default Contexts
Variations on Socket Wrapping
Async Servers
Callback-Style asyncio
Coroutine-Style asyncio
The Legacy Module asyncore
The Best of Both Worlds
Summary
Chapter 9: HTTP Clients
Python Client Libraries
Ports, Encryption, and Framing
Methods
Paths and Hosts
Status Codes
Caching and Validation
Content Encoding
Content Negotiation
Content Type
HTTP Authentication
Cookies
Connections, Keep-Alive, and httplib
Summary
Chapter 10: HTTP Servers
WSGI
Asynchronous Server-Frameworks
Forward and Reverse Proxies
Four Architectures
Running Python Under Apache
The Rise of Pure-Python HTTP Servers
The Benefits of Reverse Proxies
Platforms as a Service
GET and POST Patterns and the Question of
REST
WSGI Without a Framework
Summary
Chapter 11: The World Wide Web
Hypermedia and URLs
Parsing and Building URLs
Relative URLs
The Hypertext Markup Language
Reading and Writing to a Database
A Terrible Web Application (in Flask)
The Dance of Forms and HTTP Methods
When Forms Use Wrong Methods
Safe and Unsafe Cookies
Nonpersistent Cross-Site Scripting
Persistent Cross-Site Scripting
Cross-Site Request Forgery
The Improved Application
Summary
Chapter 12: Building and Parsing E-Mail
E-Mail Message Format
Building an E-Mail Message
Adding HTML and Multimedia
Adding Content
Parsing E-Mail Messages
Walking MIME Parts
Header Encodings
Parsing Dates
Summary
Chapter 13: SMTP
E-mail Clients vs. Webmail Services
In the Beginning Was the Command Line
The Rise of Clients
The Move to Webmail
Summary
Chapter 16: Telnet and SSH
Command-Line Automation
Command-Line Expansion and Quoting
Unix Command Arguments Can Include (Almost) Any Character
Quoting Characters for Protection
The Terrible Windows Command Line
Things Are Different in a Terminal
Terminals Do Buffering
Telnet
SSH: The Secure Shell
An Overview of SSH
SSH Host Keys
SSH Authentication
Shell Sessions and Individual Commands
SFTP: File Transfer Over SSH
Other Features
Summary
Chapter 17: FTP
What to Use Instead of FTP
Communication Channels
Using FTP in Python
ASCII and Binary Files
Advanced Binary Downloading
Uploading Data
Advanced Binary Uploading
Handling Errors
Scanning Directories
Detecting Directories and Recursive Download
Creating Directories, Deleting Things
Doing FTP Securely
Summary
Chapter 18: RPC
Features of RPC
XML-RPC
JSON-RPC
Self-Documenting Data
Talking About Objects: Pyro and RPyC
An RPyC Example
RPC, Web Frameworks, and Message Queues
Recovering from Network Errors
Summary
Index
About the Authors
Author: C. Shook
Language: English
Well, the horn doesn't show up and I can tell my lip is slipping out of
shape. It is now three weeks since I got into this place and I have
nothing different. I am able to talk to the duck men, though, and I will
say for Ogroo that he is a good teacher since I am never more than
a poor C in languages when I am in school.
And then one day Ogroo says to me, "Mac, I am happy to tell you
that we have located the object which you call a trombone. One of
the men took it and has had it hidden. He feared it was a thing of evil
power. I assured him it was not, though I was not so sure myself. I
hope that I was correct."
"Ogroo, old boy," I tell him, "the trombone is strictly a thing of good
power as I will show you if you will produce it. It is a thing of music."
"Why, Mac," says Ogroo, "why did you not say this before. We have
music too. It is our great pride."
Now during the time the mayor has been educating me, there is one
of the large buildings which I have never been in. I have asked
Ogroo about this and he has always said they were saving it as a
surprise for me. But now he gets up and starts out the door.
"You will know of the surprise at last," he says.
And he leads me to the big barn which has always been closed.
Well you can hang me for a long-hair when we get inside, for there
are about two hundred of the duck people shuffling around like a
flock of jitterbugs, and ten or twelve players are giving out with some
corny rhythm on a raised platform for a bandstand. They have about
three-fourths percussion, mostly tom-tom-like drums, but there are a
few gut buckets of some kind which they do not appear to play for
nothing.
Ogroo looks at me.
"Is it not magnificent?" he says.
"Well," I say, "it is all right, but where I come from it is done in a
slightly different manner. I shall be happy to show you if you will
kindly produce my horn."
I can hardly wait to lay my lip into a solid beat the more I listen to
these ickies peeling it off the cob, and when one of the men finally
brings in old Susie, I kiss her lovingly. She is in fine shape.
Old Ogroo stops the noise. He makes an announcement, and
everything is quiet as I step up with my slush pump. It is like
Goodman at Carnegie Hall.
Everybody crowds around as I give out with the Royal Garden Blues.
I see I have them overcome and I begin to send softly as I hear one
of the boys pick up the beat in the background. He is not so awful at
that. After I have taken two choruses, one of the gut buckets has
picked up the melody and I dub in the harmony for him. The crowd is
beginning to sway slightly when I slide into Rose Room and pretty
soon they are on the jump until it is worse than a bunch of the
alligators at a Krupa concert. All in all it is a very successful
performance indeed.
By the time I have finished, I see that I have first chair cinched, and
the crowd is eating out of my hand.
This is by no means the last performance I give. I soon have the
duck men in the band playing the best jive they can give out with, but
it is rather sorry without any reeds and only one brass. They are
entirely unable to play any wind instruments, though, so I am forced
to make the best of it.
We play for three or four hours, and when old Ogroo and I finally
leave the hall, I am cheered all down the line. I am really terrific.
"Mac," Ogroo tells me when we are outside, "you are wonderful. We
appreciate music and in fact it is the biggest thing in our lives here.
But you are lucky that we are the ones that found you on your arrival
and not the animal men from the woods. They are very ignorant, and
your trombone would have meant nothing to them."
Well, this is the first time I have heard about these animal men, and I
figure maybe they are a little closer to civilization than Ogroo thinks. I
ask him about them.
"They are our enemies," he says, "and are much stronger than we.
They control all the land surrounding us, but on the water we have
the best of them and they never try to attack us here. However we
must venture into the forests sometimes, and then we are in
constant danger. Many of us are killed or captured each year."
I think no more about this, however, and I spend my time playing for
the concerts they have every day. I am very popular with one and all.
But a few weeks afterwards, Ogroo asks me to join one of their
expeditions into the forests.
"We have to gather our monthly food crop," he says. "And everyone
in the community has to do his share. As you are now one of us, it is
only fitting that you come along."
Well, of course I clap Ogroo on the back and tell him I will be very
pleased to go, and, in fact, I am not worried much about their
enemies because I am a good hundred pounds heavier than any of
the duck men and I figured I will be plenty for these animal people to
handle. As it turns out, I am right in this respect, but I hit one bad
note which almost costs me my life and very possibly does so for my
friends.
There are about twenty of us that start out. Each one is carrying two
large baskets made out of the purple reeds which grow in the
swampy lowlands of the islands. Before we begin, I tell Ogroo that I
will swim over if he will carry my baskets, but he does not
understand what I mean until I dive into the river and demonstrate.
This exhibition is a great surprise to everyone, as they have never
seen anything like it before. When I have climbed out on the other
bank, the rest of the party jumps in and floats over rapidly. Then we
begin walking toward the deep purple forests.
We hustle around all morning, and there is no trouble. What we are
gathering is some kind of mushroom that grows around the foot of
the trees, and we are looking for certain vegetables which have to
have the shade to amount to anything. It is in the afternoon shortly
before we are ready to depart that one of the men who is acting as a
lookout gives the alarm. There is a group of animal men hunting in
the woods and they have spotted us. I am curious to see how these
men appear and I hang back some while the others run as fast as
they can on their webbed feet toward the river; they are luckily near
the water, for they could never outdistance these land people.
Well, I know I can catch up, so, as I say, I wait a couple of seconds.
But when I have a gander at our enemies, I am off faster than a
sixty-fourth beat, and it is none too soon. As a matter of fact, it is a
wonder that I am able to run at all, for what I see charging at me is
about ten big two-headed monsters running on four legs sometimes,
and sometimes on two. They are not quite as large as a man when
they stand up, but they are enough to send me heading for the river.
I dive in just before they get there and I am churning the water like
the Queen Mary when I hit the island. Then I look around to see
what has happened. The monsters are lined up at the edge of the
river watching us, but they do not try to cross over. They are pointing
at me and acting excited, and Ogroo laughs.
"They have never seen anything like you," he says. "But we are safe
now for they cannot—what did you call it—swim?"
I say that is very lucky indeed, as they are remarkably tough
appearing babies, but we do not bother any more with them and
pretty soon they have disappeared into the forests. It is over a week
later that I realize the bad note I hit and what it is going to do to us.
When the crowd sees me do this they move in even closer, all the
time telling one another to give me air, but finally one of them claims
that he is a doctor and he helps me up and I go with him and another
man in uniform who is probably a policeman. They tell me that they
are taking me to a hospital, and I do not remember much after that.
When I wake up again, I am in the hospital.
A doctor has hold of my wrist, and when he sees me open my eyes
he says, "How are you feeling now?"
I tell him okay.
"Well," he says, "you seem to have had quite a shock, and perhaps
you do not want to discuss it now, but your manner of dress and this
instrument which you have brought with you have excited my
curiosity no little."
I see that my trombone is on the table near him.
"Why no, I do not mind telling you," I say, "though you might find it
hard to believe what I have gone through. But first—where am I and
what month is it?"
The doctor lets go of my wrist.
"You are in New York," he says, "and it is September of the year
Twenty-five O Seven."
"Just a minute," I say, "I must misunderstand you. I thought you said
the year was Twenty-five O Seven."
"That is what I did say," says the doc.
"But that cannot be true," I tell him. "Why I was born in 1914 and it is
not possible for me to be living at such a period in history."
He picks up my wrist again.
"You are a little excited," he says, "and I think you had better get a bit
more rest. Then we can talk this thing over later."
I see him say something to the nurse who is standing in the doorway
all this time, and she nods as he goes out. I start to call to him but I
figure it is no use. So I go back to sleep.
The second time I wake up, the doc is back and he has four other
men with him. They are sitting in chairs around the room watching
me; as soon as they see I am awake they come over to my bed.
"These men are very much interested in your case," the doctor tells
me. "I have been telling them about your statement and the strange
circumstances attending your appearance on Fifty-Second Street
today. Now I feel that you have had enough rest and I want you to
tell them the entire story."
Well, I know they will figure I am off the beat, but I start at the
beginning and relate the whole story anyway. They do not say a
word until I have finished. Then they look at each other and have a
whispered session on the other side of the room. Finally one of them
speaks up.
"Mr. McRae," he says, "we want to question you a little further if you
don't mind. Will you please put on your clothes and come with us?"
I do like they say since there is nothing else for me to do, and when I
am dressed they take me down the hall to a big light room which is
practically all glass, and they ask me to sit down at a large table.
"Now, Mr. McRae," the first doc says, "I want you to do something for
me."
He hands me ten little blocks of different sizes and informs me that I
am to place them in the proper holes in a board which he has ready
for just that purpose. I do as he asks.
These seems to surprise him, but he is all set with another test, and I
spend the rest of the afternoon playing these little games, until I am
plenty weary of it and I say so to him.
"Well," he says, "as you likely know, we have been trying to
determine your sanity. I will say that you have demonstrated yourself