Sunday, 1 January 2017

Generating signed cookies for Amazon CloudFront in Python

Generating signed urls or cookies for CloudFront is not straightforward to do using Boto3; you're responsible for generating the signature and stitching everything together. For those needing signed urls, the Boto3 crew have given a nice, succinct example in their docs.

While Amazon's doc give a good tutorial of how to do it by hand, I couldn't find a decent Python sample, so I wrote one.

It's written for Python3, but should be easy to modify it to work on 2 and the only dependency is the cryptography library.

Check out the script on GitHub.

Sunday, 15 May 2011

Getting OpenSSL Ruby support running on Amazon EC2 Linux AMI instances

I was trying to get OmniAuth in Sinatra up and running on a vanilla Amazon Linux AMI with RVM-installed Ruby 1.9.2. I'd installed the basic development build tools, but kept getting an openssl error when trying to startup my Sinatra app:

`require': no such file to load -- openssl (LoadError)

Turns out I needed to install the OpenSSL development libs. Make sure you have all the necessary build tools installed:

sudo yum groupinstall development-tools

Then install the OpenSSL libs:

sudo yum install openssl-devel

Or,alternatively, all of the development libs:

sudo yum groupinstall development-libs

Make sure you uninstall any existing ruby RVM builds:


rvm uninstall 1.9.2
rvm cleanup all


Then reinstall:

rvm install 1.9.2

And it should now be good to go.

Tuesday, 20 October 2009

MacPorts Reference

I use MacPorts to install a lot of development software: latest Python versions, MySQL, MongoDB, etc. And I can never recall the various port commands, so I'm listing the ones I use here:

Update MacPorts: sudo port selfupdate

Search for a port: port search port_name

Install a new port: sudo port install port_name

List a port's variants: sudo port variants port_name

Install a new variant port: sudo port install port_name +variant_name

List installed ports: port installed

List outdated ports: port outdated

Update outdated ports: sudo port install outdated

Clean up temp files: sudo port clean --all installed

Delete old install versions: sudo port -f uninstall inactive

They're the ones I use the most. Anyone suggest others that are handy?

Thursday, 8 October 2009

Python through Osmosis

A short note: Python Osmosis is a great video podcast for those who want to learn, or brush up on aspects of, Python. Get it from the website, or subscribe through iTunes.

Saturday, 3 October 2009

An Alternative to ElementTree -- lxml

I use ElementTree as my XML library of choice in Python, but sometimes it is lacking in what I need it to do. I have always found its support for namespaces to be awkward to use, and recently I needed to validate generated XML against a collection of XML schemas, but ElementTree has no support for this.

So I had a hunt around and discovered lxml, a Python XML library that -- not only appears to actively developed -- provides good compatibility with ElementTree while layering on additional functionality.

The only downside to lxml is that it requires both libxml2 and libxslt to be installed, though if you install from the binary egg, it includes these libraries, making it very straightforward to run on Windows. On the Mac, you might need to upgrade the libraries, which can be a chore, though using MacPorts makes life easier.

Thursday, 10 September 2009

A new Django Podcast - Django Dose

With the demise of This Week in Django, there was a gap in the podcasting realm for Django news. Ruby and Rails have a couple of really decent podcasts, namely Ruby5 and Rails Envy.

Now there's Django Dose which, the dose guys say, it the spiritual successor to This Week in Django. I look forward to seeing how this one pans out, though it needs to get into iTunes soon; I searched for it there and had no luck.

Wednesday, 9 September 2009

NTLM Authentication with Suds

We have a requirement to hit a SOAP web service running on a Windows box with IIS, and unfortunately this service is protected by NTLM authentication. While basic HTTP authentication is well supported and trivial to implement in Suds -- a Python SOAP client module -- NTLM is not.

There is a great blog entry describing how to solve this problem; however the post recommends placing the code within Suds itself, and I had to make a couple of minor modifications to get it to work with the with the latest Suds version (0.3.6).

Here are (very minor) the modifications to C. Bess's code:

from suds.transport.https import HttpAuthenticated
from suds.transport.http import HttpTransport
from ntlm import HTTPNtlmAuthHandler
import urllib2

class WindowsHttpAuthenticated(HttpAuthenticated):
"""
Provides Windows (NTLM) http authentication for suds.
@ivar pm: The password manager.
@ivar handler: The authentication handler.
@author: Christopher Bess (modified by Matt Sullivan)
"""
def __init__(self, **kwargs):
HttpTransport.__init__(self, **kwargs)
self.pm = urllib2.HTTPPasswordMgrWithDefaultRealm()
self.handler =\
HTTPNtlmAuthHandler.HTTPNtlmAuthHandler(self.pm)
self.urlopener = urllib2.build_opener(self.handler)
urllib2.install_opener(self.urlopener)

Using this authentication class is straightforward:

t = WindowsHttpAuthenticated(
username = 'DOMAIN\%s' %username,
password = password
)
url = 'http://www.somesite.com/service.asmx?wsdl'
return Client(url, transport = t)