O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Learning Python Network Programming

Book Description

Utilize Python 3 to get network applications up and running quickly and easily

In Detail

Network programming has always been a demanding task. With full-featured and well documented libraries all the way up the stack, Python makes network programming the enjoyable experience it should be.

Starting with a walkthrough of today's major networking protocols, with this book you'll learn how to employ Python for network programming, how to request and retrieve web resources, and how to extract data in major formats over the Web. You'll utilize Python for e-mailing using different protocols and you'll interact with remote systems and IP and DNS networking.

As the book progresses, socket programming will be covered, followed by how to design servers and the pros and cons of multithreaded and event-driven architectures. You'll develop practical client-side applications, including web API clients, e-mail clients, SSH, and FTP. These applications will also be implemented through existing web application frameworks.

What You Will Learn

  • Develop an understanding of network stacks and the power of encapsulation
  • Design high-performance network server applications
  • Implement socket-based network applications using asynchronous models
  • Build client applications for major web APIs, including Amazon S3 and Twitter
  • Interact with e-mail servers using SMTP, POP3, and IMAP protocols
  • Deal with remote network servers using SSH, FTP, SNMP, SMB/CIFS, and LDAP protocols
  • Work with IP addresses including Geo-IP lookups
  • Download objects from the Web and craft custom HTTP requests with urllib and the Requests library

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the files e-mailed directly to you.

