check if a URL to an image is up and exists in Python

依然范特西╮ 提交于 2020-07-18 20:27:32

问题


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


回答1:


This is one way that is quick:

It doesn't really verify that is really an image file, it just guesses based on file extention and then checks that 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 would 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)



回答2:


This is the best approach working 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



回答3:


Take a look into imghdr

Here is some example code:

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

This should return 'png'. If it is not an image it will return None

Hope that helps!

-Blake




回答4:


You can read the header of the http request, it contains some meta-data like the content-type.

On 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 info like the size of the image and etc. The good news about this is that it doesn't download the image. It could fail if the header says that it is an image and it is not, but you can still do a last check and download the image if it pass the first filter.



来源:https://stackoverflow.com/questions/10543940/check-if-a-url-to-an-image-is-up-and-exists-in-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!