httplib2 支持使用例子(python3支持)

httplib2是一个强大的Python库,用于处理HTTP请求。它支持简单的检索、认证、缓存控制、表单提交和处理cookies。然而,它在cookie和代理服务器支持方面存在一些问题。本文通过实例详细介绍了如何使用httplib2进行HTTP请求,包括设置认证、处理Cache-Control头、提交表单以及管理cookies。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

个人觉得非常不错,就是对cookie支持存在问题,对代理服务器支持上也有问题


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 request will be made to the server. You can use the Cache-Control: header to change the caches behavior and in this example the second request adds the Cache-Control: header with a value of 'no-cache' which tells the library that the cached copy must not be used when handling this request.

Forms

Below is an example of using httplib2 to submit a form. Note that we have to use the urlencode() function from urllib.parse to encode the data before using it as the POST body.

from httplib2 import Http
from urllib.parse import urlencode
h = Http()
data = {"name": "Joe", "comment": "A test comment"}
resp, content = h.request("http://bitworking.org/news/223/Meet-Ares", "POST", urlencode(data))


Cookies

When automating something, you often need to "login" to maintain some sort of session/state with the server. Sometimes this is achieved with form-based authentication and cookies. You post a form to the server, and it responds with a cookie in the incoming HTTP header. You need to pass this cookie back to the server in subsequent requests to maintain state or to keep a session alive.

Here is an example of how to deal with cookies when doing your HTTP Post.

First, lets import the modules we will use:

import urllib.parse
import httplib2

Now, lets define the data we will need. In this case, we are doing a form post with 2 fields representing a username and a password.

url = 'http://www.example.com/login'   
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}


Now we can send the HTTP request:

http = httplib2.Http()
response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))


At this point, our "response" variable contains a dictionary of HTTP header fields that were returned by the server. If a cookie was returned, you would see a "set-cookie" field containing the cookie value. We want to take this value and put it into the outgoing HTTP header for our subsequent requests:

headers['Cookie'] = response['set-cookie']

Now we can send a request using this header and it will contain the cookie, so the server can recognize us.

So... here is the whole thing in a script. We login to a site and then make another request using the cookie we received:

#!/usr/bin/python3

import urllib.parse
import httplib2

http = httplib2.Http()

url = 'http://www.example.com/login'   
body = {'USERNAME': 'foo', 'PASSWORD': 'bar'}
headers = {'Content-type': 'application/x-www-form-urlencoded'}
response, content = http.request(url, 'POST', headers=headers, body=urllib.parse.urlencode(body))

headers = {'Cookie': response['set-cookie']}

url = 'http://www.example.com/home'   
response, content = http.request(url, 'GET', headers=headers)


Proxies

Proxy support is unavailable until the third-party socks module is ported to Python 3.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值