Python m3u8 ssl.CertificateError 问题

标签 python ssl manifest http-live-streaming m3u8

我正在尝试使用不匹配的 SSL 证书加载 HLS m3u8 list 文件。我正在为 Python 使用 m3u8 库。我的脚本如下:

#!/usr/bin/env python
from urllib import quote
import m3u8
import ssl

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

#try:
manifest = m3u8.load(input_file)
#except ssl.CertificateError:
#print "WARNING SSL Error!"
for playlist in manifest.playlists:
        print playlist.uri
        print playlist.stream_info.bandwidth

因此,当我使用我的链接运行它时,它会报告 ssl.CertificateError,因为 SSL 证书不正确,但我想跳过此检查,在这种情况下只打印 SSL 警告并继续执行脚本。这可能吗?我该怎么做?

我已将我的脚本更改为:

#!/usr/bin/env python
from urllib import quote
import m3u8
import requests

input_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

url = requests.get(input_file, verify = False)

manifest = m3u8.load(url)

for playlist in manifest.playlists:
        print playlist.uri
        print playlist.stream_info.bandwidth

但现在我得到以下错误:

/usr/local/lib/python2.7/dist-packages/requests/packages/urllib3/connectionpool.py:852: InsecureRequestWarning: Unverified HTTPS request is being made. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  InsecureRequestWarning)
Traceback (most recent call last):
  File "./open.sh", line 10, in <module>
    manifest = m3u8.load(url)
  File "/usr/local/lib/python2.7/dist-packages/m3u8/__init__.py", line 44, in load
    if is_url(uri):
  File "/usr/local/lib/python2.7/dist-packages/m3u8/parser.py", line 337, in is_url
    return re.match(r'https?://', uri) is not None
  File "/usr/lib/python2.7/re.py", line 141, in match
    return _compile(pattern, flags).match(string)
TypeError: expected string or buffer

最佳答案

此代码似乎有效。如果 SSL 证书未获批准,它还会显示存在 SSL 证书错误。

#!/usr/bin/env python
from urllib import quote
import m3u8
import requests
import ssl

in_file = quote(raw_input("Please enter the input file path: "), safe=':''/')

try:
        url = requests.get(in_file)
except requests.exceptions.SSLError:
        url = requests.get(in_file, verify = False)
        print "Warning: SSL Certificate Error!!!"
        print
        pass

manifest = m3u8.loads(url.text)

for playlist in manifest.playlists:
        print playlist.uri
        print playlist.stream_info.bandwidth

关于Python m3u8 ssl.CertificateError 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43066505/

相关文章:

python - 私有(private)变量和类局部引用

Python C 扩展——维护状态

Python 请求给出 SSL 未知协议(protocol)

ssl - 如何解密使用 Diffie Hellman 加密的 SSH .pcap 文件。使用公钥和私钥

ssl - 没有 Internet 连接的 WiFi 连接设备的最佳实践

c# - 无法在我的动态编译的 C# 应用程序中包含 list

python - 如何在二维数组中使用 numpy.searchsorted 进行矢量化

python - 为什么我不能导入 pygtk?

ipad - ASP.NET list 文件脚本未刷新

java - 简单的 Swing 应用程序。 jar 文件在我的电脑上运行,但在其他电脑上不运行