Each time user accesses http://www.example.com/some-random-symbols-1×1.png, I should return transparent image 1x1px. I’ve created according file, but how should I read it in my code to return to the user? I know how to display the image from the datastore or blobstore. But have no idea how to read and return binary file.
It can not be static file due to the following reasons:
- url will contain some-random-symbols;
- once url is accessed, prior to displaying images, I would like to log that somebody accessed the file.
In a more general case, I’d use the blobstore and the BlobstoreDownloadHandler, but for a tiny gif that will definitely fit into memory, something like this to read the file’s content:
I’d put this outside of my handler, so it was done once per instance. If you’re using 2.5, then you’ll need to import ‘with’ from future, or open and close the file yourself.
then in your handler, assuming webapp2: