Posts Tagged python

Checking a SSL certificate’s expiry date with Python

Before I found the --keep-until-expiring option in the Let’s Encrypt command line client, I was thinking I’d have to parse the cert, extract the expiry date, then check it against the current date before returning True or False.

Thankfully I found the much easier option, but I decided to post the code I wrote to read the date just in case I need something like it in the future. Read the rest of this entry »



A grab bag of half-finished posts

Because I’ve started a bunch of posts, haven’t finished them, don’t really want to delete them but stuff looks potentially useful, and I’m clearing stuff out.

Read the rest of this entry »


1 Comment

Getting a Python dev environment setup on Windows

I’ll be doing a fair amount of work in Python in the next few months, so I decided to sit down and get a good dev environment going. First on my laptop (32 bit is easier to deal with), then on my desktop.

So I’ll be doing 3 things:

  1. Getting Git setup
  2. Getting Git working with GitHub
  3. Getting Python and pip installed

First of all, started off with Git. Downloaded from, and installed it following GitHub’s Windows setup guide. (As a side note, I’m not dealing with GitHub’s native app because when I last used it, you couldn’t select individual blocks of code to be committed. I know Git philosophy is to commit often, but I just commit when I’ve got something working, and I’d have touched a few different files and done more than one thing.)

They’ve improved the installer since I last used it, so it was deceptively simple. No more messing around with config files, whee!

Second thing was integration with Github. SSH keys allow password-less authentication (I hate InteliJ’s Github integration because it uses the HTTPS repo, which requires me to enter my github password). Once again, easiest thing to do was to follow Github’s guide on generating ssh keys. Another side note: It’s ssh -T [email protected] I was trying ssh -T and was wondering why it was failing. =|

And onto the third and final thing, which is also the most difficult: Getting Python up and running with pip installed & working. I’ll split this into two parts: getting Python, and getting pip working.

Getting Python is trivial – I downloaded it from I chose 2.7.3, but could have gone with 3.2.3. (In fact, probably should have, but modules are still coded to 2.7 compatibility, so that’s what I’ll use.)

Getting pip on is a bit more complicated – you have to install easy_install, then use that to install pip. So, I used the lovely directions at StackOverflow for inspiration:

  1. Grab setuptools from – make sure the version you get matches the version of Python installed – in my case, 2.7
  2. Install setuptools. You can follow the defaults, just make sure you install to the correct directory – where you installed Python. This should be auto-detected though.
  3. Open Powershell (I’m running Win 7; if you aren’t, you should be. And if you can’t run W7, open up command prompt instead.)
  4. In the shell, change to the directory where you installed Python, and then to the Scripts directory in that folder – ie. cd C:\Python27\Scripts
  5. Type ./easy_install pip – this works because setuptools added an executable called easy_install.exe to the Scripts/ folder
  6. Pip is now installed. If you want to install something with Pip (i.e. ), open up Powershell again if it’s not already open, change to that directory, and run ./pip install requests

For bonus points, and to get Python to run without having to prepend the directory where Python is installed to every command you run with Python, append the Python directory to your environment PATH variable.

How do you do this? On Win 7, type “Path” into the search bar in the start menu. It should get you something like this:

Select the “Edit system environment variables” option. Not the one with “Your account”.

That will take you to this screen. See the button close to the bottom labeled “Environment Variables”? That’s the one you want to click. And when you do that, you’ll get this:

I’ve skipped a bit, but you want to scroll though the box on the bottom to find the Path variable. When you’ve found it, either double click on it, and single click to select it, and press Edit.

When the screen comes up, hit “End” on your keyboard to jump to the end of the line, then add a semi-colon (which is the ; symbol, if you don’t know), and paste the directory where you installed Python. Or type it. Copy & Pasting directly from an Explorer window is less error prone, so that’s what I do.

Because it’s a system variable, it’ll only take global effect when you restart your computer. If you just want to use Python in Powershell though, just open a new Powershell instance. You can verify that Python’s present in your Path by typing $env:Path and looking at the end of the line that gets printed.


, , ,

1 Comment

Stripping image metadata in Python

Currently, as part of my file deduper,¬† I’m opening images, copying the image data to a new file, and saving that file.This is done using the Python Imaging Module, or PIL. I’m actively using an updated fork of it called pillow.

However, I’ve since discovered pyexiv2, something that says it allows the “access of … XMP metadata”. Which means it might allow XMP data access in .DNG files. The exiv2 docs say this is possible, at least.

This interests me because using DNG was something I tried, but then went back to .CR2 simply because using .CR2 forced Lightroom to keep the metadata & data in separate files. I do have a bunch of .DNG files from that period though that aren’t working with my deduper because the metadata changed enough, so this module would probably be a good addition to my growing toolkit.

My chosen method of copying & wiping metadata, like I did with mp3 files, should work the same way. Just a matter of finding the pyexiv2 command for deleting image metadata, something which exiv2 states is possible, but I can’t seem to find in the pyexiv2 docs.

, , ,

No Comments

Fun with Python and image processing

Today I got to work with something I’ve been meaning to for a while – Image processing with Python, in particular the PIL/Pillow library. (Pillow’s a forked, more up to date version of PIL, and it’s in PyPi.)

So about 30 minutes after planning and googling and reading the docs, I have a first version that takes a bunch of images, all the same size (it doesn’t check though), and randomises the list, then cuts vertical stripes of equal sizes out of them, and assembles it into something like this:

This is made up of stripes of 120 photos

Then I moved into variations – swapping the red and blue channels with this code. (Result)

Or how about staggering the rgb channels so each stripe is built with the red channel from the first image, green from the second, and blue from the third? (Again, oddly funky result.)

And finally, just combining channels from three different images wholesale, without the vertical striping? (The result looks pretty interesting with the pink glow.)

What will I do next? Probably look at combining different sized images – have some way of calculating the height & width of images and cropping random portions out of them such that they can be pieced together. Not like the random shapes of a jigsaw puzzle (PIL only supports box shapes), but a collage almost.

I’m still getting over the ‘this is awesome’ and wondering how the random number generators will affect the outcome of the image though.

, ,

No Comments

supybot plugin development

Problems I’ve faced & how I fixed them:

Read the rest of this entry »

, ,

No Comments

GUI programming in Python

It’s the next thing I want to try.

It looks like either wxPython or PyQT is my best bet.

GUI Programming with Python: QT Edition is a book about using PyQT, while

Building a basic GUI application step-by-step in Python with Tkinter and wxPython is about wxPython

Meanwhile, ShowMeDo is Python’s list of tutorials, which include GUI making.


No Comments

The power of prior research

One hour after I started, the bare bones are already up and running:

Mainly because I read up a lot during the day, so I understood exactly what I had to do.

And with that, sleep time.

, , ,

No Comments

On MP4 file headers and metadata

TL;DR: I have a bunch of MP4 video files that have identical content, but the internal metadata differs. I’m writing a Python function/module/program that’ll strip out the metadata, so hashing functions can find the duplicates. Read the rest of this entry »

, ,

No Comments