Tải bản đầy đủ (.pdf) (805 trang)

Python network programming cookbook

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (11.18 MB, 805 trang )



Python Network Programming
Cookbook


Second Edition

Overcome real-world networking challenges


Pradeeban Kathiravelu
Dr. M. O. Faruque Sarker


BIRMINGHAM - MUMBAI



Python Network Programming
Cookbook


Second Edition
Copyright © 2017 Packt Publishing

All rights reserved. No part of this book may be reproduced, stored
in a retrieval system, or transmitted in any form or by any means,
without the prior written permission of the publisher, except in the
case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to


ensure the accuracy of the information presented. However, the
information contained in this book is sold without warranty, either
express or implied. Neither the authors, nor Packt Publishing, and
its dealers and distributors will be held liable for any damages
caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information
about all of the companies and products mentioned in this book by
the appropriate use of capitals. However, Packt Publishing cannot
guarantee the accuracy of this information.

First published: March 2014
Second edition: August 2017


Production reference: 1080817

Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.

ISBN 978-1-78646-399-9

www.packtpub.com


Credits

Authors


Copy Editors

Pradeeban Kathiravelu

Safis Editing

Dr. M. O. Faruque Sarker

Juliana Nair

Reviewers

Project Coordinator

Dr. S. Gowrishankar

Judie Jose

Michael Bright

Commissioning Editor

Proofreader

Kartikey Pandey

Safis Editing



Acquisition Editor

Indexer

Rahul Nair

Aishwarya Gangawane

Content Development Editor

Graphics

Abhishek Jadhav

Kirk D'Penha

Technical Editor

Production Coordinator

Mohd Riyan Khan

Aparna Bhagat



About the Author
Pradeeban Kathiravelu is an open source evangelist. He is a
Ph.D. researcher at INESC-ID Lisboa/Instituto Superior Tecnico,
Universidade de Lisboa, Portugal, and Universite Catholique de

Louvain, Belgium. He is a Fellow of Erasmus Mundus Joint
Degree in Distributed Computing (EMJD-DC), researching a
software-defined approach to quality of service and data quality in
multi-tenant clouds.
Pradeeban holds a master of science degree, Erasmus Mundus
European Master in Distributed Computing (EMDC), from
Instituto Superior Tecnico, Portugal and KTH Royal Institute of
Technology, Sweden. He also holds a first class bachelor of science
in engineering (Hons) degree, majoring in computer science and
engineering, from the University of Moratuwa, Sri Lanka. His
research interests include Software-Defined Networking (SDN),
distributed systems, cloud computing, web services, big data in
biomedical informatics, Network Functions Virtualizations (NFV),
and data mining. He is very interested in free and open source
software development and has been an active participant in the
Google Summer of Code (GSoC) program since 2009, as a student
and as a mentor.
Pradeeban has published several conference papers and coauthored a few book chapters. He has also worked on
OpenDaylight Cookbook and Learning OpenDaylight as a technical
reviewer. Python Network Programming Cookbook, Second
Edition (2017) is his first book as an author, and he is quite excited
about it.


I would like to thank my readers for the interest in the book. Please
feel free to contact me if you need any assistance in the topics or
the recipes, beyond what we have discussed in the book. I would
like to thank the entire editorial team at Packt, including Abhishek
Jadhav, Rahul Nair, and Mohd Riyan Khan. I would like to extend
my thanks to the Linux Foundation for their open source projects

on softwarization of networks and systems. I would like to thank my
friends and colleagues who helped me in various ways. I would like
to thank Prof. Luis Veiga (INESC-ID Lisboa), my MSc and Ph.D.
advisor, for sharing his wisdom and encouragement throughout my
stay in Instituto Superior Tecnico. I would like to thank him for
being my mentor since 2012. I would also like to thank Prof. Ashish
Sharma (Emory University, Atlanta) for his guidance and
motivation.
My special thanks go to my loving wife, Juejing Gu. This book
would not be a reality without her continuous support and creative
suggestions. Her tireless efforts helped me always be on time
without missing the deadlines.
I would like to thank my mom, Selvathie Kathiravelu, for her
support.
Dr. M. O. Faruque Sarker is a software architect based in
London, UK, where he has been shaping various Linux and open
source software solutions, mainly on cloud computing platforms,
for commercial companies, educational institutions, and
multinational consultancies. Over the past 10 years, he has been
leading a number of Python software development and cloud
infrastructure automation projects. In 2009, he started using
Python, where he was responsible for shepherding a fleet of
miniature E-puck robots at the University of South Wales,
Newport, UK. Later, he honed his Python skills, and he was invited


to work on the Google Summer of Code (2009/2010) programs for
contributing to the BlueZ and Tahoe-LAFS open source projects.
He is the author of Python Network Programming Cookbook and
Learning Python Network Programming both by Packt Publishing.

He received his Ph.D. in multi-robot systems from the University
of South Wales. He is currently working at University College
London. He takes an active interest in cloud computing, software
security, intelligent systems, and child-centric education. He lives
in East London with his wife, Shahinur, and daughter, Ayesha.
All praises and thanks to Allah, the God who is the Merciful and
the Beneficent. I would not be able to finish this book without the
help of God.I would like to thank everyone who has contributed to
the publication of this book, including the publisher, technical
reviewers, editors, my family and friends for their sacrifice of time,
encouraging words, and smiles, especially my wife Shahinur
Rijuani for her love and support in my work. I also thank the
readers who have patiently been waiting for this book and who
have given me lots of valuable feedback.


