-2

I am trying to load an HLS m3u8 manifest file with unmatched SSL certificate. I am using m3u8 library for Python. My script is the following:

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

So when I run it with my link it reports ssl.CertificateError because the SSL certificate is not correct, but I want to skip this check and only print an SSL warning in this case and continue with the execution of the script. Is this possible and how can I do it?

I have changed my script to:

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

But now I get the following error:

/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
Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26
  • You'll probably need to read the thing in first and then send it to your parser. See http://stackoverflow.com/questions/15445981/how-do-i-disable-the-security-certificate-check-in-python-requests – pvg Mar 28 '17 at 10:16
  • @pvg yes, I know how I can circumvent this error using urllib or requests libraries, but I want to use m3u8 library for that, and even though it is based on urllib, it does not support `verify = False`, I have already tried it. – Georgi Stoyanov Mar 28 '17 at 10:22
  • You have tried what? What is your question, then? The parser doesn't give you the control you need, there is no way you can continue after you have got that exception. Just read the data and _then_ pass it to the parser, what's the problem with that? – pvg Mar 28 '17 at 10:24
  • @pvg I have updated the main thread with your suggestion and the output of it. – Georgi Stoyanov Mar 28 '17 at 10:34
  • @pvg okay, I removed the "according to pvg", but it appears that my link which I was using is not working today, so I will wait till they fix it and then I will try again. Thanks for your help – Georgi Stoyanov Mar 28 '17 at 11:08

1 Answers1

0

This code seems to work. It also shows that there is a SSL certificate error in case that the SSL certificate is not approved.

#!/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
Georgi Stoyanov
  • 594
  • 1
  • 9
  • 26