Quantcast
Channel: CodeSection,代码区,Python开发技术文章_教程 - CodeSec
Viewing all articles
Browse latest Browse all 9596

Morphex's Blogologue: Fixing up an old blog, transparent PNG embedded in Python ...

$
0
0
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')

defimage(self,id=None,REQUEST=None,RESPONSE=None):

"""Returnsanimagerelatedtothepublishedissue."""

result=self.catalog_search(id=self.get_published_ids(),

get_local_image_ids=id,

meta_type='Issue')

ifresult:

returnbase.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.

[] [By morphex] [] [02 Aug 14:38 Europe/Oslo]

Viewing all articles
Browse latest Browse all 9596

Trending Articles