NETWORK PROGRAMMING IN PYTHON

Common Networking Terminologies

Let's quickly go through the frequently used terminologies along with their meanings which are very important for network programming. In case you already know about them, then quickly go through them, and do let us know if we missed any point.


Internet Protocol

These are the set of procedures or rules which govern the flow of data, format of data over the internet.

Let's understand this using a simple example: Suppose there are two friends A and B. If A wants to send a letter to B. What would he do? A will go to the Post Office(kind of a communnication network), put the letter in the envelop and submit it in the Post Office for delivery to B. Now, it's the responsibility of the Post Office to safely deliver the letter to B. Now suppose, A wants to send a script, comprising of 20 pages to B. Since there is a constraint that one envelop can fit at most one page so, A will have to post all the 20 pages in separate envelops, one by one. Now what is the guarantee that the Post Office will deliver the letters in the same order as they were supposed to be? Here comes the Internet Protocols.

Mainly, we will be dealing with two major protocols over the internet:

  • User Datagram Protocol(UDP)
  • Transmission Control Protocol(TCP)

User Datagram Protocol (UDP)

UDP is a connectionless protocol. In this protocol data is sent over the internet as datagrams. In the previous example, 20 pages may refer to 20 datagrams. Let's have a look at some properties of the UDP protocols.

  • Unreliable: When a UDP message is sent, there is no way to know if it will reach its destination or not; it could get lost along the way. In UDP, there is no concept of acknowledgment, retransmission, or timeout (as in TCP).
  • Not ordered: If two messages are sent to the same recipient, the order in which they arrive cannot be predicted.
  • Lightweight: There is no ordering of messages, no tracking connections, etc. Hence UDP messages are used when the rate of data transmission required is more and relibility is not important.
  • Datagrams: Packets are sent individually and are checked for integrity only if they arrive.

Let's say that in the example above, B receives all the pages, except the envelope number 13. He can then ask A to resend envelope number 13, but then how would B know, when to request for the missing envelop? It is possible, that as soon as B sends a request to A, to resend the envelope number 13, the post office delivers the missing envelope. There can be many reasons for the delayed delivery. But because in UDP, there is no concept of acknowledgement or retransmission, hence, the only way is to wait, and then ask for the lost datagram again.

So we need a better way to make sure that B receives all the data properly. Fortunately, in computer networking, UDP is not the only protocol to send data amongst computers.


Transmission Control Protocol (TCP)

In TCP there is a concept of handshake. So, What is a handshake? It's a way to ensure that the connection has been established between interested hosts and therefore data transfer can be initiated.

In simple terms, when you make a phone call to someone you first say "Hello", and in return the person replies with a "Hello". This ensures that the connection has been established between both the parties and data(voice in this case)transfer can begin now. This is, for sure, the simplest example of handshake.

Unlike UDP, TCP protocol requires that you establish a connection first. So if A is to send a letter using TCP protocol to B, he has to first establish a connection with B. Once a connection is established, A can then send the first envelope and wait for B to acknowledge that he has received it. Once A gets the acknowledgment from B that the envelope 1 is safely received, A can send envelope 2. The process repeats until A tells B that he has sent all the envelopes.

On the basis of the above example we can device the properties of TCP:

  • Reliable: TCP manages message acknowledgment, retransmission and timeout. Multiple attempts to deliver the message are made. If it gets lost along the way, the server will re-request the lost part.
  • Ordered: The messages are delivered in a particular order in which they were meant to be.
  • Heavyweight: TCP requires three packets to set up a socket connection, before any user data can be sent. The three packets are: SYN, SYN+ACK and ACK.

How TCP Connection gets initialized


IP Addressess and Ports

IP addressess are the addressess which helps to uniquely identify a device over the internet and Port is an endpoint for the communication in an operating system. Confused! Let's try to make it simple to understand.

In the above example where A sends a letter to B. A needs to know the postal address of B in order to successfully deliver the package. This postal address is unique and hence the mail man delivers the letter successfully. IP address is like the postal address. Now, suppose that B has a bunglow of 5 rooms and in each room a relative of B resides. One of the relative of B orders a pizza. Now when the pizza boy arrives to the given address, how will he come to know who ordered pizza amongst the 5 relatives? So in order to uniquely identify the person who ordered the pizza, the delivery boy needs room number. This room number is Port number in computer networks.

A system might be running thousands of services but to uniquely identify a service on a system the application requires a port number. There are total of 0 – 65535 ports on a system.

Port numbers are sometimes seen in web or other uniform resource locators (URLs) as well. By default, HTTP uses port 80 and HTTPS uses port 443, but a URL like http://www.example.com:8080/path/ specifies that the web browser connects instead to port 8080 of the HTTP server.

Some common ports are:

  • 22: Secure Shell(SSH)
  • 23: Telnet Remote Login Service
  • 25: Simple Mail Transfer Protocol(SMTP)
  • 53: Domain Name System(DNS) Service
  • 80: Hyper Text Transfer Protocol(HTTP) used in the World Wide Web

Now, coming up is some more detail about the IP addressess. IP address is of two types:

  1. Private IP address: Ranges from (192.168.0.0192.168.255.255), (172.16.0.0172.31.255.255) or (10.0.0.0 - 10.255.255.255)
  2. Public IP address: A public IP address is an IP address that your home or business router receives from your ISP(Internet Service Provider).

Firewall

The firewall monitors the incoming and outgoing network traffic based on predefined security rules like scanning digital signature, checking for valid file formats etc. It establishes a barrier between a trusted, secure internal network and another outside network, such as the Internet, that is assumed to be not secure or trusted.

Internet, Firewall and your network



Why Python for Network Programming?

There are several reasons for using Python for this course. The simplicity of python makes it the most powerful language. Python is syntactically simplest to implement amongst it's counterparts.

Also, you can do almost everything with python. 'Ohh.. Almost everything, Can we make a website using python? Can we make face detection application using python? can we make our own personal assistant using python? Can we make a penetration testing tool using python?

The answer to all of the above questions is a big YES!

The third party libraries support provided by python makes it limitless. There is a proper documentation for the third party libraries as well, hence using them in your application becomes easier.

Lastly, python is powerful enough to make websites like Quora and provide the backbone for the Google search engine, so yes, python is the perfect choice for network programming.