![]() |
problem handling redirection - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Networking (https://python-forum.io/forum-12.html) +--- Thread: problem handling redirection (/thread-37035.html) |
problem handling redirection - dhdurgee - Apr-25-2022 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: 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 RE: problem handling redirection - dhdurgee - Apr-27-2022 I thank you for reformatting that section of code to make things more readable. I hit the "insert code snippet" icon but for some reason it didn't take. That section of the code is where the problem is located. It does not result in errors, but it does not accomplish the purpose of successfully handling the redirection. For those who want to see the complete code I have attached a zip file with it. The output when I attempt to run the script is: Running the curl line above will show that it knows how to handle the redirection, so my code is obviously missing something.Thank you for any assistance you can give me. Dave[attachment=1727] RE: problem handling redirection - dhdurgee - Apr-27-2022 I had a thought occur to me and tried it, which appears to solve the problem. I had found earlier iHeart does not support "HEAD", so I was conditionally doing a "GET" if that failed. I was doing the same with the redirection, but getting yet another redirection. I speculated that perhaps the redirection was a one-shot and that issuing a "HEAD" first used up that only chance, resulting in yet another redirection. Making the redirection use "GET" instead of "HEAD" appears to have resolved the issue. Hopefully this case is of some use to others experiencing a similar issue. Dave |