默认请求头
1 | GET / HTTP/1.1 |
发送请求
GET方法
1 | import requests |
POST方法
1 | r = requests.post(url) |
其它方法
1 | r = requests.put(url) |
将请求方法放在参数中
1 | import requests |
传递参数
GET传参
1 | payload = {'key1': 'value1', 'key2': 'value2'} |
POST传参
1 | payload = {'key1': 'value1', 'key2': 'value2'} |
JSON传参
1 | import json |
设置cookie
1 | 'http://httpbin.org/cookies' url = |
设置Headers
1 | import json |
上传
直接写入
1 | f = {"file": ("upload.php","<?php phpinfo() ?>")} |
上传文件
{‘name’: file-like-objects}
{‘name’: (‘filename’, fileobj)}
1 | files = {'file': open('report.xls', 'rb')} |
也可以明确设置filename, content_type and headers1
2
3files = {'file': ('report.xls', open('report.xls', 'rb'),
'application/vnd.ms-excel', {'Expires': '0'})}
r = requests.post(url, files=files)
上传多个文件
1 | multiple_files = [('images', ('foo.png', open('foo.png', 'rb'), 'image/png')), |
流上传
1 | with open('massive-body', 'rb') as f: |
Response
1 | r.status_code |
返回体
获取unicode字符串,会自动根据响应头部的字符编码(r.encoding)进行解码,当然也可以自己设定r.encoding1
2
3'https://github.com/timeline.json') r = requests.get(
r.text
u'{"message":"Hello there, wayfaring stranger...
获取bytes字符串,会自动解码gzip和deflate数据1
2 r.content
'{"message":"Hello there, wayfaring stranger. ..
要存储web图片,可以1
2
3from PIL import Image
from StringIO import StringIO
i = Image.open(StringIO(r.content))
可以解码json对象1
2 r.json()
{u'documentation_url': u'https://developer...
返回raw response,需要在requests请求中将stream设为True1
2
3
4
5'https://github.com/timeline.json', stream=True) r = requests.get(
r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x101194810>
10) r.raw.read(
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x03'
如果不想一次性处理全部的数据,可以1
2
3
4
5tarball_url = 'https://github.com/kennethreitz/requests/tarball/master'
r = requests.get(tarball_url, stream=True)
if int(r.headers['content-length']) < TOO_LONG:
content = r.content
...
也可以迭代的处理数据1
2
3with open(filename, 'wb') as fd:
for chunk in r.iter_content(chunk_size):
fd.write(chunk)
或者1
2
3
4
5
6
7import json
import requests
r = requests.get('http://httpbin.org/stream/20', stream=True)
for line in r.iter_lines():
# filter out keep-alive new lines
if line:
print(json.loads(line))
获取响应代码
1 | 'http://httpbin.org/get') r = requests.get( |
获取响应headers
1 | r.headers |
获取发送的headers
1 | r.request.headers |
Cookie
获取cookie,返回CookieJar对象1
2
3'http://www.baidu.com' url =
r = requests.get(url)
r.cookies
将CookieJar转为字典
1 | requests.utils.dict_from_cookiejar(r.cookies) |
将字典转为CookieJar
1 | requests.utils.cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True) |
如果需要在会话中保留cookie,需要用到后面要说的Session。
Redirection and History
追踪重定向
1 | 'http://github.com') r = requests.get( |
Session
要在会话中保留状态,可以使用request.Session()。
Session对象在请求时允许你保留一定的参数和自动设置cookie
使用get,post等方法
1 | s = requests.Session() |
自己设置headers,cookies
1 | s = requests.Session() |
预设Request
可以在发送request前做些额外的设定1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21from requests import Request, Session
s = Session()
req = Request('GET', url,
data=data,
headers=header
)
prepped = req.prepare()
# do something with prepped.body
# do something with prepped.headers
resp = s.send(prepped,
stream=stream,
verify=verify,
proxies=proxies,
cert=cert,
timeout=timeout
)
print(resp.status_code)
验证
Basic Authentication
1 | from requests.auth import HTTPBasicAuth |
因为HTTP Basic Auth很常用,所以也可以直接验证1
2'https://api.github.com/user', auth=('user', 'pass')) requests.get(
<Response [200]>
Digest Authentication
1 | from requests.auth import HTTPDigestAuth |
OAuth 1 Authentication
1 | import requests |
也可以使用自己写的验证类
比如某个web服务接受将X-Pizza报头设置成密码的验证,可以这样写验证类1
2
3
4
5
6
7
8
9
10from requests.auth import AuthBase
class PizzaAuth(AuthBase):
"""Attaches HTTP Pizza Authentication to the given Request object."""
def __init__(self, username):
# setup any auth-related data here
self.username = username
def __call__(self, r):
# modify and return the request
r.headers['X-Pizza'] = self.username
return r
使用1
2'http://pizzabin.org/admin', auth=PizzaAuth('kenneth')) requests.get(
<Response [200]>
SSL证书验证
检查主机的ssl证书1
2
3'https://kennethreitz.com', verify=True) requests.get(
raise ConnectionError(e)
ConnectionError: HTTPSConnectionPool(host='kennethreitz.com', port=443): Max retries exceeded with url: / (Caused by <class 'socket.error'>: [Errno 10061] )
github是有的1
2'https://github.com', verify=True) requests.get(
<Response [200]>
如果你设置验证设置为False,也可以忽略验证SSL证书。
可以读取验证文件1
'https://kennethreitz.com', cert=('/path/server.crt', '/path/key')) requests.get(
代理
使用
1 | import requests |
socket出错
1 | pip install pysocks |
设置环境变量
1 | $ export HTTP_PROXY="http://10.10.1.10:3128" |
验证
1 | proxies = { |