www.it-ebooks.info Python Network Programming Cookbook Over 70 detailed recipes to develop practical solutions for a wide range of real-world network programming tasks Dr. M. O. Faruque Sarker BIRMINGHAM - MUMBAI www.it-ebooks.info Python Network Programming Cookbook Copyright © 2014 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 author, 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 Production Reference: 1190314 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-84951-346-3 www.packtpub.com Cover Image by Gabrielay La Pintura ([email protected]) www.it-ebooks.info Credits Author Project Coordinator Dr. M. O. Faruque Sarker Sanchita Mandal Reviewers Proofreaders Ahmed Soliman Farghal Faye Coulman Vishrut Mehta Paul Hindle Tom Stephens Joanna McMahon Deepak Thukral Indexer Mehreen Deshmukh Acquisition Editors Aarthi Kumarswamy Owen Roberts Production Coordinator Nilesh R. Mohite Content Development Editor Arun Nadar Cover Work Nilesh R. Mohite Technical Editors Manan Badani Shashank Desai Copy Editors Janbal Dharmaraj Deepa Nambiar Karuna Narayanan www.it-ebooks.info About the Author Dr. M. O. Faruque Sarker is a software architect, and DevOps engineer who's currently working at University College London (UCL), United Kingdom. In recent years, he has been leading a number of Python software development projects, including the implementation of an interactive web-based scientific computing framework using the IPython Notebook service at UCL. He is a specialist and an expert in open source technologies, for example, e-learning and web application platforms, agile software development, and IT service management methodologies such as DSDM Atern and ITIL Service management frameworks. Dr. Sarker received his PhD in multirobot systems from University of South Wales where he adopted various Python open source projects for integrating the complex software infrastructure of one of the biggest multirobot experiment testbeds in UK. To drive his multirobot fleet, he designed and implemented a decoupled software architecture called hybrid event-driven architecture on D-Bus. Since 1999, he has been deploying Linux and open source software in commercial companies, educational institutions, and multinational consultancies. 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. Currently, Dr. Sarker has a research interest in self-organized cloud architecture. In his spare time, he likes to play with his little daughter, Ayesha, and is keen to learn about child-centric educational methods that can empower children with self-confidence by engaging with their environment. I would like to thank everyone who has contributed to the publication of this book, including the publisher, technical reviewers, editors, friends, and my family members, specially 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. www.it-ebooks.info About the Reviewers Ahmed Soliman Farghal is an entrepreneur and software and systems engineer coming from a diverse background of highly scalable applications design, mission-critical systems, asynchronous data analytics, social networks design, reactive distributed systems, and systems administration and engineering. He has also published a technology patent in distributed computer-based virtual laboratories and designed numerous large-scale distributed systems for massive-scale enterprise customers. A software engineer at heart, he is experienced in over 10 programming languages, but most recently, he is busy designing and writing applications in Python, Ruby, and Scala for several customers. He is also an open source evangelist and activist. He contributed and maintained several open source projects on the Web. Ahmed is a co-founder of Cloud Niners Ltd., a software and services company focusing on highly scalable cloud-based applications that have been delivering private and public cloud computing services to customers in the MEA region on different platforms and technologies. A quick acknowledgment to some of the people who changed my entire life for the better upon meeting or working with them; this gratitude does not come in a specific order but resembles a great appreciation for their support, help, and influence through my personal life and professional career. I would also like to thank Prof. Dr. Soliman Farghal, my father, for his continuous help and support and giving me an opportunity to play with a real computer before I was able to speak properly and Sinar Shebl, my wife; she has been of great help and a deep source of inspiration. www.it-ebooks.info Vishrut Mehta has been involved in open source development since two years and contributed to various organizations, such as Sahana Software Foundation, GNOME, and E-cidadania; he has participated in Google Summer of Code last year. He is also the organization administrator for Google Code-In and has been actively involved in other open source programs. He is a dual degree student at IIIT Hyderabad, and now he is pursuing his research under Dr. Vasudeva Varma on topics related to Cloud Computing, Distributed Systems, Big Data, and Software Defined Networks. I would like to thank my advisors, Dr. Venkatesh Choppella and Dr. Vasudeva Varma, who showed me the direction in my work and helped me a lot. I would also like to thank my Google Summer of Code mentor, Patirica Tressel. Tom Stephens has worked in software development for nearly 10 years and is currently working in embedded development dealing with smartcards, cryptography, and RFID in the Denver metro area. His diverse background includes experience ranging from embedded virtual machines to web UX/UI design to enterprise Business Intelligence. He is most passionate about good software design, including intelligent testing and constantly evolving practices to produce a better product with minimal effort. Deepak Thukral is a polyglot who is also a contributor to various open source Python projects. He moved from India to Europe where he worked for various companies helping them scale their platforms with Python. www.it-ebooks.info www.PacktPub.com Support files, eBooks, discount offers, and more You might want to visit www.PacktPub.com for support files and downloads related to your book. 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.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at [email protected] 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. TM http://PacktLib.PacktPub.com Do you need instant solutions to your IT questions? PacktLib is Packt's online digital book library. Here, you can access, read and search across Packt's entire library of books. Why subscribe? f Fully searchable across every book published by Packt f Copy and paste, print and bookmark content f On demand and accessible via web browser Free access for Packt account holders If you have an account with Packt at www.PacktPub.com, you can use this to access PacktLib today and view nine entirely free books. Simply use your login credentials for immediate access. www.it-ebooks.info www.it-ebooks.info Table of Contents Preface 1 Chapter 1: Sockets, IPv4, and Simple Client/Server Programming 7 Introduction 8 Printing your machine's name and IPv4 address 8 Retrieving a remote machine's IP address 10 Converting an IPv4 address to different formats 12 Finding a service name, given the port and protocol 13 Converting integers to and from host to network byte order 14 Setting and getting the default socket timeout 15 Handling socket errors gracefully 16 Modifying socket's send/receive buffer sizes 20 Changing a socket to the blocking/non-blocking mode 21 Reusing socket addresses 23 Printing the current time from the Internet time server 25 Writing a SNTP client 26 Writing a simple echo client/server application 27 Chapter 2: Multiplexing Socket I/O for Better Performance 31 Introduction 31 Using ForkingMixIn in your socket server applications 32 Using ThreadingMixIn in your socket server applications 35 Writing a chat server using select.select 38 Multiplexing a web server using select.epoll 45 Multiplexing an echo server using Diesel concurrent library 49 Chapter 3: IPv6, Unix Domain Sockets, and Network Interfaces 53 Introduction 54 Forwarding a local port to a remote host 54 Pinging hosts on the network with ICMP 58 Waiting for a remote network service 62 www.it-ebooks.info