Uploaded by Tushar Pathak

UNIT5 MATERIAL PUP

advertisement
UNIT-V
Python Networking Programming
Python is a full- featured object-oriented programming language with a standard library that
includes everything needed to rapidly build powerful network applications. Python is very
simple and easy to learn, because of that the programming side to be as easy as possible so that
you can learn as much as possible about the networking side. A network is a discrete collection
of connected network devices. Python allows us to see the low level socket command s, just
as if we were using C language, but also has modules that implement application layer
protocols. Using some of the modules, we will be able accomplish much more, with less effort
than we would in other programming languages. Python also has libraries that provide higherlevel access to specific application-level network protocols, such as TCP, FTP, HTTP, and so
on. The following lessons will focus on writing programs for networks that use the Internet
protocol suite.
Protocols
A network protocol defines rules and conventions for communication between network
devices. Modern protocols for computer networking all generally use packet switching
techniques to send and receive messages in the form of packets - messages subdivided into
pieces that are collected and re-assembled at their destination. Network protocols must be
confirmed and installed by the sender and receiver to ensure network/data communication and
apply to software and hardware nodes that communicate on a network. In the standard model
known as OSI (Open Systems Interconnection) , there are one or more protocols at each layer
in the sender and receiver that both ends recognize and observe. The Internet Protocol family
contains a set of related and among the most widely used network protocols. Beside Internet
Protocol (IP) itself, higher-level protocols like TCP, UDP, HTTP, and FTP all integrate with
IP to provide additional capabilities. Similarly, lower-level Internet Protocols like ARP and
ICMP also co-exist with IP.
Socket
A socket is just a logical endpoint for communication. Its a virtual thing, and it does not mean
any hardware. They exist on the transport layer. It is uniquely identified by an IP address , a
port number, and a communications protocol. You can send and receive things on a socket, you
can bind and listen to a socket. The client and the server must agree on a protocol, that is, they
must agree on the language of the information transferred back and forth through the socket. It
provides an interface for applications to utilize a system resource in a way that allows
the operating system to mediate and organize the use of a limited resource by any number of
applications.
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
1
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.
2
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.
IP Addressess and Ports
3
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.0 – 192.168.255.255),
(172.16.0.0 – 172.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).
How to send email using Python
4
Sending mail is done with Python's smtplib using an Simple Mail Transfer Protocol (SMTP)
server. Python's smtplib module defines an SMTP client session object that can be used to send
mail to any Internet machine with an SMTP or ESMTP listener daemon.
$ import smtplib
The SMTP object is used for the email transfer. The following syntax is used to create the
smtplib object.
1. import smtplib
2. smtpObj = smtplib.SMTP(host, port, local_hostname)
It accepts the following parameters.
o
host: It is the hostname of the machine which is running your SMTP server. Here, we
can specify the IP address of the server like (https://www.javatpoint.com) or
localhost. It is an optional parameter.
o
port: It is the port number on which the host machine is listening to the SMTP
connections. It is 25 by default.
o
local_hostname: If the SMTP server is running on your local machine, we can
mention the hostname of the local machine.
The sendmail() method of the SMTP object is used to send the mail to the desired machine.
The syntax is given below.
1. smtpObj.sendmail(sender, receiver, message)
1. #!/usr/bin/python3
2. import smtplib
3. sender_mail = 'sender@fromdomain.com'
4. receivers_mail = ['reciever@todomain.com']
5. message = """From: From Person %s
6. To: To Person %s
7. Subject: Sending SMTP e-mail
8. This is a test e-mail message.
9. """%(sender_mail,receivers_mail)
10. try:
11. smtpObj = smtplib.SMTP('localhost')
12. smtpObj.sendmail(sender_mail, receivers_mail, message)
13. print("Successfully sent email")
14. except Exception:
15. print("Error: unable to send email")
Retrieving Email from a POP3 Server
mServer = poplib.POP3('mail.sfcn.org')
5
mServer.user(getpass.getuser())
mServer.pass_(getpass.getpass())
numMessages = len(mServer.list()[1])
for msg in mServer.retr(mList+1)[1]:
The poplib module included with Python provides simple access to POP3 mail servers that
allow you to connect and quickly retrieve messages using your Python scripts.
Connect to the POP3 mail server using
the poplib.POP3(host [,port [,keyfile [,certfile]]]) method, where host is the address of the
POP3 mail server. The optional port argument defaults to 995. The other optional
arguments, keyfile and certfile, refer to the PEM-formatted private key and certificate
authentication files, respectively.
To log in to the POP3 server, the code in pop3_mail.py calls
the user(username) and pass_(password) methods of the POP3 server object to complete the
authentication.
After it's authenticated to the POP3 server, the poplib module provides several methods to
manage the mail messages. The example uses the list() method to retrieve a list of messages
in the tuple format (response, msglist, size), where response is the server's response
code, msglist is a list of messages in string format, and size is the size of the response in
bytes.
To retrieve only a single message, use retr(msgid). The retr method returns the message
numbered msgid in the form of a tuple (response, lines, size), where response is the server
response, lines is a list of strings that compose the mail message, and size is the total size in
bytes of the message.
import poplib
import getpass
mServer = poplib.POP3('mail.sfcn.org')
6
#Login to mail server
mServer.user(getpass.getuser())
mServer.pass_(getpass.getpass())
#Get the number of mail messages
numMessages = len(mServer.list()[1])
print "You have %d messages." % (numMessages)
print "Message List:"
#List the subject line of each message
for mList in range(numMessages) :
for msg in mServer.retr(mList+1)[1]:
if msg.startswith('Subject'):
print '\t' + msg
break
7
mServer.quit()
pop3_mail.py
password:
You have 10 messages.
Message List:
Subject: Static IP Info
Subject: IP Address Change
Subject: Verizon Wireless Online Statement
Subject: New Static IP Address
Subject: Your server account has been created
Subject: Looking For New Home Projects?
Subject: PDF Online - cl_scr_sheet.xls
Subject: Professional 11 Upgrade Offer
Subject: #1 Ball Played at the U.S. Open
Subject: Chapter 3 submission
Email and MIME objects
8
Multipurpose Internet Mail Extensions (MIME) is an Internet standard that expand upon the
limited capabilities of email, and in particular to allow documents (such as images, sound, and
text) to be inserted in a message.
Gmail SMTP Settings
If you use Gmail with your work or school account, check with your administrator for the
correct SMTP configuration.
Simple Chat Room using Python
The sockets are the endpoints of any communication channel. These are used to connect the
server and client. Sockets are Bi-Directional. In this area, we will setup sockets for each end
and setup the chatroom system among different clients through the server. The server side has
some ports to connect with client sockets. When a client tries to connect with the same port,
then the connection will be established for the chat room.
There are basically two parts. The server side and the client side. When the server side script
is running, it waits for any active connection request. When one connection is established, it
can communicate with it.
In this case we are using localhost. If machines are connected via LAN, then we can use IP
addresses to communicate. The server will display its IP, and ask for a name for the server.
From the client side, we have to mention a name, and also the IP address of the server to
connect.
The Server Side Code
import time, socket, sys
print('Setup Server...')
time.sleep(1)
#Get the hostname, IP Address from socket and set Port
soc = socket.socket()
host_name = socket.gethostname()
ip = socket.gethostbyname(host_name)
port = 1234
9
soc.bind((host_name, port))
print(host_name, '({})'.format(ip))
name = input('Enter name: ')
soc.listen(1) #Try to locate using socket
print('Waiting for incoming connections...')
connection, addr = soc.accept()
print("Received connection from ", addr[0], "(", addr[1], ")\n")
print('Connection Established. Connected From: {}, ({})'.format(addr[0], addr[0]))
#get a connection from client side
client_name = connection.recv(1024)
client_name = client_name.decode()
print(client_name + ' has connected.')
print('Press [bye] to leave the chat room')
connection.send(name.encode())
whileTrue:
message = input('Me > ')
if message == '[bye]':
message = 'Good Night...'
connection.send(message.encode())
print("\n")
break
connection.send(message.encode())
message = connection.recv(1024)
message = message.decode()
print(client_name, '>', message)
The Client Side Code
import time, socket, sys
print('Client Server...')
10
time.sleep(1)
#Get the hostname, IP Address from socket and set Port
soc = socket.socket()
shost = socket.gethostname()
ip = socket.gethostbyname(shost)
#get information to connect with the server
print(shost, '({})'.format(ip))
server_host = input('Enter server\'s IP address:')
name = input('Enter Client\'s name: ')
port = 1234
print('Trying to connect to the server: {}, ({})'.format(server_host, port))
time.sleep(1)
soc.connect((server_host, port))
print("Connected...\n")
soc.send(name.encode())
server_name = soc.recv(1024)
server_name = server_name.decode()
print('{} has joined...'.format(server_name))
print('Enter [bye] to exit.')
whileTrue:
message = soc.recv(1024)
message = message.decode()
print(server_name, ">", message)
message = input(str("Me > "))
if message == "[bye]":
message = "Leaving the Chat room"
soc.send(message.encode())
print("\n")
11
break
soc.send(message.encode())
DJANGO FRAMEWORK
Django is a web application framework written in Python programming language. It is based
on MVT (Model View Template) design pattern. The Django is very demanding due to its
rapid development feature. It takes less time to build application after collecting client
requirement.
By using Django, we can build web applications in very less time. Django is designed in such
a manner that it handles much of configure things automatically, so we can focus on
application development only.
Django Architecture
As mentioned, Django follows the MVT framework for architecture.
•
M stands for Model
•
V stands for View
•
T stands for Template
MVT is generally very similar to that of MVC which is a Model, View, and Controller. The
difference between MVC and MVT here is the Django itself does the work done by the
controller part in the MVC architecture. Django does this work of controller by using
templates. Precisely, the template file is a mixture of HTML part and Django Template
Language also known as DTL.
Below is an architecture diagram for MVT.
12
The Template handles the UI and architecture part of an application. The view does the
logical part of the application and interacts with the Model to get the data and in turn
modifies the template accordingly. Here as already mentioned, Django works as a controller
and gets a URL that is linked to the view part of the application and thus transports the user
responses to the application. This complete interaction is dealt with this Django MVT
architecture. When we create a project, there would some default files that would be created.
I used the above command to create a new project in my my_projects folder. Now let me
show you the files that got created in the empty folder once the above command has been
executed. These are the files that got created under the project first that we created.
13
There is, in turn, one more folder and a manage.py file that has been created. Now going into
the first folder, we can observe the below files.
So the above files are those that got created automatically once a new project has been
created.
•
urls.py: As we know that our web page has to deal with many links, all the mappings
from one page to others can be done here.
•
wsgi.py: This is used to deploy our project.
•
manage.py: Gives us a URL where the project can be displayed.
After running this command, the output is as follows.
Output:
14
Check the output by using the URL in any web browser. We have learned about this, as there
should be some start in learning how to create a project on your own and work on it to clearly
understand how the MVT architecture runs using Django with Python.
Django Architecture Model
•
In Django, the model does the linking to the database and each model gets mapped to
a single table in the database. These fields and methods are declared under the file
models.py
•
With this linking to the database, we can actually each and every record or row from
that particular table and can perform the DML operations on the table.
•
Django.db.models.The model is the subclass that is used here. We can use the import
statement by defining as from django.db import models.
•
So after defining our database tables, columns and records; we are going to get the
data linked to our application by defining the mapping in settings.py file under the
INSTALLED_APPS.
Django View
•
This is the part where actually we would be mentioning our logic. This coding is done
through the python file views.py
15
•
This view also sends responses to the user when the application is used, to understand
briefly, we can say that this view.py can deal with HttpResponse.
•
Now, after creating a view, how can we link it to our application? How do you think
that the system is going to understand to display a particular view? This can be done
by mapping the views.py in urls.py file. As already mentioned, urls.py keeps track of
all those different pages that we created and hence map each of them.
Django Template
•
This template helps us to create a dynamic website in an easy manner. The dynamic
website deals with dynamic data. Dynamic data deals with a scenario where each user
is displayed with their personalized data; as Facebook feeds, Instagram feeds, etc.
•
The configuration of the template is done in settings.py file under
INSTALLED_APPS. So python code would search for the files under the template
subdirectory. We can create an HTML file or import any dynamic web page from the
browser and place it under the template folder.
•
And after that our usual linking of this file in urls.py and views.py to get a response is
mandatory.
•
In this way after linking all these together and running the server, we can get our web
application ready.
Django Template Language
In short, it is called as DTL. Django template has its own syntax in rendering the data on to
the web page. For displaying a dynamic variable, the variable name is written inside the curly
braces; denoted by “{{variable_name}}”. And to write if conditions, the syntax would be
defined as: {% if condition %}. The same would be followed for the end if syntax also.
Django uses render function in DTL. This render function takes three parameters.
16
•
Request
•
Mentioning the path of template in settings.py
•
Parameters that contain all variables and can create as many as possible.
By these render functions we can have our DTL to make dynamic web pages.
Features of Django
o
Rapid Development
o
Secure
o
Scalable
o
Fully loaded
o
Versatile
o
Open Source
o
o
Vast and Supported Community
Django is one of the most popular web frameworks in Python. It is secure, robust and
allows developers to rapidly develop projects and meet deadlines. It is free and opensource, it works both on Windows and *nix systems.
o
In this tutorial, we are going to learn how to install Django on Windows.
o
Install Python
o
Before you install Django, you must make sure that you have Python installed. You
can check out this guide I wrote: how to install python on windows to learn how to
install Python on Windows.
o
Install Django on Windows
o
The commands that we will run in this tutorial will work on both Windows Command
Prompt(CMD) and Powershell. I will use Powershell but you can CMD if that’s what
you prefer.
o
o
Step 1 - Open Powershell
You can search for Powershell in the Windows search box or you can open
the Run dialog box by holding Windows logo key and R(Win+R) and
type powershell and then click ok.
o
You should now have a Powershell window opened.
17
o
o
Now that we have opened Powershell, Let’s verify that Python has been installed.
o
o
Step 2 - Verify Python Installation
Type python -V on the prompt to verify that Python has been successfully installed
o
o
> python -V
you should see the Python version installed, being printed like below:
o
o
o
o
PS C:\Users\Stanley> python -V
Python 3.7.4
Step 3 - Upgrade Pip
Python now comes with pip by default. But most of the time, it comes with an old
version. it is always a good practice to upgrade pip to the latest version
o
o
> python -m pip install --upgrade pip
You should see something like the screenshot below showing that the upgrade was a
success:
18
o
o
o
Step 4 - Create a Project Directory
let’s create a project directory. We will name it django_project since this tutorial is
just a demo but in the real world, the project directory’s name would be forum, blog,
etc.
o
To create the directory:
o
o
> mkdir django_project
Change into the django_project directory:
o
o
> cd django_project
Your prompt should now show you that you’re in the “django_project” directory
like below:
o
o
o
PS C:\Users\Username\django_project>
Step 5 - Create Virtual Environment
A virtual environment(virtualenv) is an isolated Python environment where you can
install packages and dependencies without affecting other Python projects. You can
learn more about virtualenv here.
o
To create a virtual environment, type python -m venv venv and wait for a few
seconds:
o
o
> python -m venv venv
It will create a directory called venv in the project directory.
19
o
Next, we will activate the virtual environment.
o
o
Step 6 - Activate Virtual Environment
Run the following command to activate the virtualenv.
o
o
> venv\Scripts\activate
When virtualenv is activated, you will see a (venv) at the beginning of the prompt.
o
o
(venv) PS C:\Users\Stanley\django_project>
If you run into an error like the one below on Powershell when activating virtualenv,
for the sake of brevity, I described the reason and the solution
here: https://www.stanleyulili.com/powershell/solution-to-running-scripts-is-disabledon-this-system-error-on-powershell/
o
o
Now that virtual environment is activated, let’s install Django.
o
o
Step 7 - Install Django
We are going to use pip to install Django. So run the following command to start the
installation:
o
o
(venv)> pip install django
The command will install the latest version of Django. You should see Django being
downloaded just like the screenshot below:
20
o
o
If you want to install a different Django version, you can specify the version:
o
(venv)> pip install django==2.1
Start a New Project
o We will use django-admin startproject to create our Django project structure called
“testsite”.
o
Run the following command:
o
o
(venv)> django-admin startproject testsite
Navigate into the testsite directory by running the following command:
o
o
o
(venv)> cd testsite
Run the Server
Let’s now run the development server using the manage.py runserver:
o
(venv)> python manage.py runserver
21
o
o
Visit http://127.0.0.1:8000/ in your web browser.
o
Tip: if want to leave the virtualenv, you must type deactivate on the prompt.
o
You are now ready to start developing your project
22
Download