Python Unit 5
Python Unit 5
com 1
GUJARAT TECHNOLOGICAL UNIVERSITY
MASTER OF COMPUTER APPLICATIONS (MCA)
SEMESTER: III
BipinRupadiya.com 2
Unit-5
Advanced Topics II: Regular Expressions
Text Book:
John V Guttag. “Introduction to Computation and Programming Using Python”, Prentice Hall of India
BipinRupadiya.com 3
Python
Chapter-18
REs and Python
Regular Expressions, Sequence Characters in Regular Expressions, Quantifiers in
Regular Expressions, Special Characters in Regular Expressions, Using Regular
Expressions on Files, Retrieving Information from a HTML File
BipinRupadiya.com
Introduction
A regular expression is a string that contains special symbols and
characters to find and extract the information needed by us from
the given data.
A regular expression help us to search information, match, find
and split information as per our requirement.
A regular expression is also called regex.
BipinRupadiya.com
re module
Python provides re module that stands for regular expression.
This module contains method like
compile(),search(),match(),findall(),split() etc., are used in finding
the information in the available data.
So when we write a regular expression, we should import re
module as:
import re
BipinRupadiya.com
Normal String
We write a regular expression as raw strings.
For example normal string:
str=‘This is normal\nstring’
Now print(str) will display the preceding string in two lines as:
‘This is normal’
‘String’
Here the ‘\n’ character is interpreted as new line in the normal
string by the python interpreter and hence the string is broken
there and shown in the new line.
BipinRupadiya.com
Raw String
But in regular expression when ‘\n’ is used , it does not
mean to throw the string in new line.
Str= r’This is raw \nstring’
Print(str)
Output: This is raw\nstring
BipinRupadiya.com
Introduction
For example , we can write regular expression this way,
reg =r’m\w\w’ #as raw string
reg=‘m\\w\w’ #as normal string
Second string is quite confusing so will not use it more for regular expression
Lets understand the first expression in detail:
r= Raw string
m= word staring with m should be matched. (You can change m from any later as per
your choice.)
‘\w’ represent any character in A to Z, a to z and 0 to 9.
Since we used two ‘\w’ that means two characters are there after ‘m’
So regular expression represents total three characters and with ‘m’ as first character.
BipinRupadiya.com
Example
Lets understand the regular expression by the program,
OUTPUT
cat
BipinRupadiya.com
General form /Syntax
So the general form of writing regular expression is as follows:
result = re.search(‘expression’, ‘string’)
Regular Expression are used to perform the following important
operations:
Matching String
Searching for string
Finding all string
Splitting a string into a pieces
Replacing a string
BipinRupadiya.com
RegEx Functions
The re module offers a set of functions that allows us to search a string for a match:
Function Description
findall() Returns a list containing all matches
search() Returns a Match object
if there is a match anywhere in the string
split() Returns a list where the string has been split at each match
sub() Replaces one or many matches with a string
BipinRupadiya.com 12
Search() method
The search() method will search from the beginning till the
end and
returns a Match object if there is a match otherwise it will
return None.
The Match objectto retrieve information about the search result:
.span() returns a tuple containing the start-, and end
positions of the match.
.string returns the string passed into the function
.group() returns the part of the string where there was a
match
BipinRupadiya.com
Example:
A python program to create a regular expression to search for string
starting with m and having total 3 characters sing the search() method.
OUTPUT:
man
BipinRupadiya.com
findall() method
The findall() method will search from the beginning
till the end and
returns all the occurrence of the matching string in
the form of list object, otherwise it will return None.
if the matching strings are not found, then it returns
empty list.
We can retrieve the resultant string from the list using a
for loop
BipinRupadiya.com
Example
python program to create a regular expression to search for string starting with m
and having total 3 characters using the findall() method.
Output:
['man', 'mop', 'man', 'moh']
BipinRupadiya.com
match() method
match() method searches in the beginning of the
of the string and
if the matched string is found then it will return
an object that contains the resultant string,
otherwise it will return None.
We can access the string from the return object using
group() method.
BipinRupadiya.com
Example:
A python program to create a regular expression to search for string starting with
m and having total 3 characters using the match() method.
OUTPUT:
None
<re.Match object; span=(0, 3), match='man'>
BipinRupadiya.com
Split() method
splits the string according to regular expression
and
the resultant pieces are returned as a list .
If there is no string , then it will return empty list.
We can retrieve the resultant string from the list using
a for loop
BipinRupadiya.com
Example
A python program to create a regular expression to split a string into a pieces
where one or more non alpha numeric character are found.
OUTPUT:
['This', 'is', 'the', 'core', 'python', 's', 'book']
['This; is the', "'core' python's book"]
BipinRupadiya.com
Sub() method
This method substitute /replace new strings in the
place of existing strings.
After substitution, the main string is returned by
this method.
BipinRupadiya.com
Example
python program to create a regular expression to search and replace string using
the sub() method.
OUTPUT:
The PM of India is Narandra Modi
BipinRupadiya.com
Sequence character in RegEx
Character Description
\d Represents digit ( 0 to 9)
\D Represents non-digit
\s Represents white space Ex. \t \n \r \f \v
\S Represents non-white space character
\w Represents any alphanumeric (a to z, A to Z, 0 to 9)
\W Represents any non- alphanumeric
\b Represents space around words
\A Matches only at start of the string
\Z Matches only at end of the string
BipinRupadiya.com 23
['Bina', 'Bipin']
['Bina']
['Hitesh', 'Bipin']
['Bipin']
BipinRupadiya.com 24
Quantifiers in RegEx
in regular expressions, some character represent more than one
character to be matched in the string.
Such characters are called 'quantifiers'
Character Description
* Zero or more repetitions of the preceding RegExp
+ One or more occurrences repetitions of the preceding RegExp
? Zero or one repetitions of the preceding RegExp
{m} Exactly the specified number of occurrences
{m,n} From m to n.
m default to 0.
n to infinity
BipinRupadiya.com 25
10001
Example
Bipin Rupadiya
9228582425
1-11-2008
BipinRupadiya.com 26
Meta / Special characters
Characters with special significance can be used in RegExp.
These characters will make our searching easy
BipinRupadiya.com 27
Example
BipinRupadiya.com 28
Output:
['anandi', 'and', 'ankita']
<re.Match object; span=(0, 5), match='bipin'>
<re.Match object; span=(8, 13), match='bipin'>
<re.Match object; span=(30, 33), match='you'>
BipinRupadiya.com 29
Using
Regular Expression
on File
BipinRupadiya.com
Example:
To read email from file and display it on cmd using RegEx
['[email protected]']
BipinRupadiya.com ['[email protected]'] 31
Example:
retrieve data from one file & write it to another file
101 100000.50
202 30000.30
303 4000.40
BipinRupadiya.com 32
Create File : index.html
BipinRupadiya.com
Example: Read HTML file using RegEx
OUTPUT:
[('Roti', '10.00'), ('Bread', '20.00'), ('Cake', '30.00'), ('Cookie', '40.00')]
BipinRupadiya.com
Python
Chapter-21
Threading
Concurrent Programming and GIL, Uses of Threads, Creating Threads in Python,
Thread Class Methods, Single Tasking using a Thread, Multitasking using Multiple
Threads, Thread Synchronization Deadlock of Threads, Avoiding Deadlocks in a
Program, Communication between Threads, Thread Communication using
notify() and wait() Methods, Thread Communication using a Queue, Daemon
Threads
BipinRupadiya.com
What is process?
A process is an instance of a computer program
that is being executed.
Any process has 3 basic components:
An executable program.
The associated data needed by the program
(variables, work space, buffers, etc.)
The execution context of the program (State of
process)
BipinRupadiya.com 36
What is Thread?
A thread is an entity within a process that can be scheduled for
execution.
It is the smallest unit of processing that can be performed in an OS
(Operating System).
In simple words, a thread is a sequence of such instructions within
a program that can be executed independently of other code.
For simplicity, we can assume that a thread is simply a subset of a
process!
More than one thread can exist within the same process.
These threads share the memory and the state of the process.
BipinRupadiya.com 37
Type of statement execution
A thread represents execution of statements.
The way the statements executed is of two type:
1. Single tasking
Only one task given to the processor at a time
2. Multitasking
To use the processor’s time in an optimum way, we
can give it several jobs at a time
BipinRupadiya.com 38
Single Tasking
BipinRupadiya.com 39
Process based Multitasking
Processor
¼ millisecond
1 2 3 4 Memory
Jobs of Programs
BipinRupadiya.com 40
Thread based Multitasking
Task-1 Thread-1
Processor
Task-2 Thread-2
Program
BipinRupadiya.com 41
Multithreading
Multithreading is defined as the ability of a processor to execute
multiple threads concurrently.
Multithreading
BipinRupadiya.com 42
Concurrent Programing
It is possible to create multiple processes and set them
to work simultaneously.
In the same way it is possible to create multiple
threads and set them to execute different parts of
program simultaneously.
Executing the task of parts of a program
simultaneously is called concurrent programming.
BipinRupadiya.com 43
GIL - Global Interpreter Lock
When more than on thread is running at a time the
data of one thread is available to another thread.
In such case there is a possibility that the data may
undergo unwanted manipulations.
This happens especially when more than one thread
is acting on the data simultaneously.
PVM use an internal global interpreter lock (GIL)
that allows only single thread to run at a time
BipinRupadiya.com 44
Usage Threads
Threads are used in server side programs to serve the
needs of multiple client on a network or Internet.
Server side have to deal with lakhs of clients and their
requests so thread is used to perform various job.
Threads are used in gaming and animation too.
For example,
in game for example flight will be moving from left to
right, and gun is used to shoot flight. So, this two
different task is done by thread.
BipinRupadiya.com
Benefits of using threads
Running several threads is similar to running several
different programs concurrently, but with the following
benefits
1. Multiple threads within a process share the same data space
with the main thread and can therefore share information or
communicate with each other more easily than if they were
separate processes.
2. Threads sometimes called light-weight processes and they do
not require much memory overhead; they are cheaper than
processes.
BipinRupadiya.com 46
Creating a Thread in Python
Python provides Thread class of threading module that is
useful to create a threads.
To create our own thread, we are supposed to create an
object of Thread class.
Different ways to create thread :
1. Creating a thread without using a class.
2. Creating a thread by creating a sub class to Thread class.
3. Creating a thread without creating sub class to Thread
class.
BipinRupadiya.com
1. Creating a thread without using a class:
Create thread by using following line
t =Thread(target=functionname,[args=(arg1,arg2,…..)])
BipinRupadiya.com
Example:
thread without using a class
Thread is running...
Thread is running...
Thread is running...
Thread is running...
Thread is running...
BipinRupadiya.com
Example:
thread without using a class with args
BipinRupadiya.com
Example:
Creating a thread by sub class to Thread class
Output:
1
2
3
4
5
BipinRupadiya.com
Example: access instance variable
OUTPUT:
Thread-1
BipinRupadiya.com
3. Creating a thread
without a sub class to Thread class:
We can create an independent class say MyThread
That does not inherit python’s Thread class.
Create an object of that class
obj=MyThread(‘Hello’)
Create a thread by creating an object to “Thread” class and
specifying the of MyThread class as its target as:
t1=Thread(target=obj.display, args(1,2))
BipinRupadiya.com
Example:
Create a thread by creating an object to “Thread” class
Output:
Point-1
( x: 10 , y: 20)
BipinRupadiya.com
Methods of Thread class
run()
The run() method is the entry point for a thread.
start()
The start() method starts a thread by calling the run method.
join([time])
The join() waits for threads to terminate.
isAlive()
The isAlive() method checks whether a thread is still executing.
getName()
The getName() method returns the name of a thread.
setName()
The setName() method sets the name of a thread.
BipinRupadiya.com 56
Methods of Thread class
t.name
This is a property that represents the thread's name.
t.setDaemon(flag)
Makes a thread daemon thread if the flag is True.
t.isDaemon()
Returns True if the thread is a daemon thread, otherwise False.
t.daemon
This is a property that takes either True or False to set the thread as
daemon or not.
BipinRupadiya.com 57
Daemon threads
Daemon threads(behaves like background thread)
Some threads do background tasks, like sending
keep-alive packets, or performing periodic garbage
collection, or whatever.
These are only useful when the main program is
running, and it's okay to kill them off once the
other, non-daemon, threads have exited.
BipinRupadiya.com
Single Task Using a Thread
A thread can be employed to execute one task at a time.
Suppose there are 3 tasks, and all are executed one by one with
single thread then it is know as single tasking thread.
Lets look at example:
Let’s Plan a program for preparation of Tea
Task-1 :
Boil milk and tea powder for 5 minute
Task-2 :
Add sugar and boil for the 3 minute
Task-3 :
Filter it and serve.
BipinRupadiya.com
Example:
BipinRupadiya.com
Multitasking by multiple Thread
In multitasking, several tasks are executed at a time.
For this, we need more than one thread.
For example,
To perform two tasks, we can take 2 threads and attach
them to the 2 tasks.
And those tasks are simultaneously executed by the two
threads.
More than one thread is used so it is know as multi-
threading.
Multi-threading is used in multi tasking.
BipinRupadiya.com
Example:
perform 2 tasks using 2 threads Simultaneously
Cut Ticket : 1
Show Chair : 1
Cut Ticket : 2
Show Chair : 2
Cut Ticket : 3
Show Chair : 3
Cut Ticket : 4
Show Chair : 4
Cut Ticket : 5
Show Chair : 5
BipinRupadiya.com
Example-2
Multitasking by multiple Thread
Lets take the case of railway reservation.
Everyday several people want to reserve berth for them.
Lets take only one berth is available, and two
passengers(threads) are asking for berth.
Lets assume that counter-1’s clerk has requested to server to
allot that to his passengers.
And counter-2’s clerk has requested to server to allot that to his
passengers.
It means, two passengers are competing with each other, lets
see whom the berth is allotted.
BipinRupadiya.com
Example:
OUTPUT:
Available berth : 1
1 berths allotted to First Person
Available berth : 1
1 berths allotted to Second Person
OUTPUT IS WRONG…!!!
BipinRupadiya.com
So, what wrong happens???
When thread1 enters the reserve method,
it sees available berth number=1,
so berth is allotted to t1 and display massage like “1 berth is allotted for
First person”
After this , t1 will sleep for 1.5 seconds.
In this time ticket will be printed on printer.
But at the same time when t1 is sleeping, t2 also enters the reserve()
method.,
it also sees available berth number=1.
Reason for this is berth allocation is not updated by 1 so this happens.
So t2 will be allotted berth and
After this for printing the ticket t2 will go to sleep for 1.5 seconds.
BipinRupadiya.com
Conti..
Now, after completing 1.5 seconds t1 will wakeup, and it
will update the status of available berth by, below
equation.
self.available-=wanted
So now available berth will be 0 as it is updated by t1
But we have also allotted berth two second passenger t2
So, We will get wrong result.
This can be solved using Thread Synchronization
BipinRupadiya.com
Thread Synchronization
When one thread is acting on object , preventing other
thread from acting on the same object is called “Thread
Synchronization” or “Thread safe”
The object on which thread are synchronized is called
‘Synchronized object’ or ‘mutex’(mutually exclusive lock)
Thread synchronization is done by two techniques.
1. Using locks
2. Using semaphores
BipinRupadiya.com
1. Locks
Locks can be used to lock the object on which the thread is acting.
When a thread enters the object, it locks the object and after the execution is
completed, it will unlock the object and comes out of it.
This means the object is locked mutually on threads.
This locked objects is call mutex (mutually exclusive lock)
Example:
It is like a room with only one door.
A person has entered the room and locked it from behind.
The second person who want to enter the room should wait till the first
person comes out.
BipinRupadiya.com 68
Thread Synchronization using Locks
t2
t1
Wait till t1 comes out
Thread Synchronization
BipinRupadiya.com
Example:
Thread
Synchronization
OUTPUT:
Available berth : 1
1 berths allotted to First Person
Available berth : 0
no berth to allot
BipinRupadiya.com 70
Semaphore
A semaphore is an object that provides synchronization
based on a counter.
A semaphore is created as an object of semaphore class:
l=semaphore(counter_value)
#here the counter value by default is 1.
When the acquire()=Lock method is called
counter is decremented by 1.
And when release()=Unlock method is called,
counter is incremented by 1.
BipinRupadiya.com
Deadlock on Thread
Daily, thousand of people book tickets in trains and cancel ticket
also.
If a programmer wants to develop code for this, he may visualize
that booking ticket and cancelling them are reverse procedures.
Hence he will write these 2 tasks as separate and opposite tasks,
and assign 2 different threads to do these task simultaneously.
Booking ticket and cancelling ticket involves train and
compartments, which we consider as two objects.
BipinRupadiya.com
book_ticket
To book a ticket,
The ‘book_ticket’ thread first enters the train object to verify
the tickets are available or not.
Then it enters the compartment object and reserve the ticket
in a particular compartment.
It may change the status of ticket as ‘reserved’
cancel_ticket
To cancel a ticket,
The ‘cancel_ticket’ thread first enters the compartment object
and update the ticket status as ‘available’
Then it enters the train object and updates the available
number of tickets.
BipinRupadiya.com
Deadlock on Thread
For this purpose, we need two locks for train and compartment.
#cancel_ticket thread
compartment
lock-2:
lock on compartment
lock1:
lock on train cancel_ticket
Deadlock of Threads
BipinRupadiya.com
Deadlock on Thread
First book_ticket thread locks on train.
book_ticket
At the same time, cancel_ticket thread
locks on compartment.
When book_ticket thread wants to lock
on compartment, it will find that train
compartment object is already locked by
cancel_ticket
Similar thing happens to cancel_ticket compartment
thread.
Both waits for the each other to
release the train and compartment. cancel_ticket
This is called deadlock of threads.
Deadlock of Threads
BipinRupadiya.com
Example : Deadlock
#book_ticket thread
lock-1:
lock on train
lock-2:
lock on compartment
#cancel_ticket thread
lock-1:
lock on compartment
lock-2:
lock on train
BipinRupadiya.com
Example : Avoiding Deadlock
BipinRupadiya.com 79
Thread communication using Boolean variable
OUTPUT:
Item [1] produced...
Item [2] produced...
Item [3] produced...
Item [4] produced...
Item [5] produced...
Item [6] produced...
Item [7] produced...
Item [8] produced...
Item [9] produced...
Item [10] produced...
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
BipinRupadiya.com 80
Communication between threads
Previous program is not an efficient way of
communication.
Why?
Consumer checks the ‘dataprodover’ at
some point of time, and find it False. lst prod
1, 2, 3, 4
So it goes into sleep for the next 100
milliseconds. dataprodover
Meanwhile , the data production may be False
over.
But consumer comes out of sleep after
100 milliseconds and then it can find
Producer Consumer
‘dataprodover’ is True.
This means that there may be a time Communication between
delay of 1 to 99 milliseconds to Producer and Consumer thread
receive the data after its actual
production is completed.
BipinRupadiya.com
Communication between threads
In previous program,
It takes too much time to produce the things, as
how can we improve the efficiency of Thread?
BipinRupadiya.com
Thread communication
using notify() and wait() method.
The condition class of threading module is useful to improve the
speed of communication between the threads.
condition class object is called condition variable and is created as:
cv=condition()
condition class methods are used in thread communication.
1. notify() method is useful to immediately inform the consumer
thread that the data production is completed.
2. notify_all() method is useful to inform all waiting consumer
thread at once that production is over.
The consumer thread now not need ‘dataprodover’ boolean variable
to check production is over or not.
BipinRupadiya.com
Conti…
Consumer can simply wait using wait() method till it gets any
information of production through notify() or notify_all() methods.
Wait is written as below:
cv.wait(timeout=0)
Here timeout=0 will show that once a msg is received of “actual
production is over”, after this it will not wait for a second to
receive the data. So time delay will be there.
Note:
wait() and notify() or notify_all() methods must be between
lock acquire() and lock release() method.
BipinRupadiya.com
Example:
notify()
&
wait()
of
Condition
object
BipinRupadiya.com 85
Thread Communication using Queue
The Queue class of queue model is useful to create a queue that holds the
data produced by the producer.
The data can be taken from the queue and utilized by the Consumer.
A queue is a FIFO (First in First out) where data is stored from one side
and deleted from other side.
Queue are useful when several Procedures want to communicate with
several consumers.
Another advantage is that while using queues, we need not use locks since
queues are thread safe.
To create a Queue
q = Queue()
Suppose, we want to insert an item ‘i’ into queue I
q.put(i)
BipinRupadiya.com 86
The put() is used by Procedure to insert item into the queue
The Consumer use get() to retrieve the item from queue.
x=prod.q.get()
#prod is the instance of procedure class that contains the queue ‘q’
q.empty() return true if queue is empty otherwise false
q.full() return true if queue is full otherwise false
q.put() prod
prod.q.get()
654321
Producer Consumer
Thread Communication between
BipinRupadiya.com 87
Producer and Consumer using Queue
Example: Queue OUTPUT:
BipinRupadiya.com 88
Daemon threads
Daemon threads(behaves like background thread)
Some threads do background tasks, like sending
keep-alive packets, or performing periodic garbage
collection, or whatever.
These are only useful when the main program is
running, and it's okay to kill them off once the
other, non-daemon, threads have exited.
BipinRupadiya.com
Python
Chapter-23
Networking
Protocol, Sockets, Knowing IP Address, URL, Reading the Source Code of a
Web Page, Downloading a Web Page from Internet, Downloading an Image
from Internet, A TCP/IP Server, A TCP/IP Client, A UDP Server, A UDP Client,
File Server, File Client, Two-Way Communication between Server and Client,
Sending a Simple Mail,
BipinRupadiya.com
Introduction
Interconnection of computer is called
network.
Simple network can be formed as
connecting two or more than two computer
via cable.
Computer network will enable the users to
share files and folders and get synchronized
with other users.
Internet is the biggest example of network.
BipinRupadiya.com
Need for Computer Networks
Computer network also allows the user to share resources such as
printers and faxes.
Printer can be installed on one computer and can be accessed
by other users in a network.
The user can also setup a central system wherein common files
and folders which are frequently used by all the users can be
stored.
All the users within the network can easily access those files.
An organization you have several computers but just one phone
line, a network makes it easier to use the Internet.
Numerous computers can share one modem to use the
equivalent Internet connection.
BipinRupadiya.com
Client-server in Network
Computer which is sending requests to server is
known as client.
Generally most of the networks are based on this
client server network.
BipinRupadiya.com
Three Requirements of network:
Hardware:
Includes computer, cable, modems, routers, hubs etc.
Software:
includes programs to communicate between server and clients.
Protocol:
It defines rules and conventions for communication between network
devices.
Network protocols include mechanisms for devices to identify and
make connections with each other, as well as formatting rules that
specify how data is packaged into messages sent and received.
Two types of protocols on which other protocols are based on:
1. TCP/IP Protocol
2. UDP
BipinRupadiya.com
1. TCP/IP Protocol:
TCP is a connection-oriented protocol.
Connection-orientation means that the communicating devices should
establish a connection before transmitting data and should close the
connection after transmitting the data.
TCP/IP model layers
TCP/IP functionality is divided into Five layers, each of which include
specific protocols.
1. The application layer
2. The transport layer
3. IP layer
4. Data link Layer
5. Physical layer
BipinRupadiya.com
TCP/IP model layers
1. Application layer
It provides applications with standardized data exchange.
Its protocols include the Hypertext Transfer Protocol (HTTP), File
Transfer Protocol (FTP), Post Office Protocol 3 (POP3), Simple Mail
Transfer Protocol (SMTP) and Simple Network Management Protocol
(SNMP).
It sends data to next layer TCP in the form of bytes.
2. Transport layer
It is responsible for maintaining end-to-end communications across the
network.
The transport protocols include TCP and User Datagram Protocol (UDP),
which is sometimes used instead of TCP for special purposes.
It divides the data into the packets.
BipinRupadiya.com
TCP/IP model layers
3. IP layer
It inserts the packets into envelops called ’Frames.’
Each frame contains packet and its IP address of destination
computer.
4. Data link Layer:
This frames are sent to Datalink Layer, which dispatches them
and correct destination computer on network.
5. Physical layer:
It is actually used to physically transmit the data using
hardware.
BipinRupadiya.com
UDP
User Datagram Protocol or
Universal Datagram Protocol
UDP is a connectionless protocol.
UDP is suitable for applications that need fast, efficient
transmission, such as games.
There is no guarantee that the messages or packets sent
would reach at all.
So it is not reliable at all.
BipinRupadiya.com
Sockets
Sockets and the socket API are used to send messages across a
network.
They provide a form of inter-process communication (IPC).
The obvious example is the Internet, which you connect to via your
ISP.
Basically, sockets act as a communication link between two entities,
i.e. a server and a client.
A server will give out information being requested by a client.
For example,
when you visited any web page, the browser created a socket
and connected to the server
BipinRupadiya.com
Socket Programming
Each socket is given an identification number , which is
called ‘Port Number’.
Establishing communication between Server and client using
socket is called Socket Programming.
We should use a new port number for each new socket.
Every new service on the net should be assigned a new port
number.
The port number from 0 to 1023 are used by our computer
system for various applications.
BipinRupadiya.com
Some Reserved Port Numbers
and Associated Services
Port Number Application or Services
13 Date and Time Services
21 FTP which transfer files
23 Telnet, which provide remote login
25 SMTP, which delivers mails
67 BOOTP, which provides configuration at boot time
80 HTTP, which transfer web pages
109 POP2, which is mailing service
110 POP3, which is a mailing service
119 NNTP, which is for transferring news articles
443 HTTPS, which transfer web pages securely
BipinRupadiya.com
A server connected with clients
through Sockets
The point of the story is that a server on network uses socket to connect
a client.
When server wants to communicate with so many clients at that time
several sockets are needed.
Every socket will have different port number.
BipinRupadiya.com
Create Sockets
TO create a socket, python provides socket module.
import socket
The socket() function of socket module is useful to create a socket.
s=socket.socket(address_family,type)
address_family
It indicates which version of the IP address should be used, IPV4 or
IPV6.
It takes two values either:
Socket.AF_INET #IPV4 protocol
Socket.AF_INET6 #IPV6 protocol
type:
The type of communications between the two endpoints,
SOCK_STREAM for connection-oriented protocols(TCP) and
SOCK_DGRAM for connectionless protocols.(UDP)
BipinRupadiya.com
gethostbyname()
To know the IP address of website on the internet,
we can use gethostbyname() function available in socket module.
Output
IP Address : 172.217.166.35
BipinRupadiya.com
Find IP address of your computer
Output:
Your Computer
Name :Bipinsir-PC
IP Address :10.1.52.53
BipinRupadiya.com 105
URL: Uniform Resource Locator
It represents the address that is specified to access some
information or resource on Internet.
Example:
URL : https://www.BipinRupadiya.com:80/index.html
Protocol : https
Server name or IP address : www.BipinRupadiya.com
Port Number (optional) : 80
File that is referred : index.html
BipinRupadiya.com 106
How to parse URL?
We can parse the URL and find out all parts of the URL
To parse URL urlparse() of urllib.parse module is used
Pass URL to urlparse()
It returns a tuple containing parts of URL
scheme : protocol of URL
netloc : website name with port number if present
path : path of webpage
port : the port number
We can also get total URL from totle by calling geturl()
BipinRupadiya.com 107
Example:
BipinRupadiya.com 108
Output:
ParseResult(scheme='http', netloc='www.bipinrupadiya.com', path='/p/about-
us.html', params='', query='', fragment='')
Scheme : http
Net Location : www.bipinrupadiya.com
Path : /p/about-us.html
Parameters :
Port Number : None
Total URL : http://www.bipinrupadiya.com/p/about-us.html
BipinRupadiya.com 109
Reading a source code of a webpage
Module we need for this purpose is urllib.request
And function of this module, urlopen() is used for reading the source of a web page.
b'<!doctype html>\n<!--[if lt IE 7]> <html class="no-js ie6 lt-ie7 lt-ie8 lt-ie9"> <![endif]-->\n<!--[if IE 7]> <html class="no-js ie7 lt-ie8 lt-ie9"> <![endif]-->\n<!--[if IE
8]> <html class="no-js ie8 lt-ie9"> <![endif]-->\n<!--[if gt IE 8]><!--><html class="no-js" lang="en" dir="ltr"> <!--<![endif]-->\n\n<head>\n <meta charset="utf-
8">\n <meta http-equiv="X-UA-Compatible" content="IE=edge">\n\n <link rel="prefetch" href="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">\n\n <meta
name="application-name" content="Python.org">\n <meta name="msapplication-tooltip" content="The official home of the Python Programming Language">\n <meta
name="apple-mobile-web-app-title" content="Python.org">\n <meta name="apple-mobile-web-app-capable" content="yes">\n <meta name="apple-mobile-web-app-status-
bar-style" content="black">\n\n <meta name="viewport" content="width=device-width, initial-scale=1.0">\n <meta name="HandheldFriendly" content="True">\n <meta
name="format-detection" content="telephone=no">\n <meta http-equiv="cleartype" content="on">\n <meta http-equiv="imagetoolbar" content="false">\n\n <script
src="/static/js/libs/modernizr.js"></script>\n\n <link href="/static/stylesheets/style.67f4b30f7483.css" rel="stylesheet" type="text/css" title="default" />\n <link
href="/static/stylesheets/mq.3ae8e02ece5b.css" rel="stylesheet" type="text/css" media="not print, braille, embossed, speech, tty" />\n \n\n <!--[if (lte IE
8)&(!IEMobile)]>\n <link href="/static/stylesheets/no-mq.fcf414dc68a3.css" rel="stylesheet" type="text/css" media="screen" />\n \n \n <![endif]-->\n\n \n <link
rel="icon" type="image/x-icon" href="/static/favicon.ico">\n <link rel="apple-touch-icon-precomposed" sizes="144x144" href="/static/
….
BipinRupadiya.com
Downloading a webpage from Internet
Steps:
1. open the webpage using urlopen() function. This
function will return the content of the webpage into file
like object.
2. now we can read data from that object.
3. Here urlopen() function can raise the
‘urllib.error.HTTPError’ exception, If the file is not
found.
4. now openour file in wb mode (as web page contain
binary data) and write the ‘content’ data to our file
BipinRupadiya.com
Example:
BipinRupadiya.com 112
Downloading an Image from Internet
BipinRupadiya.com
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.
Lets discuss the general steps to be used at server side:
BipinRupadiya.com 114
Step-1:
Create a TCP/IP Socket at server side using
socket() function
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
socket.AF_INET represents IP Address version 4
socket.STREAM indicates that we are using TCP/IP protocol for
communication with client
A socket use IP address version 4 and TCP/IP protocol by default
We can create socket without giving protocol version and type of
the protocol as:
s=socket.socket() #this uses TCP/IP protocol by default
BipinRupadiya.com 115
Step-2:
Bind the socket with the host name and port
number
s.bind((host, port)) # here (host, port) is a tuple
Host name can be an IP Address or a web site name.
If we want to run this on local computer
use ‘loclhost’ or it’s IP address 127.0.0.1
Step-3:
specify maximum number of connection using listen()
s.bind(5) # maximum connection allowed is 5
BipinRupadiya.com 116
Step-4:
The server should wait till client accepts
connection. That is done using accept()
c, addr = s.accept()
c is the connection object, used to send message to the client
addr is the address of the client that has accepted the connection
Step-5:
send message string to client using send()
c.send(b”message string”) #or
c.send(”message string”.encode()) #or
The message string should be in the form of byte stream as they
are used in TCP/IP
BipinRupadiya.com 117
Step-6:
close the connection
c.close()
After sending the message to the client, the server can be
disconnected by closing the connection object
BipinRupadiya.com 118
Example: TCP/IP Server
BipinRupadiya.com
A TCP/IP Client
A client is a program that receive the data or services from the server
We use following steps to create client
Step-1: Create the socket object that uses TCP/IP to receive data
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Step-2: Connect the socket using connect method()
s.connect((host,port))#connect it to server
Step-3: Receive the message from the server using recv()
msg=s.recv(1024)
1024 is a buffer size, at a time reading from server
We can modify it, in multiple of 1024
like 2048 or 3072 so it can be converted to normal string using decode()
Step-4: Disconnect the client using close()
s.close()
BipinRupadiya.com 120
Example: TCP/IP Client
BipinRupadiya.com
Output:
First execute TCP/IP server: server.py
BipinRupadiya.com
A UDP server
If we want to create a server that uses UDP protocol to send message
s=socket.socket(socket.AF_INET, socket.sock_DGRAM)
Server will send data in the form of packet called datagram
The server can send data to the client using sendto()
UDP is a connection-less protocol,
server does not know where the data should be sent
Hence we need to specify client address in sendto()
s.sendto(b“message string”,(host,port))
The message string is the binary string to be sent
The tuple (host, port) represent host and port of the client
The server will wait for 5 second after it send 2 message to the client
BipinRupadiya.com
Example: UDP Server
BipinRupadiya.com
A UDP Client
At the client, the socket should be created to use UDP protocol
s=socket.socket(socket.AF_INET, socket.sock_DGRAM)
The socket should be bound to the server using bind()
s.bind((host,port))
The client can receive message with the help of recvfrom()
msg,addr=s.recvfrom(1024)
retrun msg and IP address of the server
1024 is a buffer size, at a time reading from server
Since the client does not know how many messages the server sends we can use
recvfrom() inside while loop to read all messages
But the problem is that client will hang when server disconnects.
To rectify this problem we have to set some time for the socket so that the client will
automatically disconnect after time elapses using settimeout().
s.settimeout(5)
This method instructs the socket to block when 5 seconds time elapsed.
Hence if server disconnects, the client will wait for another 5 seconds time and disconnects.
The settimeout() can raise socket.timeout exception
BipinRupadiya.com 125
Example: UDP Client
BipinRupadiya.com
Output:
First execute UDP server: server.py
BipinRupadiya.com
File Sever
File server is a program
that accept file name from client,
searches for the file on the hard disk and
send the content of the file to the client.
If the file is not found server sends msg to client that
‘File does not exist.’
We can create a file server using TCP/IP type of socket
connection.
BipinRupadiya.com
Example:
File Server
BipinRupadiya.com
File Client
It sends request to server to search for file and send the
content of the file to client
We have to type file name at the file client
This file name is sent to the file server and the file
content are received by the client in return
We can create a file client using TCP/IP type of socket
connection
BipinRupadiya.com
Example: File client
BipinRupadiya.com
Output:
First execute File server: server.py
BipinRupadiya.com
Two way communication between
Server and client
BipinRupadiya.com
Example: Server
BipinRupadiya.com
Example: client
BipinRupadiya.com
Output:
First execute chat server: server.py
BipinRupadiya.com
Sending a Simple Mail
SMTP server is useful to send mail.
We need SMTP class of smtplib module.
Create an object of SMTP to connect SMTP server.
server=smtplib.SMTP(“smtp.gmail.com”,587)
First parameter is SMTP server name and
Second parameter is server’s port number
Once we connect to mail server we can send mail using
send_message()
server.send_message(msg)
BipinRupadiya.com
server.send_message(msg)
Where msg represent total message string including
Address to which mail should be send
Address from where the mail send
Subject of the mail
Body of the mail
It is an object of MIMEText class of email.mime.text module.
MIME : Multi-Purpose Internet Mail Extensions
The MIME indicates an extension of the original internet e-mail protocol
People use the protocol to exchange different kind of data on the internet.
msg=MIMEText(body)#body indicate mail body text
msg[“From”]=fromaddr
msg[“To”]=toaddr
msg[“Subject”]=“subject of your mail”
BipinRupadiya.com 138
Login to server
Before sending email, we have to login in to the server with
valid password
This is done in two step
1. Put smtp connection in to TLS (Transport Layer Security)
mode
server.starttls()
#This start the process of verifying authenticity of server
2. We have to login to the server with our mail address and
password.
server.login(fromaddr , ”password”)
BipinRupadiya.com 139
Example:
BipinRupadiya.com 142
Bipin S. Rupadiya
(MCA, PGDCA, BCA)
Assistant Professor,
JVIMS-MCA (537), Jamnagar
www.BipinRupadiya.com
BipinRupadiya.com 143