Python for Network Engineers

Implement a Url Shortener Service in Flask

By George El. • January 2021 • Reading time: 5 minutes

In this post I will describe how to implement a url shortener service using flask. There are two main ways to implement a url shortener service. The first one is to take the URL and apply the md5 hash algorigthm. Then convert this 128bit number to base62 or base64. The problem with this approach is that the base62 results in a 21 byte string which is long.

Log to Console, File and SMTP With Logging Module

By George El. • December 2020 • Reading time: 2 minutes

In this post I will show you how to use the logging module to log messages to console, file as well as send email using smtp, depending on the level of the message Create a logger First we will create a logger and set level to DEBUG import logging logger = logging.

Build a REST API With FAST API

By George El. • December 2020 • Reading time: 3 minutes

In this post I will show you how to build quickly a REST API using the package FAST API The endpoints for each will be as shown First create a virtual environment and install fast api and uvicorn python3 -m venv env source env/bin/activate pip install fastapi, uvicorn Instead of putting all the endpoints in one file we will use a modular approach and use one file for the routers and one for the switches.

Flask, uwsgi, Nginx with Docker

By George El. • November 2020 • Reading time: 8 minutes

In this post I will describe briefly what is docker and how to run a simple python program in a docker container. Then I will do the same for a flask app using uswgi and nginx in different containers and then I will combine the two using docker-compose. What is a Container?

Threads vs Processes vs Asyncio

By George El. • November 2020 • Reading time: 6 minutes

In this post I will explain the difference between threads, processes and asyncio and what is the best approach to use when you want to configure your devices. Concurrency vs Parallelism Lets first explain the difference between concurrency and parallelism. Concurrency means that an application gives the impression that executes many tasks at the same time, but in reality only one task is executed at a time.

Abstract Classes in Python

By George El. • August 2020 • Reading time: 2 minutes

In my post Classes in Python I have an example of creating a class named Car and instantiating this class. In reality you would rarely do something like this, but rather use this class as a base class in order to create subclasses. Subclasses are classes that extend the base class and inherit all functionality from it.

Create a Tkinter Gui With Sqlite Backend

By George El. • March 2020 • Reading time: 4 minutes

In this post I will show you how to create a single gui app using tkinter as frontend and sqlite as a backend. Python has many gui libraries and tkinter is one of them that is supported natively, that is you don’t need to install any extra modules. The app has the ability to search, add, update and delete router from a sqlite database.

Perform an Arp Poisoning Attack Using Scapy

By George El. • November 2019 • Reading time: 3 minutes

In this post I will show you how to perform an arp poisoning attack on local network using a python package called scapy. Scapy is a package that allows us to craft our own packets specifying all layers from layer2 to layer7. In my local network I have two pcs, with addresses 192.

How to Talk to Mysql Databases

By George El. • August 2019 • Reading time: 7 minutes

In this post I will describe how to use python to execute raw queries on mysql databases. install the database First we need to install mysql or maria-db (a fork of mysql) either in unix or windows. if you have windows 10 you can install mysql on wsl (windows subsystem for linux).

Using TextFSM to Parse Cli Output

By George El. • July 2019 • Reading time: 4 minutes

Many times we connect to a router or switch usually with netmiko and we want to parse the output of a command. TextFSM is a Python module for parsing semi-structured text into python tables. It uses templates that use regular expressions in order to parse the output. The good thing is that there are already hundreds of templates for many devices and commands.