Day 17. Client-server architecture and Django web framework.

Web framework of python.

Hello Everyone…

Earlier we had studied python3 and also we had done a hands-on practice of python, now after completing the python, we are starting the Django web framework. which is used to develop web applications, websites and many more. So today we are discussing some related topics of this here.

Topics we are covered today..

  • Client-server architecture.
  • HTTP.
  • Virtual environment & Packages.
  • Django.

Let’s start..

1. Client-server architecture.

Client/Server architecture is a network model designed for the end-users called clients, to access the resources such as songs, videos, etc from a central computer known as Server.

The central controller is known as a server while all other computers in the network are called clients.

A server performs all the major operations such as security and network management.

A server is responsible for managing all the resources such as files, directories, printer, etc.

All the clients communicate with each other through a sever. For example, if client 1 wants to send some data to client 2, then it first sends the request to the server for permission. The server sends the response to the client 1 to initiate its communication with the client 2.

The relation between client and server

2. HTTP.

The Hypertext Transfer Protocol (HTTP) is an application-level protocol for distributed, collaborative, hypermedia information systems. This is the foundation for data communication for the World Wide Web (i.e. internet) since 1990. HTTP is a generic and stateless protocol that can be used for other purposes as well as using extensions of its request methods, error codes, and headers.

HTTP working process.

HTTP protocol works in a client and server model like many other protocols A web browser using which a request is initiated is called a client and a web server software which response to that request is called a server.

Below mentioned are some key points & terms to note about HTTP protocol, before we go ahead and understand a complete HTTP request and response in practical.

  • HTTP is an application layer protocol
  • The default port if not mentioned in the request, is assumed as 80
  • The hostname in the request is case insensitive
  • World Wide Web Consortium and the Internet Engineering Task Force, both coordinates in the standardization of the HTTP protocol
  • HTTP allows the improvement of its request and response with the help of intermediates in between(for example a gateway, a proxy, or a tunnel)
  • The resources that can be requested by using HTTP protocol is made available with the help of a type of URI(Uniform Resource Identifier) called URL(Uniform Resource Locator).
  • TCP (Transmission Control Protocol), is used to establish a connection to the application layer port 80 used by HTTP.(its not at all necessary to use port 80 for HTTP connections, but if not explicitly mentioned in the URL, port 80 is assumed)
  • A series of request and response in HTTP is called as a session in HTTP
  • HTTP version 0.9 was the first documented version of HTTP
  • HTTP is a stateless protocol(which means every connection is independent of each other.)

DNS

he Domain Name Systems (DNS) is the phonebook of the Internet. Humans access information online through domain names, like nytimes.com or espn.com. Web browsers interact through Internet Protocol (IP) addresses. DNS translates domain names to IP addresses so browsers can load Internet resources.

Each device connected to the Internet has a unique IP address that other machines use to find the device. DNS servers eliminate the need for humans to memorize IP addresses such as 192.168.1.1 (in IPv4), or more complex newer alphanumeric IP addresses such as 2400:cb00:2048:1::c629:d7a2 (in IPv6).

How does DNS work?

The process of DNS resolution involves converting a hostname (such as www.example.com) into a computer-friendly IP address (such as 192.168.1.1). An IP address is given to each device on the Internet, and that address is necessary to find the appropriate Internet device — like a street address is used to find a particular home. When a user wants to load a webpage, a translation must occur between what a user types into their web browser (example.com) and the machine-friendly address necessary to locate the example.com webpage.

To understand the process behind the DNS resolution, it’s important to learn about the different hardware components a DNS query must pass between. For the web browser, the DNS lookup occurs “ behind the scenes” and requires no interaction from the user’s computer apart from the initial request.

3. Virtual Environments & Packages.

Python applications will often use packages and modules theta don’t come as part of the standard library. The application will sometimes need a specific version of a library, because the application may require that a particular bug has been fixed or the application may be written using an obsolete version of the library’s interface.

