0% found this document useful (0 votes)
6 views33 pages

Networking in Python - v2

The document provides an overview of networking in Python, explaining the concepts of computer networks, protocols, sockets, and the roles of clients and servers. It details how to create TCP/IP and UDP servers and clients, including socket programming, handling IP addresses, and using URLs. Additionally, it covers file servers and clients, as well as the introduction of threading in Python programs.

Uploaded by

zooquest007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views33 pages

Networking in Python - v2

The document provides an overview of networking in Python, explaining the concepts of computer networks, protocols, sockets, and the roles of clients and servers. It details how to create TCP/IP and UDP servers and clients, including socket programming, handling IP addresses, and using URLs. Additionally, it covers file servers and clients, as well as the introduction of threading in Python programs.

Uploaded by

zooquest007
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Networking using

PYTHON
NETWORKING IN PYTHON
Interconnection of computers is called a computer network. A
simple network can be formed by connecting two computers using
a cable. Thus, a network can have two computers or two thousand
computers.

For example, Internet is the largest network on the earth where


millions of computers are connected.

The main advantage of a network is sharing the resources. The


data and files available in a computer can be shared with other
computers in the network.
NETWORKING IN PYTHON
There are three requirements to establish a network:

• Hardware: Includes the computers, cables, modems, routers,


hubs, etc.
• Software: Includes programs to communicate between
servers and clients.
• Protocol: Represents a way to establish connection and helps
in sending and receiving data in a standard format.
Protocol
A protocol represents a set of rules to be followed by every computer on the
network. Protocol is useful to physically move data from one place to
another place on a network. While sending data or receiving data, the
computers in the network should follow some rules.

For example, if a computer wants to send a file on a network, it is not


possible to send the entire file in a single step. The file should be broken into
small pieces and then only they can be sent to other computer. The following
are the two types of protocols.

• TCP/IP Protocol- connection oriented


• UDP- connectionless
NETWORKING IN PYTHON
In a network, some computers receive data or services from
other computers. The computers that receive services are called
‘client’ machines. Other computers which provide their services
in the network are called ‘server’ machines.

To send data from one place to another on a network, first of all


the computers should be correctly identified/technically named
on the network. This is done with the help of IP addresses. An IP
address is a unique identification number given to every
computer on the network. It contains four integer numbers in
the range of 0 to 255 and separated by a dot, as shown below

[Link]
NETWORKING IN PYTHON
[Link]
This IP address may represent, for example a website on a server machine on
Internet as:
[Link]

Therefore, to open ‘[Link]’ site, we can type the site address as


‘[Link]’ or its IP address as ‘[Link]‘.

But when we type the IP address in numeric form, that number is mapped to
the website automatically. This mapping service is available on Internet, which
is called ‘DNS’ (Domain Naming service or system).

So, Domain Naming System (DNS) is a service on Internet that maps the IP
addresses with corresponding website names. On Internet, IP addresses of 4
bytes are used and this version is called IP address version 4. The next new
version of IP address is version 6, which uses 16 bytes to identify a computer.
Sockets
It is possible to establish a logical connecting point between a
server and a client so that communication can be done through
that point.

This point is called ‘socket’. Each socket is given an identification


number, which is called ‘port number’. Port number takes 2 bytes
and can be from 0 to 65,535.

Establishing communication between a server and a client using


sockets is called ‘socket programming’.

A new port number is assigned for each new socket. Similarly, we


should allot a new port number depending on the service
provided on a socket.
Sockets
A new port number is assigned for each new socket. Similarly, we
should allot a new port number depending on the service
provided on a socket.

The port numbers from 0 to 1023 are used by our computer


system for various applications (or services) and hence we should
avoid using these port numbers in our networking programs.
Reserved Sockets
Port Number Application or Service
13 Date and time services
21 FTP which transfers files
23 Telnet, which provides remote login
25 SMTP, which delivers mails
67 BOOTP, which provides configuration at boot time
80 HTTP, which transfers web pages
109 POP2, which is a mailing service
110 POP3, which is a mailing service
119 NNTP, which is for transferring news articles
443 HTTPS, which transfers web pages securely
Sockets
A server on a network uses socket to connect to a client. When
the server wants to communicate with several clients
simultaneously, several sockets are needed. Every socket will have
a different port number, as shown in figure below.
Sockets
To create a socket, Python provides socket module. The socket() function of
socket module is useful to create a socket object as:

s = [Link](address_family, type)

Here, the first argument ‘address_family’ indicates which version of the IP


address should be used, whether IP address version 4 or version 6. This
argument can take either of the following two values:

socket.AF_INET # Internet protocol (IPv4) – this is default


socket.AF_INET6 # Internet protocol (IPv6)

The second argument is ‘type’ which represents the type of the protocol to be
used, whether TCP/IP or UDP. This can assume one of the following two values:

socket.SOCK_STREAM # for TCP – this is default