Table of Contents

  1. Learning Python Network Programming
    1. Table of Contents
    2. Learning Python Network Programming
    3. Credits
    4. About the Authors
    5. About the Reviewers
    6. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    7. Preface
      1. What this book covers
      2. What you need for this book
        1. Virtual environments
        2. Installing Python 3
          1. Ubuntu and Debian
          2. RHEL, CentOS, Scientific Linux
          3. Fedora
        3. Alternative installation methods
          1. Pythonz
          2. JuJu
        4. Windows
        5. Other requirements
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code
        2. Errata
        3. Piracy
        4. Questions
    8. 1. Network Programming and Python
      1. An introduction to TCP/IP networks
        1. IP addresses
        2. Network interfaces
        3. Assigning IP addresses
        4. IP addresses on the Internet
        5. Packets
        6. Networks
        7. Routing with IP
        8. DNS
        9. The protocol stack or why the Internet is like a cake
        10. Layer 4 – TCP and UDP
          1. Network ports
          2. UDP
          3. TCP
          4. UDP versus TCP
        11. Layer 5 – The application layer
        12. On to Python!
      2. Network programming with Python
      3. Breaking a few eggs
        1. Taking it from the top
        2. Downloading an RFC
        3. Looking deeper
        4. Programming for TCP/IP networks
          1. Firewalls
          2. Network Address Translation
          3. IPv6
      4. Summary
    9. 2. HTTP and Working with the Web
      1. Request and response
      2. Requests with urllib
      3. Response objects
      4. Status codes
      5. Handling problems
      6. HTTP headers
      7. Customizing requests
        1. Content compression
        2. Multiple values
      8. Content negotiation
        1. Content types
      9. User agents
      10. Cookies
        1. Cookie handling
        2. Know your cookies
      11. Redirects
      12. URLs
        1. Paths and relative URLs
        2. Query strings
        3. URL encoding
        4. URLs in summary
      13. HTTP methods
        1. The HEAD method
        2. The POST method
      14. Formal inspection
      15. HTTPS
      16. The Requests library
        1. Handling errors with Requests
      17. Summary
    10. 3. APIs in Action
      1. Getting started with XML
        1. The XML APIs
        2. The basics of ElementTree
          1. Pretty printing
          2. Element attributes
          3. Converting to text
      2. The Amazon S3 API
        1. Registering with AWS
        2. Authentication
          1. Setting up an AWS user
        3. Regions
        4. S3 buckets and objects
        5. An S3 command-line client
          1. Creating a bucket with the API
          2. Uploading a file
          3. Retrieving an uploaded file through a web browser
          4. Displaying an uploaded file in a web browser
          5. Downloading a file with the API
        6. Parsing XML and handling errors
          1. Parsing XML
          2. Finding elements
          3. Handling errors
        7. Further enhancements
        8. The Boto package
        9. Wrapping up with S3
      3. JSON
        1. Encoding and decoding
        2. Using dicts with JSON
        3. Other object types
      4. The Twitter API
        1. A Twitter world clock
        2. Authentication for Twitter
          1. Registering your application for the Twitter API
          2. Authenticating requests
          3. A Twitter client
        3. Polling for Tweets
        4. Processing the Tweets
        5. Rate limits
        6. Sending a reply
        7. Final touches
        8. Taking it further
          1. Polling and the Twitter streaming APIs
        9. Alternative oAuth flows
      5. HTML and screen scraping
        1. HTML parsers
        2. Show me the data
        3. Parsing HTML with lxml
        4. Zeroing in
        5. Searching with XPath
          1. XPath conditions
        6. Pulling it together
      6. With great power...
        1. Choosing a User Agent
        2. The Robots.txt file
      7. Summary
    11. 4. Engaging with E-mails
      1. E-mail terminologies
      2. Sending e-mails with SMTP
        1. Composing an e-mail message
        2. Sending an e-mail message
      3. Sending e-mails securely with TLS
      4. Retrieving e-mails by using POP3 with poplib
      5. Retrieving e-mails by using IMAP with imaplib
      6. Sending e-mail attachments
      7. Sending e-mails via the logging module
      8. Summary
    12. 5. Interacting with Remote Systems
      1. Secure shell – access using Python
        1. Inspecting the SSH packets
      2. Transferring files through SFTP
      3. Transferring files with FTP
        1. Inspecting FTP packets
      4. Fetching Simple Network Management Protocol data
        1. Inspecting SNMP packets
      5. Reading Light-weight Directory Access Protocol data
        1. Inspecting LDAP packets
      6. Sharing files with SAMBA
        1. Inspecting SAMBA packets
      7. Summary
    13. 6. IP and DNS
      1. Retrieving the network configuration of a local machine
      2. Manipulating IP addresses
        1. IP network objects
        2. Network interface objects
        3. The IP address objects
        4. Planning IP addresses for your local area network
      3. GeoIP look-ups
        1. DNS look-ups
        2. Inspecting DNS client/server communication
      4. NTP clients
        1. Inspecting the NTP client/server communication
      5. Summary
    14. 7. Programming with Sockets
      1. Basics of sockets
      2. Working with TCP sockets
        1. Inspecting the client/server communication
        2. TCP servers
        3. Inspecting client/server interaction
      3. Working with UDP sockets
      4. TCP port forwarding
      5. A non-blocking socket I/O
      6. Securing sockets with TLS/SSL
        1. Inspecting standard SSL client/server communication
      7. Creating a custom SSL client/server
        1. Inspecting interaction between a custom SSL client/server
      8. Summary
    15. 8. Client and Server Applications
      1. Client and server
      2. An echo protocol
        1. Framing
      3. A simple echo server
        1. Handling the received data
        2. The server itself
      4. A simple echo client
      5. Concurrent I/O
      6. Multithreading and multiprocessing
        1. Threading and the GIL
      7. A multithreaded echo server
      8. Designing a chat server
      9. A chat protocol
      10. Handling data on persistent connections
      11. A multithreaded chat server
        1. Queues
        2. Locks
      12. A multithreaded chat client
      13. Event-driven servers
      14. A low-level event-driven chat server
      15. Frameworks
      16. An eventlet-based chat server
      17. An asyncio-based chat server
      18. More on frameworks
      19. Taking our servers forward
      20. Summary
    16. 9. Applications for the Web
      1. What's in a web server?
      2. Python and the Web
        1. Web frameworks
      3. Flask – a microframework
        1. Templating
        2. Other templating engines
        3. Adding some style
        4. A note on security
          1. XSS
          2. CSRF
        5. Finishing up with frameworks
      4. Hosting Python web applications
        1. CGI
        2. Recycling for a better world
        3. Event-driven servers
        4. WSGI
      5. Hosting in practice
      6. Summary
    17. A. Working with Wireshark
      1. Packet sniffers
      2. Wireshark
        1. Installation
        2. Capturing some packets
        3. Filtering
        4. Inspecting packets
        5. A versatile tool
    18. Index