Python For Web Development
Python For Web Development
ISSN 2320–088X
IMPACT FACTOR: 7.056
DOI: https://doi.org/10.47760/ijcsmc.2022.v11i04.006
Abstract— With evolution of web, several competitive languages such as Java, PHP, Python, Ruby are
catching the attention of the developers. Recently Python has emerged as a popular and the preferred web
programming language, because of its simplicity to code and ease of learning. Being a flexible language, it
offers fast development of web-based applications. It offers development using CGI and WSGI. Web
development in Python is aided by the powerful frameworks such as Django, web2py, Pyramid, and Flask that
Python supports. Thus, Python promises to emerge as one of the preferred choice language for web
applications.
Web is a rapidly growing repository of resources. Internet is used as a medium for accessing these resources.
Web architecture mainly comprises of two entities, namely client and server. Web client is an application
(browser) on host machine that urges these resources, and web server is a machine on web that is responsible
for fulfilling the request issued by client. Hypertext Transfer Protocol (HTTP) is the most popular protocol
used by client and server for web communication. In a static web, browser issues HTTP request to the HTTP
server, which searches for the requisite resource in its database and returns it as an HTTP response. To
avoid any compatibility issues, every request issued by browser is in form of a URL (Uniform Resource Locator).
The URL protocol defines the rules for communication between client and server. It comprise of host name (IP
address) which helps in identifying the server system on the web, port number which determines the service
(for example, FTP, email service) on the server that should respond to request, and the access path of the
resource (web page) on server. The web where responses are already stored in server database in form of
static web pages is termed static web. However, response returned by server to the client may be generated on
the fly depending upon the request of the client.
Web applications offer several benefits over traditional applications that are required to be installed at each
host computer that wishes to use them. Web applications do not incur publishing and distribution costs as
opposed to traditional applications where the software (applications) were published using CD’s and
distributed. They need not be installed at each client host; rather they are placed at a central server and
accessed by large number of clients. Since a web application is managed centrally, application updates and
data backups can be performed easily. The web applications are easily accessible irrespective of the
boundaries of space and time. Since, they are accessed through browser, the platform accessing them is not
an issue, and thus they provide cross-platform compatibility. Inspite of above- mentioned advantages, web
applications have a few limitations. Internet connectivity and server availability is required for accessing web
application through browser. However, accessing them through Internet my take more time as compared to
applications installed on host systems. Also, web applications require compatible web browsers. Since they
are deployed on web, they are vulnerable to several Internet attacks.
Web programming using CGI and WSGI requires building web applications from the scratch by using
Python standard libraries. Python provides with web frameworks in the form of packages/ modules that
simplify the task of writing application programs. These frameworks lighten tedious job of developers. They
support server and client side programming by providing support for several activities such as request
interpretation (getting form parameters, handling cookies and sessions), response generation (generating data
in HTML or other format such as pdf, excel), and storing data. The web frameworks are further categorized as
full- stack and non-full-stack frameworks. Full-stack frameworks provide components for every phase of
programming in contrast to non-full-stack frameworks.
All the frameworks include templates and data persistence as key ingredients for constructing web. Templates
are used to avoid complex code that results when HTML and Python code is mixed in a single file. Templates
are HTML files with placeholder for the data depending upon user input. Data persistence deals with storing
and retrieving data and maintaining consistency. The data can be stored and maintained using plain text files,
relational database engines such as MYSQL, Oracle, or some object-oriented databases. The web framework
providing support for WSGI should be preferred. This makes deploying an application easier.
Keywords— Python, Web server application development, Django frameworks, Model View Controller, MVC,
MVT, WSGI, CGI, PHP, Flask, Pyramid, Web2py, PyQt, Artificial Intelligence, Machine Learning, Data
Science and Visualization, Web Scraping
I. INTRODUCTION
1. Web Development:
The critical point was a few years ago. We all thought that high-speed Internet and advanced browsers
would allow us to create unwieldy web pages. However, mobile devices changed this world. Lightweight,
portable, and adaptive are the three factors that shape a website Development these days. Most users
prefer lightweight devices, even if they have a powerful desktop. Also, people can use different devices
to visit a website.
Ionic, the JavaScript versatile system instead of Angular, is a significant marker for what modern web
development really resembles. Angular is not necessarily dead. There are a lot of reasons for people to
continue use Angular over React.js and other JavaScript apparatuses if the utilization case is right.
On the other hand, a website needs to deal with an offset of complex necessities with execution and
effortlessness. That is why web developers do not always use technologies that give a simple and
templated approach to manufacture portable and responsive websites.
Web Components are a set of features that provide a standard component model for the Web allowing for
encapsulation and interoperability of individual HTML elements with technologies: Custom elements,
Shadow DOM, HTML templates.
Custom Elements. Quite simply, these are fully valid HTML elements with custom templates, behaviours
and tag names (e.g. <one-dialog>) made with a set of JavaScript APIs.
Shadow DOM. Capable of isolating CSS and JavaScript. HTML templates. User- defined templates in
HTML that aren’t rendered until called upon.
Web components simplify many complexities in front-end development. Web Components are a collection
of building blocks for web pages or web applications in simple terms. With web components, developers
have a wider range of opportunity.
Surely, JavaScript frameworks such as React, Vue or Angular follow a similar approach, however these
frameworks do so at a cost. Language features need to be compiled, and many JS frameworks rely on a
runtime to manage all their abstractions. Web components allow developers to get the benefits without all
that heavy weight. web components are generally available in all the major browsers, except for Microsoft
Edge and Internet Explorer 11.
resource API. Nowadays, many companies are using the microservices approach; or rather they are using the
micro frontends approach to build their web apps.
The reason microservices are all the rage right now is that they make it so much easier to develop, integrate,
and maintain applications. Microservices are especially useful for larger companies since they allow teams
to work on separate items without the need for any horribly complicated orchestration between them. This
ultimately comes down to the fact that individual functionalities are treated separately, initially allowing you
to build applications step-by-step, and later allowing you to work on each element separately (so you can
add, improve, or fix without risking breaking the entire application).
API stands for Application Programming Interface, where the keyword is interface. APIs are the doorway,
so to speak, that allows developers to interact with an application.
In short, APIs allow internal and external developers to accomplish one of two things: access an
application’s data or use an application’s functionality. Ultimately, this is how the world’s electronics,
applications, and web pages are linked up to communicate with one another and work together.
Things like using a social account to authenticate on a website, having the weather on your phone, being
able to access Google maps from a separate application, or triggering an Internet of Things devices — they
all rely on APIs to function. APIs and microservices have changed the web development world profoundly.
2. Python:
Python is an interpreted high-level general-purpose programming language. Python's design philosophy
emphasizes code readability with its notable use of significant indentation. Its language constructs as well
as its object-oriented approach aim to help programmers write clear, logical code for small and large- scale
projects. Python is dynamically typed and garbage collected. It supports multiple programming paradigms,
including structured (particularly, procedural), object-oriented and functional programming. Python is often
described as a "batteries included" language due to its comprehensive standard library.
Guido van Rossum began working on Python in the late 1980s, as a successor to the ABC programming
language, and first released it in 1991 as Python 0.9.0.Python 2.0 was released in 2000 and introduced new
features, such as list comprehensions and a garbage collection system using reference counting. Python 3.0
was released in 2008 and was a major revision of the language that is not completely backward compatible
and much Python 2 code does not run unmodified on Python 3. Python 2 was discontinued with version
2.7.18 in 2020.
Python 3.9 (Current October 2020 Dictionary Merge & Update Operators
Version) New removeprefix() and removesuffix() string
methods
Built-in Generic Types
II. CONTENT
3.1 Advantages of Python:
According to Stackoverflow, Python is the most preferred language which means that the majority of
developers use python.
Python today has multiple implementations including Jython, scripted in Java language for Java Virtual
Machine; IronPython written in C# for the Common Language Infrastructure, and PyPy version written in
RPython and translated into C. To be noted, Cpython which is written in C and developed by Python Software
Foundation is the default and most popular implementation of Python. While these implementations work in the
native language they are written in, they are also capable of interacting with other languages through use of
modules. Most of these modules work on community development model and are open-source and free.
The diverse application of the Python language is a result of the combination of features which give this
language an edge over others. Some of the benefits of programming in Python include:
of the many tools available for GUI development using Python are PyQt, Tkinter, Python GTK+, wxWidgets,
and Kivy.
community support, and an enormous collection of libraries. Python can also be used as an abstraction layer in a
device firmware while C/C++ handles the system level side of things.
Web Development involves programming both client and server side. Programming at server side is
associated with writing web applications, providing web site, web pages, etc.
First tier is the client which is a web browser which renders the static/dynamic content returned by server,
middle tier is the server which uses dynamic web languages and tools such as Python, PHP, Ruby, ASP.NET for
web application development which handles the user specific request, and returns the data. Third tier is the
database which provides storage means.
Programming at client side is associated with developing tools or interfaces for accessing these web
applications, sites, or pages. Server-side programming languages include Python, PHP, Ruby, ASP.NET, Java,
CGI (C, Perl). Client-side programming languages include HTML, CSS, and JavaScript.
CGI and WSGI - Web Server Application Development using Python Standard Library
4.1 CGI
Dynamic web involves generating the reply on the fly based upon the user input (such as input in a form).
Since, server can only take the user request and to return the response; it cannot handle user-specific data and
generate the response. So, it uses web applications for dynamically generating response.
Creation of a web server needs a base server and a handler. Base server is responsible for carrying out HTTP
communication between client and server, whereas handler is responsible for processing the request and
returning the result in the form of a web page or document. The module BaseHTTPServer has the common base
server class HTTPServer available in it. Within the same module, basic handler termed
BASEHTTPRequestHandler is available, but it offers only the functionality to take the client input.
SimpleHTTPRequestHandler found in SimpleHTTPServer module offers additional GET functionality. GET
method is used for fetching the requisite document from the database. Parameters of GET method are part of
URL. Similar to GET method is the POST method that is used for updating data. POST parameters are included
in the body of the document. CGIHTTPRequestHandler found in CGIHTTPServer module takes
SimpleHTTPRequestHandler and provides additional functionality of POST method.
When a web server receives a client request via GET or POST method, it invokes the web application, and
returns the dynamically generated HTML page to the client. This process takes place through CGI. CGI is the
Common Gateway Interface, which act as an interface between server and the application program. Figure 2
presents overview of working of CGI. CGI application depicted in Figure may use database for data storage and
retrieval purpose.
CGI provides support for use of cookies using which server saves data on client side. It also supports
processing of multiple inputs provided for a field, for example using checkboxes. Web programming using CGI
is not preferred since for each client request, server forks a process of CGI python program. This will lead to
wastage of time since python interpreter will be initiated for each request. For a large number of requests, this
may bring the server to halt. The problem associated with CGI can be overcome by using two modes–
embedded or daemon.
4.2 WSGI
Web Server Gateway Interface (WSGI) is a standard that facilitate interaction between the server and the
application by acting as an interface. It is currently the most preferred interface for python web programming.
Figure 3 presents overview of working of WSGI.
A WSGI application is an invokable application that takes two parameters. First parameter is a dictionary
type that comprises of environment variables such as HTTP_HOST, SERVER_PROTOCOL and their values.
Second parameter is a callable function that must be executed for initiating response to the client. The response
prepared contains response code indicating status of response which comprises of status code and reason phrase
(200 OK, 302 Found, 403 Forbidden, 500 Server Error). HTTP response may also contain HTTP headers such
as Date (date and time of message initiation), Server (server software and its version), Content-Type (type of
content in response body) application is sent back to the client. It may be noticed that WSGI acts as an interface
between the application and the server. The program at server side that invokes WSGI application can either be
written by the developer or he may use the python provided reference server available in library
wsgiref.simple_server.WSGIServer. Thus, using WSGI one can implement both sides of the interface, server as
well as application.
5.1 Django:
Being a full stack framework, Django supports quick web application development requiring lesser core to be
done. It is popularly known as ―the web framework for perfectionists with deadlines‖ . Django was first released
back in 2005 when front- ends were relatively straightforward. There were no dedicated JavaScript front-end
frameworks like React, Angular, Vue, Ember, and the rest. It provides ease in creating web applications with
fewer lines of code and is scalable. It includes a built-in server that can be used for developing and testing the
applications. The framework comes with comprehensive, well-written documentation. Features of this
framework include templates, support for relational database model (Databases include MySQL, SQLite, Oracle,
and PostgreSQL), comprehensive security, etc. It is well-suited for database driven applications. The framework
is based on the principle of reusability of code and no redundancy of information. Examples of applications built
using this framework include Pinterest, Instagram, Mozilla, Spotify, The Onion, Diqus.
Why Django?
• Django is a rapid web development framework that can be used to develop fully fleshed web applications in a
short period of time.
• It has built-in admin interface which makes easy to work with it.
• It is very scalable.
MVT Structure
MVT Structure has the following three parts (Figure 5) –
Model: Model is going to act as the interface of your data. It is responsible for maintaining data. It is the logical
data structure behind the entire application and is represented by a database (generally relational databases such
as MySQL, Postgres).
View: The View is the user interface — what you see in your browser when you render a website. It is
represented by HTML/CSS/JavaScript and Jinja files.
Template: Django’s template language is designed to strike a balance between power and ease. It’s designed to
feel comfortable and easy-to-learn to those used to working with HTML, like designers and front-end
developers. But it is also flexible and highly extensible, allowing developers to augment the template language
as needed.
# \myApp_project\myApp_root\
\myApp
init.py
asgi.py # Django 3 only settings.py
urls.py wsgi.py
manage.py-This file is used to interact with your project via the command line(start the server, sync the
database… etc.
$ python manage.py help
folder ( myApp ) – This folder contains all the packages of your project. Initially it contains four files –
_init_.py – It is python package.
settings.py – As the name indicates it contains all the website settings. In this file we register any applications
we create, the location of our static files, database configuration details, etc.
urls.py – In this file we store all links of the project and functions to call.
wsgi.py – This file is used in deploying the project in WSGI. It is used to help your Django application
communicate with the web server
Now let’s look inside the \myApp_root folder to see what Django has created -
• The migrations folder is where Django stores migrations, or changes to your database. There’s nothing in here
you need to worry about right now.
• admin.py is where you register your app’s models with the Django admin application.
• tests.py - contains test procedures that run when testing of the app starts.
band_listing(request):
bands = models.Band.objects.all()
return render(request, 'bands/band_listing.html', {'bands': bands})
Authentication
Django comes with a full-featured and secure authentication system. It handles user accounts, groups,
permissions, and cookie-based user sessions. This lets you easily build sites that allow users to create accounts
and safely log in/out.
@login_required
def my_protected_view(request):
"""A view that can only be accessed by logged-in users"""
return render(request, 'protected.html', {'current_user': request.user})
Security
Django provides multiple protections against:
• Clickjacking
• Cross-site scripting
• Cross Site Request Forgery (CSRF)
• SQL injection
• Remote code execution
5.2. TurboGears
TurboGears framework combines SQLAlchemy, Pylons, Genshi, Repoze, and Tosca Widgets. This framework
includes 1.x and 2.x series where the 2.x series comprises of all the above specified components. However, 1.x
series consists of SQLAlchemy, Genshi, CherryPy, and MonchKit. It supports both client and server-side web
programming. It offers designer friendly template system. It has found its use mainly for offering solutions for
industrial problems with high complexity. The framework supports several databases among
which SQLAlchemy is considered to be most powerful database management system. This framework is used
by SourceForge, Fedora Community, TavolaClandestina, Glossom, ShowMeDo, etc.
5.3. web2py
The framework supports fast development of scalable, secure, and portable web applications. It does not require
any installation and can be run via USB drive. This framework has no dependencies. The web2py application
itself acts as an Integrated Development Environment offering all the capabilities such as creating application,
debugging, and testing. It comes with a ticketing system that helps in tracking the source of error occurred by
assigning a ticket to the user. It supports several databases such as MySQL, PostgreSQL, SQLite, Oracle.
Examples of applications built using this framework include Movuca, Instant Press, and LinkFindr.
5.4. Flask
Flask framework is based on Werkzeug, and Jinja 2, and good intensions [28]. This framework has no
dependencies apart from Python Standard Library. Flask does not include components that need third party
support such as validating form or providing a means of communication between the application and the
database. However, such features may be added using extensions. Services offered by this framework include
built-in HTTP server, support for unit testing, and RESTful web service [29]. Applications built using this
framework are minitwit, flaskr, flask.pocoo.org etc.
5.5. Pyramid
Pyramid is a fast, reliable, simple, and mini web development framework [30]. This framework is compatible
with Python 3. Being a fully documented framework, developers find an easy start for developing web
applications. It supports several databases such as SQLalchemy, Zope Object Database, and CouchDB.
Websites built using this framework include newcars.com, SurveyMonkey, PwnedList.
Almost all python web frameworks specified above support features such as built-in server, internationalization
(helps in adding languages and their translation), authentication and authorization of end-user, caching,
debugging, error logging (help in keeping track of errors occurred along with their description, and time),
routing the request to another URL, and managing sessions.
IV. CONCLUSIONS
Inspite of availability of so many web development languages, Python is evolving as the popular language
among developers. Web development in Python can be done using CGI or WSGI that uses python standard
libraries. However, such development requires building web applications from scratch. So, Python provides web
development frameworks such as Django, Pyramid, web2py that helps in web application development. This
helps the Python as a Tool for Web Server Application Development developer in doing the minimal requisite
core needed for building the applications.
REFERENCES
[1]. Author name(s), paper name, Journal/ Magazine/ Periodical name, issue no., page nos.
[2]. Grove, Ralph F. "Web Based Application Development." Jones & Bartlett Publishers, 2009
[3]. Forcier, jeff, Paul Bissex, and Wesley ―Python web development with Django‖, Addison-Wesley
Professional, 2008
[4]. https://f5-studio.com/articles/introduction-to-modern-web- development/
[5]. https://en.wikipedia.org/wiki/Python_(programming_language)
[6]. https://www.python.org/doc/versions/
[7]. https://towardsdatascience.com/top-16-python-applications-in-real-world- a0404111ac23
[8]. "Django" https://www.djangoproject.com/
[9]. "TurboGears" http://turbogears.org/
[10]. "web2py" http://web2py.com/
[11]. "Flask" http://flask.pocoo.org/