我正在尝试使用不匹配的 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/