【源码阅读】Requests
调用实例:
1 import requests 2 r = requests.get('https://www.python.org') 3 r.status_code 4 r.content 5 ....
requests包目录:
Requests/ —— packages/ —— __init__.py —— _internal_utils.py —— adapters.py —— api.py #api —— auth.py —— certs.py —— compat.py —— cookies.py —— exceotions.py —— hooks.py —— models.py #类 —— sessions.py —— status_codes.py #http状态嘛 —— structures.py —— structures.py —— utils.py
__init__.py中引入了各大模块,requests.get相当于使用的api.get
from .api import request, get, head, post, patch, put, delete, options
get方法:
1 def get(url, params=None, **kwargs): 2 3 kwargs.setdefault('allow_redirects', True) 4 return request('get', url, params=params, **kwargs)
get, head, post, patch, put, delete, options都指向了api.requests方法。区别在于是否初始化了'allow_redirects'的值
api.requests:
def request(method, url, **kwargs): """ Usage:: >>> import requests >>> req = requests.request('GET', 'http://httpbin.org/get') <Response [200]> """ with sessions.Session() as session: return session.request(method=method, url=url, **kwargs)#调用了session.requests~
session.requests:
1 def request(self, method, url, 2 params=None, 3 data=None, 4 headers=None, 5 cookies=None, 6 files=None, 7 auth=None, 8 timeout=None, 9 allow_redirects=True, 10 proxies=None, 11 hooks=None, 12 stream=None, 13 verify=None, 14 cert=None, 15 json=None): 16 17 # Create the Request. 18 req = Request( 19 method = method.upper(), 20 url = url, 21 headers = headers, 22 files = files, 23 data = data or {}, 24 json = json, 25 params = params or {}, 26 auth = auth, 27 cookies = cookies, 28 hooks = hooks, 29 )# 初始化类 30 prep = self.prepare_request(req) #加工Request类,return:PreparedRequest 31 32 proxies = proxies or {} #代理 33 34 settings = self.merge_environment_settings( 35 prep.url, proxies, stream, verify, cert 36 ) 37 38 # Send the request. 39 send_kwargs = { 40 'timeout': timeout, 41 'allow_redirects': allow_redirects, 42 } 43 send_kwargs.update(settings) 44 resp = self.send(prep, **send_kwargs) #发送请求 45 46 return resp
send:
1 def send(self, request, **kwargs): 2 3 kwargs.setdefault('stream', self.stream) 4 kwargs.setdefault('verify', self.verify) 5 kwargs.setdefault('cert', self.cert) 6 kwargs.setdefault('proxies', self.proxies) 7 8 if isinstance(request, Request): 9 raise ValueError('You can only send PreparedRequests.') 10 11 allow_redirects = kwargs.pop('allow_redirects', True) 12 stream = kwargs.get('stream') 13 hooks = request.hooks 14 15 if allow_redirects: 16 checked_urls = set() 17 while request.url in self.redirect_cache: 18 checked_urls.add(request.url) 19 new_url = self.redirect_cache.get(request.url) 20 if new_url in checked_urls: 21 break 22 request.url = new_url 23 24 adapter = self.get_adapter(url=request.url) 25 26 start = datetime.utcnow() 27 28 r = adapter.send(request, **kwargs) 29 30 r.elapsed = datetime.utcnow() - start 31 32 r = dispatch_hook('response', hooks, r, **kwargs) 33 34 if r.history: 35 36 for resp in r.history: 37 extract_cookies_to_jar(self.cookies, resp.request, resp.raw) 38 39 extract_cookies_to_jar(self.cookies, request, r.raw) 40 41 gen = self.resolve_redirects(r, request, **kwargs) 42 43 history = [resp for resp in gen] if allow_redirects else [] 44 45 if history: 46 history.insert(0, r) 47 r = history.pop() 48 r.history = history 49 50 if not stream: 51 r.content 52 53 return r