python keep-alive connection and download image -


i need download image site python, image changed every 1 second. possible: - set python connection [keep-alive] - download image; sleep 1.5; download image; sleep 1.5; download image; sleep 1.5; - close connection

i mean doesn't create connection site every 1.5 sec use 1 keep-alive connection. , close connection @ end of script (after 15 sec example). sure connection closed.

if have ideas how it, please show me example. thanks!

by using requests library optional flags:

import requests r = requests.get(url='http://upload.wikimedia.org/wikipedia/en/a/a9/example.jpg',stream=true) print r.headers['last-modified'] 

has file creation time output:

thu, 03 oct 2013 23:15:52 gmt 

we use 'stream=true' flag, this description, because:

at point response headers have been downloaded , connection remains open, hence allowing make content retrieval conditional

you can check see if new file timestamp updated old one, , download if file updated. download file, use r.content:

image = r.content 

here working code:

import requests import time oldtime = '' in xrange(100):     r = requests.get(url='http://upload.wikimedia.org/wikipedia/en/a/a9/example.jpg',stream=true)     newtime = r.headers['last-modified']     if newtime != oldtime:         image = r.content         oldtime = newtime     # can put time.sleep() statement here, not necessary 

Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -