Network Programming in Python : The Basic
eBook - ePub

Network Programming in Python : The Basic

Network Programming and Management

John Galbraith

Share book
  1. English
  2. ePUB (mobile friendly)
  3. Available on iOS & Android
eBook - ePub

Network Programming in Python : The Basic

Network Programming and Management

John Galbraith

Book details
Book preview
Table of contents
Citations

About This Book

For programmers who need to use Python for network-related activities and apps

Key Features
? Comprehensive coverage of Python 3's improved SSL support.
? Create an asynchronous I/O loop on your own.
? A look at the "asyncio" framework, which is included with Python 3.4.

Description
This book includes revisions for Python 3 as well as all of the classic topics covered, such as network protocols, network data and errors, email, server architecture, and HTTP and web applications. • Comprehensive coverage of Python 3's improved SSL support
• How to create an asynchronous I/O loop on your own.
• A look at the "asyncio" framework, which is included with Python 3.4.
• The Flask web framework's URL-to-Python code connection.
• How to safeguard your website from cross-site scripting and cross-site request forgery attacks.
• How Django, a full-stack web framework, can automate the round journey from your database to the screen and back.

What you will learn
• asynchronous models and socket-based networks
• Monitor distant systems using Telnet and SSH connections
• Interact with websites using XML-RPC, SOAP, and REST APIs
• Configure virtual networks in various deployment scenarios
• Analyze security weaknesses in a network

Who this book is for
This book is for Python programmers who need a thorough understanding of how to use Python for network-related activities and applications. This book covers all you need to know about web application development, systems integration, and system administration.

Table of Contents
1. Client-Server Networking
2. UDP(User Datagram Protocol )
3. Transmission control protocol (TCP)
4. Domain name system & socket names
5. Data and Errors on the Internet
6. SSL/TLS
7. Architecture of the Server
8. Message Queues and Caches
9. HTTP Clients
10. Servers that handle HTTP
11. www (world wide web)
12. E-mail Construction And Parsing
13. Simple Mail Transfer Protocol(SMTP)
14. Post Office Protocol (POP)
15. Internet Message Access Protocol (IMAP)
16. SSH and Telnet
17. File Transfer Protocol (FTP)
18. Remote Procedure Call (RPC)

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Network Programming in Python : The Basic an online PDF/ePUB?
Yes, you can access Network Programming in Python : The Basic by John Galbraith in PDF and/or ePUB format, as well as other popular books in Computer Science & Programming in Python. We have over one million books available in our catalogue for you to explore.

Information

CHAPTER 1

Client-Server Networking: An Overview

The Python language is used to explore network programming in this book. It covers the fundamental principles, modules, and third-party libraries that you’ll need to communicate with remote machines via the Internet using the most common communication protocols.
The book does not have enough room to teach you how to write in Python if you have never seen the language or written a computer program before; instead, it assumes that you have already learned something about Python programming from the numerous great tutorials and books available. I hope the Python examples in this book provide you some ideas for structuring and writing your own code. But I’ll use advanced Python capabilities without explanation or apologies—though I might point out how I’m utilizing a certain approach or construction when I believe it’s particularly intriguing or brilliant.
This book, on the other hand, does not begin by presuming you are familiar with networking! You should be able to start reading this book at the beginning and learn about computer networking along the way if you’ve ever used a web browser or sent an e-mail. I’ll approach networking from the perspective of an application programmer who is either creating a network-connected service—such as a web site, an e-mail server, or a networked computer game—or designing a client software to use one.
This book, on the other hand, will not teach you how to set up or configure networks. The disciplines of network architecture, server room administration, and automated provisioning are separate topics that do not intersect with the discipline of computer programming as it is described in this book. While Python is becoming a big part of the provisioning landscape thanks to projects like OpenStack, SaltStack, and Ansible, if you want to learn more about provisioning and its many technologies, you’ll want to look for books and documentation that are specifically about provisioning and its many technologies.

