Python Forum
problem handling redirection
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
problem handling redirection
#1
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
Larz60+ write Apr-25-2022, 05:51 PM:
Please post all code, output and errors (it it's entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Fixed for you this time. Please use BBCode tags on future posts.
Reply
#2
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:

Output:
dhdurgee@z560:~/Downloads$ stream2chromecast.py -playurl https://stream.revma.ihrhls.com/zc4814 -device 172.16.80.227 ----------------------------------------- Stream2Chromecast version:0.6.3 Copyright (C) 2014-2016 Pat Carter GNU General Public License v3.0 https://www.gnu.org/licenses/gpl-3.0.html ----------------------------------------- Redirecting to https://n3ea-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsNjQAapiQN1Ky2XLfAw Redirecting to https://n3ea-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsOIwARCy_2PF5A_ISbA Redirecting to https://n31a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsOoAAcDdphleW1z0pqg Redirecting to https://n14a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsPPcAAAsr-P5LZo3vWg Redirecting to https://n20a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsP3kArlhwnxm6u9H7gQ Redirecting to https://n14a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsQksAsvp6mulkB0CtgQ Redirecting to https://n14a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsREgA7LW-9ioBdWoCzg Redirecting to https://n20a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsRjsAbDItCd2QdYvNRw Redirecting to https://n31a-e2.revma.ihrhls.com/zc4814?rj-ttl=5&rj-tok=AAABgGjsSDQAYjSMnQINfwNJMg HTTP Error: Too many redirects dhdurgee@z560:~/Downloads$
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
.zip   stream2chromecast.zip (Size: 40.67 KB / Downloads: 82)
Reply
#3
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
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020