Python urllib2 with gzip or deflate encoding

Python LogoThe other night I was working on some Python code that interacts with the zKillboard API. The API call returns kill information via JSON for EVE Online. While making the request, I was getting an error that I was using "non-acceptable encoding." It turns out that the zKillboard guys require you to accept gzip or deflate encoding in order to save on bandwidth. Here's a snippet showing how I added the "Accept-encoding" header to my urllib2 request:

<script src=""></script>
import json
import urllib2

request = urllib2.Request('')
request.add_header('Accept-encoding', 'gzip,deflate')

data = urllib2.urlopen(request)

kills = json.load(data)

For all you EVE Online guys out there, the following API request will give you the kills in Jita over the last 24 hours. If you like the idea of a video game you can write code against, check out EVE Online's free trial. That link will give you 30 days free.

Related Reading:

My Bookshelf

Reading Now

Other Stuff