Morphex's blogologue (Life, technology, music, politics, business, mental health and more)

This is the blog of Morten W. Petersen, aka. morphex in various places. I blog about my life, and what I find interesting and/or important. This is a personal blog without any editor or a lot of oversight so treat it as such. :)

My email is morphex@gmail.com.

I am leavingnorway.info.

An OGG/Vorbis player, implemented in Javascript.

My Kiva bragging page
My shared (open source) code on GitHub

Morphex's Blogodex

News
Me on Instagram
Slashdot

Zope hosting by Nidelven IT

Morten Petersen on Linkedin

Morten Petersen on Facebook

Morten Petersen on SoundCloud

Morten Petersen on MixCloud

Blogologue on Twitter



Older entries



Atom - Subscribe - Categories

Facebook icon Share on Facebook Google+ icon Share on Google+ Twitter icon Share on Twitter LinkedIn icon Share on LinkedIn

Fixing up an old blog, transparent PNG embedded in Python code

So I've been fixing up on my blog lately, adding some iframe and javascript code, as well as backend code, to make it play my playlist from SoundCloud automatically.

This blog is running on Zope 2, and the blog software was written by me.. it's not been maintained well though, so I've been fixing up some minor things here and there.

One of the things I needed to fix was that on the search page, some images were referenced that for some reason wasn't approved by the weblog layer, and raised old-style HTTP authentication boxes.

So I figured I'd fix this in an easy and quick way. I created a new image in The GIMP, 1x1 transparent, and saved it as a PNG with as little metadata as possible and level 9 compression.

Looking at the file afterwards in Python, it looks like this:

-rwxrwx---+ 1 Morphex None 117 aug 2 14:26 1x1_transparent.png
-rwxrwx---+ 1 Morphex None 68 aug 2 14:36 1x1_transparent2.png

Morphex@Morphex-PC /cygdrive/C/Users/Morphex.Morphex-PC/Documents
$ python
Python 2.7.10 (default, Jun 1 2015, 18:17:45)
[GCC 4.9.2] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> f=open('1x1_transparent2.png')
>>> f.read()
'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4<MY BLOG LAYOUT LINE BREAK>
\x89\x00\x00\x00\x0bIDAT\x08\xd7c`\x00\x02\x00\x00\x05\x00\x01\xe2&\x05\x9b\x00\x00\x00\x00IEND\xaeB`\x82'
>>>

I updated the IssueDealerWeblog code to return this:

    security.declarePublic('image')
    def image(self, id=None, REQUEST=None, RESPONSE=None):
        """Returns an image related to the published issue."""
        result = self.catalog_search(id=self.get_published_ids(),
                                        get_local_image_ids=id,
                                        meta_type='Issue')
        if result:
            return base.base.image.im_func(self, id=id, REQUEST=REQUEST, RESPONSE=RESPONSE)
        else:
            RESPONSE.setHeader('content-type', 'image/png')
            RESPONSE.setBody('\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x00\x01\x00\x00\x00\x01\x08\x06\x00\x00\x00\x1f\x15\xc4<MY BLOG LAYOUT LINE BREAK>
\x89\x00\x00\x00\x0bIDAT\x08\xd7c`\x00\x02\x00\x00\x05\x00\x01\xe2&\x05\x9b\x00\x00\x00\x00IEND\xaeB`\x82', lock=True)
            return

And now the search page returns without raising any authentication boxes. It's not the most purist way to just fail silently like this, but a quick fix that helps with the appearance and user-friendliness for a regular user.

[Edit: Adding paragraph..] And in case you're wondering, that image binary is released to the public domain, so you can use the Python string here anywhere. Or download the image No description available.

[Permalink] [By morphex] [Python and web (Atom feed)] [02 Aug 14:38 Europe/Oslo]