Apr-25-2022, 05:49 PM
I am attempting to update a python script from 2018 to handle some new cases and have encountered a problem I have been unable to solve.
This script is to pass an internet media stream to a Chromecast device for playback. One such media is an iHeart radio stream. When this stream is opened it returns a redirection request, but my attempts to handle it result in additional redirect requests. As a diagnostic I used the following curl command to retrieve the first few seconds of the stream and view how it did so:
curl --trace kcsjtrace.log -L -m 3 http://stream.revma.ihrhls.com/zc4814 >kcsj.out 2>kcsjerr.log
Here is the code I am attempting to handle this with:
Any help appreciated.
Dave
This script is to pass an internet media stream to a Chromecast device for playback. One such media is an iHeart radio stream. When this stream is opened it returns a redirection request, but my attempts to handle it result in additional redirect requests. As a diagnostic I used the following curl command to retrieve the first few seconds of the stream and view how it did so:
curl --trace kcsjtrace.log -L -m 3 http://stream.revma.ihrhls.com/zc4814 >kcsj.out 2>kcsjerr.log
Here is the code I am attempting to handle this with:
resp = get_resp(url, "HEAD", {}) if resp.status == 404: resp = get_resp(url, "GET", {}) if resp.status != 200: redirect_codes = [ 301, 302, 303, 307, 308 ] if resp.status in redirect_codes: redirects = 0 req_header = {} while resp.status in redirect_codes: redirects += 1 if redirects > 9: sys.exit("HTTP Error: Too many redirects") headers = resp.getheaders() for header in headers: if len(header) > 1: if header[0].lower() == "location": redirect_location = header[1] req_header["Host"] = (urlparse.urlparse(redirect_location)).netloc if header[0].lower() == "set-cookie": req_header["Accept"] = "*/*" req_header["User-Agent"] = "python/2.7.18" req_header["Cookie"] = (header[1].split(";"))[0] if redirect_location.startswith("http") is False: redirect_location = get_full_url(url, redirect_location) print "Redirecting to " + redirect_location resp = get_resp(redirect_location, "HEAD", req_header) if resp.status == 404: resp = get_resp(url, "GET", req_header) if resp.status != 200: sys.exit("HTTP error:" + str(resp.status) + " - " + resp.reason) else: sys.exit("HTTP error:" + str(resp.status) + " - " + resp.reason) print "Found HTTP resource" def get_resp(url, type="HEAD", header=[]): url_parsed = urlparse.urlparse(url) scheme = url_parsed.scheme host = url_parsed.netloc path = url.split(host, 1)[-1] conn = None if scheme == "https": conn = httplib.HTTPSConnection(host) else: conn = httplib.HTTPConnection(host) conn.request(type, path, "", header) resp = conn.getresponse() return resp
Any help appreciated.
Dave