this means it may not be possible for one python installation to meet the requirements of every application. If application A needs version 1.0 od a particular module but application B needs version 2.0, then the requirements conflict and installing either version 1.0 or 2.0 will leave one application unable to run.

The solution for this problem is to create a virtual environment, a self-contained directory tree that contains a Python installation for a particular version of python, plus several additional packages.

Creating a virtual environment, The module used to create and manage a virtual environment is called venv. venv will usually install the most recent version of Python that you have available.

If you have multiple versions of Python on your system, you can select a specific Python version by running pythn3 or whichever version you want.

To create a virtual environment, decide upon a directory where you want to place it, and run the venv module as a script with the directory path.

python3 -m venv <name_of_virtualenv>

This will create the <name_of_virtualenv> of directory

Once you ‘ve created a virtual environment, you may activate it.

$ source <name_of_virtualenv> /bin/activate

Managing Packages with pip, you can install, upgrade, and remove packages using a program called pip. By default pip install packages from the Python Packages index.

You can also install a specific version of a package by giving the package name followed by == and the version number.

(<name_of_variable>)$ pip3 install requests==2.6.0(<name_of_variable>)$ pip3 freeze

pip3 freeze will give you the list of installed packages.

To deactivate the virtual environment

(<name_of_virtualenv>)$ deactivate

4. Django.

Django is a free and open-source web application framework written in Python. A framework is nothing more than a collection of modules that make development easier. They are grouped together and allow you to create applications or websites from an existing source, instead of from scratch.

This is how websites — even simple ones designed by a single person — can still include advanced functionality like authentication support, management and admin panels, contact forms, comment boxes, file upload support, and more. In other words, if you were creating a website from scratch you would need to develop these components yourself. By using a framework instead, these components are already built, you just need to configure them properly to match your site.

The official project site describes Django as “a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.

Getting started with Django.

installing Django in a setup virtual environment in a project.

(<name_of_virtualenv>)$ pip3 install django

Creating a Django project.

From the command line, cd into a directory where you’d like to store your code, then run the following command:

$ django-admin startproject <project_name>

Let’s look at what start-project created:

<project_name>/
manage.py
<project_name>/
__init__.py
setting.py
urls.py
Wsgi.py

These files are:

The outer <project_name>/ root directory is just a container for your project. its name doesn’t matter to Django; you can rename it to anything you like.

manage.py: A command-line utility that lets you interact with this Django project in various ways. You can read all the details about manage.py in Django-admin and manage.py.

The inner <project_name>/ directory is the actual Python package for your project, its name is the Python package name you’ll need to import anything inside it.

<project_name>/init.py: An empty file that tells Python that this directory should be considered a Python package. If you’re a Python beginner.

<project_name>/settings.py: Settings/configuration for this Django project. Django settings will tell you all about how settings work.

<project_name>/urls.py: The URL declarations for this Django project; a ‘“table of contents” of your Django-powered site.

<project_name>/wsgi.py: An entry-point for WSGI-compatible web servers to serve your project.

The development server:

Let’s verify your Django project works. Change into the outer <project_name> directory. If you haven’t already, and run the following commands:

$ python manage.py runserver

You ‘ll see the following output on the command line:

Performing system checks…System check identified no issue (0 silenced).You have unapplied migirations; your app may not work properly until they are applied.
Run 'python manage.py migrate' to apply them.
October 08, 2019 - 20:45:56
Django version 2.2, using settings 'mysite.settingd'
Starting development server at
http://127.0.0.1:8000/
Quit server with CONTROL-C.

You’ve started the Django development server, a lightweight webserver written purely in Python. We’ve included with Django so you can develop things rapidly, without having to deal with configuring a production server — such as Apache — until you’re ready for production.

Now that the server’s running, visit http://127.0.0.1:8000// with your web browser. you’ll see a “Congratulations!” page with a rocket taking off. it worked!

Thank You…

Data Scientist, Blockchain enthusiast, Entrepreneur.