Advertisement

Advertisement

Python FTP Client Tutorial

FTP or File Transfer Protocol is a common way to transfer files. For FTP, Python has a built in package called ftplib.

There is also a Secure File Transfer Protocol (SFTP) that uses SSH to encrypt the communication. We will focus just on traditional FTP in this guide. For SFTP you can check out the Paramiko package.

In this guide, we will look at some basic operations like:

  • Connect and login to an FTP server
  • List directories and files
  • Upload and download files

Note these examples are all using Python 3.

Read and Send Email with Python

Python 3 has built-in libraries for IMAP, POP3, and SMTP. We will focus on learning how to send mail with SMTP and read/manage email with IMAP. We will also look at how to send an SMS text message using email.

If you need your own email hosting, check out Interserver.net hosting where you can host unlimited emails for unlimited domains as cheap as $4/month. You could also set up your own SMTP server on a VPS, but that is a hassle.

Python MySQL Tutorial

MySQL is an ubiquitous database server. Many hosts, like Interserver.net offer unlimited MySQL databases with a $4/month plan. With their hosting, you can also setup as many PHP, Python, Ruby, or Node.js apps.

There are a few libraries available, but the one I'm focusing on is PyMySQL, a pure Python implementation. This version is the easiest to install with the least amount of dependencies and most portable.

You could use an ORM like SQLAlchemy or Django. Those options are good if you need to easily switch between MySQL and another database like SQLite. Here, we will focus only on MySQL and assume this is the only database being used.

If you're interested in learning how to setup a Python web app on Interserver, it is really easy using cPanel. Check out my tutorial Setup Python WSGI apps on cPanel (Flask/Django).

How to format an SD card or USB flash drive

If you want to create a bootable SD card or USB flash drive using a disk image like a .iso or .img you have a few options.

  • Use the dd command-line tool
  • Use a GUI tool like BalenaEtcher

This is a common task when setting up an SD card for a Raspberry Pi or creating a USB flash drive with a Linux LiveCD distro. In this example we will review both the Etcher option and dd option.

Python SQLite3 Tutorial

Python has a built-in Sqlite3 module named sqlite3. This module allows you to create, connect, and modify SQLite 3 databases.

To learn more about SQLite3 and how to use it in general, check out my SQLite3 Tutorial and my other sqlite tutorials.

If you need a database-agnostic library, something that you can use with SQLite and then easily configure to start using MySQL without rewriting all of your code, you want to use an ORM. ORM stands for Object-Relational-Mapping and the most popular one for Python is SQLAlchemy and Django's ORM.

Using stdin, stdout, and stderr in Python

Among the popular operating systems, they have all standardized on using standard input, standard output, and standard error with file desciptors 0, 1, and 2 respectively. This allows you to pipe the inputs and outputs to different locations. Let's look at how to utilize standard input, output, and error in Python.

To learn more about piping, redirection, stdin, stdout, and stderr in general, see my tutorial STDIN, STDOUT, STDERR, Piping, and Redirecting.

Taking Command Line Arguments in Python

When processing command line arguments in Python, you have a few options available:

  • sys.argv - a raw list of arguments passed to the Python interpreter
  • argparse - Python's standard package for argument parsing (replaces getopt and optparse)
  • fileinput.input() - A special function, similar to Ruby's ARGF, that returns the lines of input from standard input or from the list of filenames provided as arguments.
  • docopt - A third-party package that uses the docstring as documentation and configuration for arg parsing
  • click - A third-party package that uses decorators to map command arguments

Let's look at examples of each option.

Create Parent Directories with Python

A common task when working with systems is to create directories. There can be times when you need to create a directory several layers deep. The normal mkdir commands only let you create a single level at a time. This example will show you how to create multiple directory levels at once, the equivalent of running mkdir -p in the Bash shell.

Use jpackage to Create Native Java App Installers

jpackage is a new tool with JDK 14 that generates native system installers for distributing your appliction. It will create .msi for Windows, .dmg for Mac, and .deb or .rpm for Linux distrubtions. This is ideal for GUI applications. This guide will demonstrate how to use this tool to create system native installers for your applications.

Symmetric encryption with Python using Fernet (AES)

The third-party cryptography package in Python provides tools to encrypt byte using a key. The same key that encrypts is used to decrypt, which is why they call it symmetric encryption. Fernet is an encryption spec that utilizes AES-128 under the hood with HMAC and some other additions. If you need to encrypt and decrypt some data using Python, this is a very easy way to do it.

How to Create Java Runtime Images with jlink

Before Java 9 and Project Jigsaw, you would have to package the entire monolithic Java Runtime Environment (JRE) to distribute your application with an embedded JRE runtime. This took up a lot of space. Now, you can generate your own custom runtime with only the modules you want to keep the size of the final package smaller.

You can think of a runtime image as another name for a Java runtime environment (JRE). When you create a custom runtime, you are generating a new JRE that only includes the modules you want. You can exclude core java modules that you don't need and you can include custom modules that you wrote. They will all be "baked in" to the runtime.

If you are familiar with Python Virtual Environments, it is similar to that. It is a directory that contains an isolated Java environment with only the specific dependencies needed that will not conflict with other isolated runtimes.

After following this guide, you will understand how to use jlink to create runtime images with only the modules you want. You should also know how to add custom modules including third-party modules like JavaFX.

Advertisement

Advertisement