About the Reviewers
Dr. S. Gowrishankar is currently working as an associate
professor in the Department of Computer Science and Engineering
at Dr. Ambedkar Institute of Technology, Bengaluru, Karnataka,
India.
He received his Ph.D. in Engineering from Jadavpur University,
Kolkata, West Bengal, India in 2010, MTech in software
engineering and BE in computer science and engineering from
Visvesvaraya Technological University (VTU), Belagavi,
Karnataka, India in the year 2005 and 2003 respectively.
From 2011 to 2014 he worked as a senior research scientist and
tech lead at Honeywell Technology Solutions, Bengaluru,
Karnataka, India.
He has published several papers in various reputed international

journals and conferences. He is serving as an editor and reviewer
for various prestigious international journals. He is also a member
of IEEE, ACM, CSI, and ISTE.
He has delivered many keynote addresses and invited talks
throughout India on a variety of subjects related to computer
science and engineering. He was instrumental in organizing several
conferences, workshops, and seminars. He has also served on the
panel of a number of academic bodies of universities and
autonomous colleges as a BOS and BOE member.
His current research interests are mainly focused on data science,
including its technical aspects as well as its applications and


implications. Specifically, he is interested in the applications of
Machine Learning, Data Mining, and Big Data Analytics in
Healthcare.
I would like to acknowledge my earnest gratitude to my wife,
Roopa K M, for her constant source of support and encouragement
throughout this assignment. I'm truly thankful to almighty God for
having her in my life and give her my deepest expression of love
and appreciation.
Michael Bright, RHCE/RHCSA, is a solution architect working in
the HPE EMEA Customer Innovation Center.
He has strong experience across Cloud and Container technologies
(Docker, Kubernetes, AWS, GCP, Azure) as well as NFV/SDN.
Based in Grenoble, France, he runs a Python user group and is a
co-organizer of the Docker and FOSS Meetup groups. He has a
keen interest in Container, Orchestration, and Unikernel
technologies on which he has presented and run training tutorials in
several conferences.

He has presented many times on subjects diverse as NFV, Docker,
Container Orchestration, Unikernels, Jupyter Notebooks,
MongoDB, and Tmux.
Michael has a wealth of experience across pure research, R&D and
pre-sales consulting roles.


www.PacktPub.com
For support files and downloads related to your book, please visit w
ww.PacktPub.com.
Did you know that Packt offers eBook versions of every book
published, with PDF and ePub files available? You can upgrade to
the eBook version at www.PacktPub.comand as a print book
customer, you are entitled to a discount on the eBook copy. Get in
touch with us at for more details.
At www.PacktPub.com, you can also read a collection of free
technical articles, sign up for a range of free newsletters and
receive exclusive discounts and offers on Packt books and eBooks.

/>
Get the most in-demand software skills with Mapt. Mapt gives you
full access to all Packt books and video courses, as well as
industry-leading tools to help you plan your personal development
and advance your career.


Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser



Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the
heart of our editorial process. To help us improve, please leave us
an honest review on this book's Amazon page at zon.
com/dp/1786463997.
If you'd like to join our team of regular reviewers, you can e-mail
us at We award our regular
reviewers with free eBooks and videos in exchange for their
valuable feedback. Help us be relentless in improving our products!


I dedicate this book to the world, in memory of my dad,
Kanapathipillai Kathiravelu.


Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Sections
Getting ready
How to do it…
How it works…
There's more…
See also
Conventions
Reader feedback

Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
1. Sockets, IPv4, and Simple Client/Server Programming
Introduction
Printing your machine's name and IPv4 address
Getting ready
How to do it...
How it works...
Retrieving a remote machine's IP address
How to do it...
How it works...
Converting an IPv4 address to different formats
How to do it...


How it works...
Finding a service name, given the port and protocol
Getting ready
How to do it...
How it works...
Converting integers to and from host to network byte order
How to do it...
How it works...
Setting and getting the default socket timeout
How to do it...
How it works...

Handling socket errors gracefully
How to do it...
How it works...
Modifying a socket's send/receive buffer sizes
How to do it...
How it works...
Changing a socket to the blocking/non-blocking mode
How to do it...
How it works...
Reusing socket addresses
How to do it...
How it works...
Printing the current time from the internet time server
Getting ready
How to do it...
How it works...
Writing an SNTP client
How to do it...
How it works...
Writing a simple TCP echo client/server application
How to do it...
How it works...


Writing a simple UDP echo client/server application
How to do it...
How it works...
2. Multiplexing Socket I/O for Better Performance
Introduction
Using ForkingMixIn in your socket server applications

How to do it...
How it works...
Using ThreadingMixIn in your socket server applications
Getting ready
How to do it...
How it works...
Writing a chat server using select.select
How to do it...
How it works...
Multiplexing a web server using select.epoll
How to do it...
How it works...
Multiplexing an echo server using Diesel concurrent library
Getting ready
How to do it...
How it works...
3. IPv6, Unix Domain Sockets, and Network Interfaces
Introduction
Forwarding a local port to a remote host
How to do it...
How it works...
Pinging hosts on the network with ICMP
Getting ready
How to do it...
How it works...
Waiting for a remote network service
How to do it...



×