没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
How to use httplib2 in Python 3
Updated Jul 13, 2009 by pilgrim@gmail.com
Simple Retrieval
import httplib2
h = httplib2.Http(".cache")
resp, content = h.request("http://example.org/", "GET")
The 'content' is the content retrieved from the URL. The content is already
decompressed or unzipped if necessary. The 'resp' contains all the response
headers.
Python 3 makes a distinction between
bytes and strings.
In httplib2, the response
headers are strings, but the content is bytes. If you want to turn the content into a
string, you need to
determine the character encoding,
then explicitly convert it to a
string. The exact algorithm for doing this depends on the media type; httplib2 can
not help you determine the character encoding.
Once you determine the character encoding, the rest is easy. For example, if you
determine that the encoding is UTF-8, you would say:
str_content = content.decode('utf-8')
Authentication
To PUT some content to a server that uses SSL and Basic authentication:
import httplib2
h = httplib2.Http(".cache")
h.add_credentials('name', 'password')
resp, content = h.request("https://example.org/chap/2",
"PUT", body="This is text",
headers={'content-type':'text/plain'} )
Cache-Control
Use the
Cache-Control:
header to control how the caching operates.
import httplib2
h = httplib2.Http(".cache")
resp, content = h.request("http://bitworking.org/")
...
resp, content = h.request("http://bitworking.org/",
headers={'cache-control':'no-cache'})
The first request will be cached and since this is a request to bitworking.org it will be
set to be cached for two hours, because that is how I have my server configured.
Any subsequent GET to that URI will return the value from the on-disk cache and no
资源评论
lll9p
- 粉丝: 0
- 资源: 2
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功