跟踪重定向使用http.clien

2024-04-26 17:17:27 发布

您现在位置:Python中文网/ 问答频道 /正文

有没有办法使用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

^{pr2}$

Tags: 模块pathclienthttphosturldataresponse