Le Duy Khanh Le Duy Khanh - 2 years ago 146
Python Question

python - Verify if a url is a video raw file link without urllib.request.urlopen

I want to verify if a url is video raw file link or not, for example:


Below is my current code:

def is_video(url):
r = None
r = urllib.request.urlopen(urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}))
return False
content_type = r.getheader("Content-Type")
if re.match("video*", content_type):
return True
return False

This code will have issue if the video url is a big video, and it may cause timeout error on server.

Are there any better approaches?

Answer Source

If you just want to check the Content-Type of the header you can send a HEAD request instead of the GET.

Once you have obtained the response from the HEAD request you can check for video in the Content-Type header as above.


>>> req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
>>> r = urllib.request.urlopen(req)
>>> r.getheader('Content-Type')
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download