对于 python 客户端获取 URL wrt 缓存和重定向应该做什么?
我正在用 Python 2.7.2 编写一个客户端,使用 httplib 从网络获取 url:
def connectHttp(url, sub_url)
conn = httplib.HTTPConnection(url)
try:
conn.request("GET", "/" + sub_url)
except Exception as ex:
conn.close()
logMessage('Connection problems:')
logMessage(str(ex))
logMessage('Exception message:')
logMessage(ex.message)
logMessage('-------')
return (503, "")
response = conn.getresponse()
status = response.status
data = response.read()
conn.close()
return (status, data)
我的问题是我不知道如何处理重定向,我什至不确定 httplib 是否有一个选项。另一件事是有一种简单的方法可以防止在服务器上进行缓存(更正:代理)(例如,通过添加带有随机数据的虚拟查询字符串)
非常感谢......
I am writing a client in Python 2.7.2 using httplib to fetch urls from the web:
def connectHttp(url, sub_url)
conn = httplib.HTTPConnection(url)
try:
conn.request("GET", "/" + sub_url)
except Exception as ex:
conn.close()
logMessage('Connection problems:')
logMessage(str(ex))
logMessage('Exception message:')
logMessage(ex.message)
logMessage('-------')
return (503, "")
response = conn.getresponse()
status = response.status
data = response.read()
conn.close()
return (status, data)
My problem is that I don't know how to handle redirects and I'm not even sure if httplib has an option for that. Another thing is there an easy way to prevent caching on the server (correction: proxy) (for ex, by adding a dummy query string with random data)
Much appreciated...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
httplib2 处理重定向。缓存控制是一个更大的话题;第一个问题是,尝试强制非缓存页面而不是将其留给服务器是否是一个好主意 - 如果作者包含缓存,我首先假设他们这样做是有原因的。
httplib2 handles redirects. Cache control is a much larger topic; the first question is whether it's a good idea to try to force non-cached pages rather than leaving it up to the server - if the author included caching, I'd start by assuming they did so for a reason.