socket.SOCK_DGRAM # for UDP
Knowing IP Address
To know the IP Address of a website on Internet, we can use
gethostbyname() function available in socket module. This
function takes the website name and returns its IP Address.

For example,

addr = [Link]('[Link]')

will return the IP Address of [Link] website into ‘addr’


variable.

If there is no such website on Internet, then it returns ‘gaierror’


(Get Address Information Error).

Note: Import socket module


URL
URL (Uniform Resource Locator) represents the address that is specified
to access some information or resource on Internet.

Look at the example URL:

[Link]

The URL contains four parts:

1. The protocol to use ([Link]


2. The server name or IP address of the server
([Link]).
3. The third part represents port number, which is optional (:80).
4. The last part is the file that is referred. This would be generally
[Link] or [Link] file (/[Link]).
URL parsing
When a URL is given, we can parse the URL and find out all the parts of
the URL with the help of urlparse() function of [Link] module in
Python. We can pass the URL to urlparse() function, it returns a tuple
containing the parts of the URL.

tpl = [Link](‘urlstring’)

We can retrieve the individual parts of the URL from the tuple ‘tpl’ using
the following attributes:

1. scheme = this gives the protocol name used in the URL.


2. netloc = gives the website name on the net with port number if
present.
3. path = gives the path of the web page.
4. port = gives the port number.
Reading the Source Code of a Web Page
If we know the URL of a Web page, it is possible to get the source code
of the web page with the help of urlopen() function. This function
belongs to [Link] module. When we provide the URL of the web
page, this function stores its source code into a file-like object and
returns it as:

file = [Link]("[Link]

Now, using read() method on ‘file’ object, we can read the data of that
object. While executing this program, Internet connection should be
switched ON in the computer.
Downloading a Web Page from Internet
It is also possible to download the entire web page from Internet and save
it into our computer system. Of course, the images of the web page may
not be downloaded.

First we should open the web page using the urlopen() function. This
function returns the content of the web page into a file like object. Now,
we can read from this object using read() method as:

file = [Link]("[Link]
content = [Link]()
Downloading a Web Page from Internet
The urlopen() function can raise ‘[Link]’ if the web page
is not found. The next step is to open a file and write the ‘content’ data
into that file. Since web pages contain binary data, to store the web
pages, we have to open the file in binary write mode as:

f = open('[Link]', 'wb') # we want to write data into [Link]


[Link](content) # write it
A TCP/IP Server
A server is a program that provides services to other computers on the
network or Internet. Similarly a client is a program that receives services
from the servers. When a server wants to communicate with a client, there
is a need of a socket. A socket is a point of connection between the server
and client. The following are the general steps to be used at server side:

1. Create a TCP/IP socket at server side using socket() function.

s = [Link](socket.AF_INET, socket.SOCK_STREAM)

Here, socket.AF_INET represents IP Address version 4 and


socket.SOCK_STREAM indicates that we are using TCP/IP protocol for
communication with client. Anyhow, a socket uses IP address version 4 and
TCP/IP protocol by default. Hence, we can create a socket without giving
protocol version and type of the protocol as:

s = [Link]() # this uses TCP/IP protocol by default


A TCP/IP Server
2. Bind the socket with host name and port number using bind() method.
Here, host name can be an IP Address or a web site name.

If we want to run this program in an individual computer that is not


connected in any network, then we can take host name as ‘localhost’. This
represents that the server is running in the local system and not on any
network. The IP Address of the ‘localhost’ is [Link]. As an alternative to
‘localhost’, we can also mention this IP Address. bind() method is used in
the following format:

[Link]((host, port)) # here, (host, port) is a tuple.

3. We can specify maximum number of connections using listen() method


as:
[Link](5); # maximum connections allowed are 5
A TCP/IP Server
4. The server should wait till a client accepts connection. This is done using
accept() method as:
c, addr = [Link]() # this method returns c and addr
Here, ‘c’ is connection object that can be used to send messages to the
client. ‘addr’ is the address of the client that has accepted the connection.

5. Finally, using send() method, we can send message strings to client. The
message strings should be sent in the form of byte streams as they are used
by the TCP/IP protocol.

[Link](b"message string")

Observe the ‘b’ prefixed before the message string. This indicates that the
string is a binary string. The other way to convert a string into binary format
is using encode() method, as:

[Link]()
A TCP/IP Server
6. After sending the messages to the client, the server can be
disconnected by closing the connection object as:

[Link]()
A TCP/IP Client
A client is a program that receives the data or services from the server. We use
the following general steps in the client program:

1. At the client side, we should first create the socket object that uses TCP/IP
protocol to receive data. This is done as:

s = [Link](socket.AF_INET, socket.SOCK_STREAM)

2. Connect the socket to the server and port number using connect() method.

[Link]((host, port))

3. To receive the messages from the server, we can use recv() method as:

msg = [Link](1024)
A TCP/IP Client
3. To receive the messages from the server, we can use recv() method
as:
msg = [Link](1024)

Here, 1024 indicates the buffer size. This is the memory used while
receiving the data. It means, at a time, 1024 bytes of data can be
received from the server. We can change this limit in multiples of 1024.
For example, we can use 2048 or 3072 etc.

The received strings will be in binary format. Hence they can be


converted into normal strings using decode() method.

4. Finally, we should disconnect the client by calling close() method on


the socket object as:

[Link]()
A UDP Server
If we want to create a server that uses UDP protocol to send messages,
we have to specify socket.SOCK_DGRAM while creating the socket
object, as:

s = [Link](socket.AF_INET, socket.SOCK_DGRAM)

It means the server will send data in the form of packets called
‘datagrams’. Now, using sendto() function, the server can send data to
the client. Since UDP is a connection-less protocol, server does not know
where the data should be sent. Hence we have to specify the client
address in sendto() function, as:

[Link](“message string”, (host,port))

Here, the "message string" is the binary string to be sent. The tuple
(host, port) represents the host name and port number of the client.
A UDP Client
At the client side, the socket should be created to use UDP protocol as:

s = [Link](socket.AF_INET, socket.SOCK_DGRAM)

The socket should be bound to the server using bind() method as:

[Link]((host, port))

Now, the client can receive messages with the help of recvfrom() method as:

msg, addr = [Link](1024)

This method receives 1024 bytes at a time from the server which is called
buffer size. This method returns the received message into ‘msg’ and the
address of the server into ‘addr’.
A UDP Client
Since the client does not know how many messages (i.e. strings) the
server sends, we can use recvfrom() method inside a while loop and
receive all the messages as:
while msg:
print('Received: '+ [Link]())
msg, addr = [Link](1024)

But the problem here is that the client will hang when the server
disconnects. To rectify this problem, we have to set some time for the
socket so that the client will automatically disconnect after that time
elapses. This is done using settimeout() method.

[Link](5)

This method instructs the socket to block when 5 seconds time is elapsed.
Hence if the server disconnects, the client will wait for another 5 seconds
time and disconnects. The settimeout() method can raise [Link]
exception.
A UDP Client
It is better to run the UDP Client program first so that it will wait
for the server. Then run the UDP Server program.

This program will send two strings to the client after 5 seconds
from the time of its execution. The client receives them. Once the
server is disconnected, the client will wait for another 5 seconds
and then terminates
File Server
A file server is a server program that accepts a file name from a
client, searches for the file on the hard disk and sends the content
of the file to the client.

When the requested file is not found at server side, the server
sends a message to the client saying ‘File does not exist’.

We can create a file server using TCP/IP type of socket connection.


File Client
A file client is a client side program that sends a request to the
server to search for a file and send the file contents.

We have to type the file name from the key board at the file
client. This file name is sent to the file server and the file
contents are received by the client in turn.

We can create a file client program using TCP/IP type of socket


connection.

Save this program as [Link]. Now run the [Link] and


[Link] programs in separate DOS windows. Enter file name
in the [Link] program and you can see the file contents
displayed at the client.
Introduction to Thread
A thread represents a separate path of execution of a group of
statements. In a Python program, if we write a group of statements,
these statements are executed by the Python Virtual Machine (PVM)
individually. This execution is called a thread because PVM uses a
thread to execute these statements. This means that in every Python
program, there is always a thread running internally that the PVM
appoints to execute the program statements.

The ‘threading’ module is needed while dealing with threads. This


module contains a method ‘current_thread()’ that returns an object
containing the information about the presently running thread in the
program.
Thread- sample program (main thread)
# every Python program is run by main thread
import threading
print('Let us find the current thread')
# find the name of the present thread
print('Currently running thread:',threading.current_thread().getName())
# check if it is main thread or not
if threading.current_thread() == threading.main_thread():
print('The current thread is the main thread')
else:
print('The current thread is not main thread')
Thread (Single tasking)
A thread represents the execution of statements. The way the
statements are executed is of two types:
Single tasking: A task means doing some calculation, processing,
etc. Generally, a task involves the execution of a group of
statements, for example, executing a program. In a ‘single tasking’
environment, only one task is given to the processor at a time.
Thread (Multi tasking)
Multi-tasking: To use the processor’s time optimally, we can give it several jobs
at a time. This is called multitasking. But how can we give several jobs at a time?
Suppose there are 4 tasks that we want to execute. We load them into the
memory, as shown in the Figure. The memory is divided into 4 parts, and the
jobs are loaded there. Now, the microprocessor has to execute them all at a
time. So the processor will take a small time, like a millisecond and divide this
time between the number of jobs. Here, there are 4 jobs. So we get ¼
millisecond time for each job. This small part of the processor time is called
‘time slice’. It will allot exactly ¼ millisecond time for executing each of the jobs.
Within this time slice, it will try to execute each job.

You might also like