python - 验证 url 是否是没有 urllib.request.urlopen 的视频原始文件链接 [英] python - Verify if a url is a video raw file link without urllib.request.urlopen

查看:35
本文介绍了python - 验证 url 是否是没有 urllib.request.urlopen 的视频原始文件链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想验证一个url是否是视频原始文件链接,例如:

http://hidden_​​path/video_name.mp4

以下是我当前的代码:

def is_video(url):r = 无尝试:r = urllib.request.urlopen(urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}))除了:返回错误content_type = r.getheader("内容类型")如果 re.match("video*", content_type):返回真返回错误

如果视频url是大视频,此代码会出现问题,可能会导致服务器超时错误.

有没有更好的方法?

解决方案

如果你只是想检查头部的 Content-Type 你可以发送一个 HEAD 请求而不是 GET.

一旦您从 HEAD 请求中获得响应,您就可以在 Content-Type 标头中检查 video 如上所述.>

示例:

<预><代码>>>>req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})>>>r = urllib.request.urlopen(req)>>>r.getheader('内容类型')'视频/mp4'

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

http://hidden_path/video_name.mp4

Below is my current code:

def is_video(url):
    r = None
    try:
        r = urllib.request.urlopen(urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}))
    except:
        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?

解决方案

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.

Example:

>>> req = urllib.request.Request(url, method='HEAD', headers={'User-Agent': 'Mozilla/5.0'})
>>> r = urllib.request.urlopen(req)
>>> r.getheader('Content-Type')
'video/mp4'

这篇关于python - 验证 url 是否是没有 urllib.request.urlopen 的视频原始文件链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