Check if image url is updated in Python

I am making a website. I want to check from the server whether the link provided by the user is actually an image that exists.

+5
source share
4 answers

This is one of the quick ways:

It does not check if this is an image file, it only guesses based on the file extension, and then checks if the URL exists. If you really need to verify that the data returned from the URL is actually an image (for security reasons), then this solution will not work.

import mimetypes, urllib2

def is_url_image(url):    
    mimetype,encoding = mimetypes.guess_type(url)
    return (mimetype and mimetype.startswith('image'))

def check_url(url):
    """Returns True if the url returns a response code between 200-300,
       otherwise return False.
    """
    try:
        headers = {
            "Range": "bytes=0-10",
            "User-Agent": "MyTestAgent",
            "Accept": "*/*"
        }

        req = urllib2.Request(url, headers=headers)
        response = urllib2.urlopen(req)
        return response.code in range(200, 209)
    except Exception:
        return False

def is_image_and_ready(url):
    return is_url_image(url) and check_url(url)
+10
source

This is the best approach for my application, based also on previous comments:

def is_url_image(image_url):
   image_formats = ("image/png", "image/jpeg", "image/jpg")
   r = requests.head(image_url)
   if r.headers["content-type"] in image_formats:
      return True
   return False
+2
source

imghdr

:

import imghdr
import httplib
import cStringIO

conn = httplib.HTTPConnection('www.ovguide.com', timeout=60)
path = '/img/global/ovg_logo.png'
conn.request('GET', path)
r1 = conn.getresponse()

image_file_obj = cStringIO.StringIO(r1.read())
what_type = imghdr.what(image_file_obj)

print what_type

'png'. , None

, !

-Blake

+1

http-, , .

python 3:

from urllib.request import urlopen
image_formats = ("image/png", "image/jpeg", "image/gif")
url = "http://localhost/img.png"
site = urlopen(url)
meta = site.info()  # get header of the http request
if meta["content-type"] in image_formats:  # check if the content-type is a image
    print("it is an image")

You can also get other information, such as image size, etc. The good news is that it does not load an image. It may fail if the title says that it is an image, but it is not, but you can still do the last check and load the image if it passes the first filter.

0
source

All Articles