Python Date/Time Libraries


Maya: DateTimes for Humans
https://github.com/kennethreitz/maya

Arrow: Python library that offers a sensible, human-friendly approach to creating, manipulating, formatting and converting dates, times, and timestamps
http://arrow.readthedocs.io/en/latest/

Pendulum: Python Date/Time Made Easy
https://pendulum.eustace.io/

Delorean: Time Travel Made Easy
https://github.com/myusuf3/delorean

Moment: Dealing with dates in Python shouldn’t have to suck
https://github.com/zachwill/moment

udatetime: Fast RFC3339 compliant date-time library
https://github.com/freach/udatetime

Fast ISO8601 date time parser for Python written in C
https://github.com/closeio/ciso8601

Python – Check SSL Certificate Expiry Date


Just playing with python 3 to make a simple utility to check my domain expiry date

# save as check_ssl.py

import OpenSSL
import ssl, socket
import argparse

# get domain
parser = argparse.ArgumentParser()
parser.add_argument(“domain”)
args = parser.parse_args()
domain = args.domain

# get SSL Cert info
cert = ssl.get_server_certificate((domain, 443))
x509 = OpenSSL.crypto.load_certificate(OpenSSL.crypto.FILETYPE_PEM, cert)
x509info = x509.get_notAfter()

exp_day = x509info[6:8].decode(‘utf-8’)
exp_month = x509info[4:6].decode(‘utf-8’)
exp_year = x509info[:4].decode(‘utf-8’)

exp_date = str(exp_day) + “-” + str(exp_month) + “-” + str(exp_year)

print(“SSL Certificate for domain”, domain, “will be expired on (DD-MM-YYYY)”, exp_date)

then run with the domain you want to check as the argument

python check_ssl.py example.com

 

Python – PyODBC – Microsoft Access : Simple script


Here’s a simple python script showing how to access MDB (Microsoft Access) database

import os
import pyodbc

dataFile = "Northwind.mdb"
databaseFile  = os.getcwd() + "\\" + dataFile
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%s" % databaseFile
dbConnection   = pyodbc.connect(connectionString)
cursor = dbConnection.cursor()
cursor.execute("select top 5 * from Suppliers")
rows = cursor.fetchall()
for row in rows:
    print row.CompanyName

take a look at the script below to get column names, column type and width

import os
import pyodbc

dataFile = "Northwind.mdb"
databaseFile  = os.getcwd() + "\\" + dataFile
connectionString = "Driver={Microsoft Access Driver (*.mdb, *.accdb)};Dbq=%s" % databaseFile
dbConnection   = pyodbc.connect(connectionString)
cursor = dbConnection.cursor()

for row in cursor.columns(table='Suppliers'):
    print "Field name: " + str(row.column_name)
    print "Type: " + str(row.type_name)
    print "Width: " + str(row.column_size)