views:

176

answers:

4

I have a php script that outputs a random image each time it's called. So when I open the script in a web browser, it shows one image and if I refresh, another image shows up.

I'm trying to capture the correct image from visiting the web site through a command line (via mechanize). I used urllib2.urlopen(...) to grab the image, but each time I do that I get a different image. I want to be able to consistently grab the same image. How can I accomplish that?

Thanks!

UPDATE: Here's an example of what I'm talking about. If you reload this image in a web browser, a different image pops up each time. If you right click and save, you get the correct image. And if you keep doing that, you keep getting the correct image... BUT, how do you that from a command line?

http://www.biglickmedia.com/art/random/index.php

+1  A: 

Most likely your image is cached by browser set this:

<?php
      header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
      header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
?>

and each time you generating the image use another name for it (can be done by adding milliseconds to original one).

Artem Barger
+1  A: 

You can try caching the image generated to disk and make it available to the user via a different link.

After the image is generated, move it to a temp folder, where the user can download the generated static image. After it is downloaded, delete to make space.

Nikko
A: 

Grab it once and cache it on your side.

Andriy Bohdan
+1  A: 

When you save it from the browser, it is not going back to the server to re-request the image, it is serving the one that it is displaying from its cache.

Your command line client needs to do the same thing. You need to save the image every time you request it. Then when you find the one you want to keep you need to copy the already saved image to wherever it is you want to keep it permanently.

If the server is always serving a new random image there's nothing else you can do.

Gareth Simpson