我正在做一个网站。我想从服务器检查用户提交的链接是否实际上是存在的图像。
4 回答
这是适用于我的应用程序的最佳方法,也是基于以前的评论:
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
这是一种快速的方法:
它并没有真正验证这真的是一个图像文件,它只是根据文件扩展名进行猜测,然后检查 url 是否存在。如果您确实需要验证从 url 返回的数据实际上是图像(出于安全原因),那么此解决方案将不起作用。
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)
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.
看看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
希望有帮助!
-布莱克