Structure

  • Layers of Application
  • Talking a protocol
  • A Network Conversation in its Natural State
  • Turtles, Turtles, Turtles
  • The Foundation: Stacks and Libraries
  • The process of encoding and decoding
  • The Internet Protocol (IP)
  • Internet Protocol (IP Addresses)
  • Routing
  • Fragmentation of packets
  • Learning More About internet protocol
  • Conclusion

Objective:

In this chapter you will learn to use layer of application used in python like google geocoding, about internet protocol, how to encode and decode in python, many thing of python libraries, routing etc.

The Foundation: Stacks and Libraries

When you first start learning Python network programming, there are two notions that will come up repeatedly.
  • The concept of a protocol stack, in which basic network services are utilized as the foundation for more complex services to be built.
  • The fact that you’ll frequently be utilizing Python libraries containing previously written code—whether modules from Python’s built-in standard library or packages from third-party distributions you download and install—that already know how to communicate with the network protocol you want to utilize.
In many cases, network programming simply get in choosing and implementing a library that already implements the network functions you require. The main goals of this book are to introduce you to a number of important networking libraries for Python, as well as to teach you about the lower-level network services that those libraries are based on. Knowing the lower-level content is useful both for understanding how the libraries work and for understanding what happens when anything goes wrong at a lower level.
Let’s start with a basic example. The following is a mailing address:
Taj mahal
Agra, Uttar Pradesh
This physical address’s latitude and longitude are of importance to me. Google, fortunately, has a Geocoding API that can do such a conversion. What would you need to do in order to take advantage of Python’s network service?
When considering a new network service, it’s always a good idea to start by seeing if someone has already developed the protocol that your software will need to speak—in this example, the Google Geocoding protocol. Begin by going over the Python Standard Library’s documentation for everything related to geocoding.
https://docs.python.org/3/library/
Is there any mention of geocoding? I don’t think so, either. Even if you don’t always find what you’re searching for, it’s necessary for a Python programmer to check through the Standard Library’s table of contents on a regular basis because each read-through will help you get more comfortable with the Python services.
Doug Hellmann’s “Python Module of the Week” blog is another excellent resource for learning about Python’s possibilities thanks to its Standard Library.
Because the Standard Library does not offer a package to assist you in this scenario, you can look for general-purpose Python packages on the Python Package Index, which is a wonderful resource for locating packages provided by other programmers and organizations from all over the world. Of course, you may look on the website of the vendor whose service you’ll be using to see if it has a Python library for accessing it. Alternatively, you may run a generic Google search for Python plus the name of whatever web service you wish to utilize and see if any of the first few results point to a package you should try.
In this example, I used the Python Package Index, which can be found at the following address:
https://pypi.org/
I typed in geocoding and found a package called pygeocoder, which provides a nice interface to Google’s geocoding features (albeit, as its description indicates, it is not vendor-provided but rather was built by someone other than Google).
https://pypi.org/project/pygeocoder/
Because this is such a typical scenario—finding a Python package that sounds like it might already do precisely what you’re looking for and wanting to try it out on your system—I thought I’d take a time to introduce you to the best Python technology for fast trying out new libraries: virtualenv!
Installing a Python package used to be a painful and irrevocable process that necessitated administrative intervention.
privileges on your machine, and as a result, your Python installation on your system has been permanently altered. After numerous months of preparation,
If you’re doing a lot of Python work, your system Python installation could end up being a wasteland of dozens of packages, all installed at the same time.
by hand, and you may find that any new packages you try to install may fail due to incompatibility.
with the outdated packages from a project that ended months ago hanging on your hard drive
Python programmers who are cautious are no longer in this predicament. Many of us only ever install virtualenv as a system-wide Python package. Once virtualenv is installed, you can build as many small, self-contained “virtual Python environments” as you like, where you can install and uninstall packages and experiment without polluting your systemwide Python. When a project or experiment is completed, you just delete the virtual environment directory associated with it, and your system is clean.
You’ll need to establish a virtual environment to test the pygeocoder package in this situation. If this is the first time you’ve installed virtualenv on your machine, go to this URL to download and install it:
https://pypi.org/project/virtualenv/
After you’ve installed virtualenv, use the following instructions to establish a new environment. (On Windows, the virtual environment’s Python binary directory will be called Scripts rather than bin.)
$ virtualenv –p python3 geo_env
$ cd geo_env
$ ls
bin/ include/ lib/
$ . bin/activate
$ python -c ‘import pygeocoder’
Traceback (most recent call last):
File “<string>”, line 1, in
ImportError: No module named ‘pygeocoder’
The pygeocoder package is not yet available, as you can see. To install it, use the pip command from within your virtual environment, which is now on your path as a result of the activate command you ran.
$ pip install pygeocoder
Downloading/unpacking pygeocoder
Downloading pygeocoder-1.2.1.1.tar.gz
Running setup.py egg_info for package pygeocoder
Downloading/unpacking requests>=1.0 (from pygeocoder)
Downloading requests-2.0.1.tar.gz (412kB): 412kB downloaded
Running setup.py egg_info for package requests
Installing collected packages: pygeocoder, requests
Running setup.py install for pygeocoder
Running setup.py install for requests
Successfully installed pygeocoder requests
2
The pygeocoder package will now be available in the virtualenv’s python binary.
$ python -c ‘import pygeocoder’
Now that you’ve installed the pygeocoder package, you should be able to run the search1.py programme, as shown in Listing 1-1.
Listing 1-1: Obtaining a Longitude and Latitude
#!/usr/bin/env python3
# Network Programming in Python: The Basics
from pygeocoder import Geocoder
if __name__ == ‘__main__’:
address = taj mahal’
print(Geocoder.geocode(address)[0].coordinates)
By running it at the command line, you should see a result like this:
$ python3 search1.py (27.1751° N, 78.0421° E)
And there it is, right there on your computer screen, the answer to our inquiry concerning the latitude and longitude of the address! The information was obtained directly from Google’s web site. The first sample software was a huge hit.
Are you frustrated that you opened a book on Python network programming only to be instructed to download and install a third-party package that turned a potentially intriguing networking challenge into a tedious three-line Python script? Relax and unwind! Ninety percent of the time, you’ll discover that this is how programming problems are addressed—by locating other Python programmers who have already solved the problem you’re encountering and then building smartly and succinctly on their solutions.
However, you are not quite finished with this example. You’ve seen how a complicated network service can be accessed with relative ease. But what lies beneath the attractive pygeocoder user interface? What is the procedure for using the service? You’ll now learn more about how this complex service is actually just the top tier of a network stack with at least a half-dozen additional layers.

