Python m3u8 ssl.CertificateError

时间:2017-03-28 10:01:38

标签: python ssl manifest hls m3u8

我正在尝试加载具有无与伦比的SSL证书的HLS m3u8清单文件。我正在使用m3u8库来进行Python。我的脚本如下:

#!/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

1 个答案:

答案 0 :(得分:0)

此代码似乎有效。它还表明,如果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