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...