Layers of Application

To tackle a problem, the first application listed employed a third-party Python library acquired from the Python Package Index. It was well-versed in the Google Geocoding API and its usage guidelines. But what if that library didn’t exist at all? What if you had to create your own client for Google’s Maps API?
Look at search2.py, which is shown in Listing 1-2, for the answer. Instead of employing a geocoding-aware third-party library, it uses the popular requests library, which is the foundation for pygeocoding and, as you can see from the pip install line, is already installed in your virtual environment.
Listing 1-2. Using the Google Geocoding API to get a JSON Document
#!/usr/bin/env python3
# Network Programming in Python: The Basics
import requests
def geocode(address):
base = ‘https://nominatim.openstreetmap.org/search’
parameters = {‘q’: address, ‘format’: ‘json’}
user_agent = ‘ Client-Server Networking: An Overview search2.py’
headers = {‘User-Agent’: user_agent}
response = requests.get(base, params=parameters, headers=headers)
reply = response.json()
print(reply[0][‘lat’], reply[0][‘lon’])
if __name__ == ‘__main__’:
geocode(‘taj mahal’)
When you run this Python program, you’ll get a result that’s very similar to the first script.
$ python3 search2.py
{‘lat’: 27.1751° N, ‘lng’: - 78.0421° E }
The results aren’t identical—for example, you can see that the JSON data encoded the result as a “object” that requests has handed to you as a Python dictionary. However, it is evident that this script achieves roughly the same result as the previous one.
The first thing you’ll notice about this code is that the higher-level pygeocoder module’s semantics are missing. If you don’t look attenti...

Table of contents