[Python] Follow redirect using http.client

Discussão em 'Python' iniciado por Stack, Março 23, 2025.

  1. Stack

    Stack Membro Participativo

    Is there a way to automatically follow redirects using the http.client module:

    def grab_url(host, path = '/'):
    class Data: pass
    result = Data()
    try:
    con = http.client.HTTPConnection(host)
    con.request('GET', path)
    response = con.getresponse()
    if response.status == 200:
    result.content = response.read().decode('utf-8')
    result.headers = response.getheaders()
    catch Exception as e:
    print(e)
    return result


    The above works as long as the request returns a http response of 200, but I don't know how to handle redirects like 301?

    Using pyCurl I would simply set the FOLLOWLOCATION to True:

    def grab_url(host, path = '/'):
    buffer = BytesIO()
    c = pyCurl.Curl()
    c.setopt(c.FOLLOWLOCATION, True)
    c.setopt(c.URL, host + path)
    c.setopt(c.WRITEDATA, buffer)
    c.perform()
    status = c.getinfo(c.RESPONSE_CODE)
    if status == 200:
    return buffer.getvalue().decode('iso-8859-1')

    Continue reading...

Compartilhe esta Página