使用http.client关注重定向

时间:2018-04-15 19:25:19

标签: python python-3.x redirect http.client

有没有办法使用http.client模块自动跟踪重定向:

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

只要请求返回200的http响应,上述方法就有效,但我不知道如何处理301等重定向?

使用pyCurl我只需将FOLLOWLOCATION设置为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')

0 个答案:

没有答案