category: Web development | Python Tutorial

Category: Web development

Twisted

Twisted is an event-driven network engine.

Its more general than CMS-like frameworks such as Flask or Django.

You can create many network applications including a custom server, low level sockets echo client, Bit-torrent client or even create a mail client.
Protocols

Related course: Python and Django Full Stack Web Developer Bootcamp
icon

Protocols

You don’t have to define the protocol. Twisted includes lots and lots of protocol implementations:

That includes: HTTP, FTP, SMTP, POP3, IMAP4, DNS, IRC, MSN, OSCAR, XMPP/Jabber, telnet, SSH, SSL, NNTP.

python twisted

Install

Python Twisted can be installed with the command

pip install twisted

It’s recommended to use a virtual environment (virtualenv)

You can install virtualenv with pip:

pip install virtualenv

Navigate to your projects directory. Then run the commands below:

$virtualenv try-twisted
$ . try-twisted/bin/activate
$ pip install twisted[tls]
$ twist --help

Server

Twist supports many protocols out of the box.

A web server will be stared if you type the command

twist web

To open an ftp server, simply type

twist ftp

If you type the twist command you can see an overview of all supported protocols.

Flask vs Django

There are many Python web frameworks: Django, Flask, Pyramid, Bottle, Tornado, Pecan, Diesel, Falcon, and many more.

Django and Flask are very popular web frameworks.

In this article we’ll focus on Django and Flask.

Related course: Python and Django Full Stack Web Developer Bootcamp
icon

Framework Size

Flask is a microframework. It provides the minimum to make a web app. This means it’s also faster to learn Flask than Django.

Django is heavier than the Flask framework, but provides more features.

Data storage

The Django framework uses an ORM by default (Object Relational Mapper). This maps classes to database tables.

The Flask framework leaves it up to developer. You can use it with SQLAlchemy, MongoDb or something more simple like SQLite.

That can be a better choice as an ORM is not a silver bullet, sometimes you waste development time because you can’t modify the SQL query directly.

Template system

Both Django and Flask have a template rendering system inspired by Jinja2.

Functionally they are the same, but they are slightly different. That difference means you can’t always copy templates from one system to another without issues.

Admin system

Django provides an admin system. It comes with the ORM database system and a directory structure.

There is an all inclusive experience with Django. That means that multiple Django projects will have the same directory structure.

Flask doesn’t have these features out of the box, to have an admin system or use an ORM you’ll need to install custom modules.

Python web application

Web applications can be made with Python. To create web applications, you need a Web Framework. A Web Framework is a module that makes web development much easier.

The most popular Web Frameworks is: Django.

Related course: Python and Django Full Stack Web Developer Bootcamp
icon

Backend - Web Frameworks

Which other web Frameworks exist?
There are more Web Framworks like Bottle, Pyramid and Morepath. You can also implement your Web Framework, but that’s like reinventing the wheel.

What is usually inside an Web Framework
A lot of code for creating web apps. A webframework contains all kind of functionality thats useful when creating a web app.

This includes the typical HTTP operations, database access, model-view-controller, URL router and many more:


FeatureUse
URL routingGiven a URL, execute the rightccode
Database Manipulation ToolsAn ORM or raw SQL.
FormattingHTML, JSON, XML output
Template EngineDisplays the data using templates
Session support
SecurityCSRF protection

Frontend

AngularJS and React
When creating your app, you may want to build an awesome frontend interface using AngularJS or React. This can be combined using an API on the Python side. They have a steep learning curve, but they are great modules.

AngularJS course: Angular 7 (formerly Angular 2) - The Complete Guide
icon

Hosting

To put your web app online, you can use PythonAnywhere.
By using their Cloud, you don’t have to setup any servers manually. Its very easy for Python web hosting.

Flask hello world

Flask a web framework that can be used to build web apps with Python.

It’s very minimalist and lightweight, great for making a SaaS app.

Related course: Python Flask: Make Web Apps with Python

Flask Example

First install Flask, you can do so with pip.
Once installed copy the file below and save as web.py

from flask import Flask

app = Flask(__name__)

@app.route('/hello')
def helloIndex():
return 'Hello World from Python Flask!'

app.run(host='0.0.0.0', port= 81)

Start with:

python web.py

Python will show:

# python web.py 
* Running on http://0.0.0.0:81/ (Press CTRL+C to quit)

This means you have a web server running on port 81.

Open your web browser and type http://127.0.0.1:81/hello

This will show you the hello world message.

Flask hello world

Routes

We defined the route (‘/hello’) and mapped it to a function:

@app.route('/hello')
def helloIndex():
return 'Hello World from Python Flask!'

If you leave it blank you’ll have the index route.


1