version.py代码如下:
__title__ = 'requests'
__description__ = 'Python HTTP for Humans.'
__url__ = 'http://python-requests.org'
__version__ = '2.21.0'
__build__ = 0x022100
__author__ = 'Kenneth Reitz'
__author_email__ = 'me@kennethreitz.org'
__license__ = 'Apache 2.0'
__copyright__ = 'Copyright 2018 Kenneth Reitz'
__cake__ = u'\u2728 \U0001f370 \u2728'
没啥好说的定义了Request类的基本信息
_internal_utils.py
代码如下:
# -*- coding: utf-8 -*-
"""
requests._internal_utils
~~~~~~~~~~~~~~
Provides utility functions that are consumed internally by Requests
which depend on extremely few external helpers (such as compat)
"""
from .compat import is_py2, builtin_str, str
def to_native_string(string, encoding='ascii'):
"""Given a string object, regardless of type, returns a representation of
that string in the native string type, encoding and decoding where
necessary. This assumes ASCII unless told otherwise.
"""
if isinstance(string, builtin_str):
out = string
else:
if is_py2:
out = string.encode(encoding)
else:
out = string.decode(encoding)
return out
def unicode_is_ascii(u_string):
"""Determine if unicode string only contains ASCII characters.
:param str u_string: unicode string to check. Must be unicode
and not Python 2 `str`.
:rtype: bool
"""
assert isinstance(u_string, str)
try:
u_string.encode('ascii')
return True
except UnicodeEncodeError:
return False
这段代码主要定义了两个函数:to_native_string 和 unicode_is_ascii。下面是对这两个函数的详细解释:
to_native_string(string, encoding=‘ascii’)
这个函数接受一个字符串作为输入,并尝试将其转换为其本地的字符串表示形式。这里的“本地”指的是Python的内置字符串类型。
encoding
参数对输入的字符串进行编码,转换为字节串。默认的编码是ASCII。encoding
参数对输入的字符串进行解码,转换为Unicode串。unicode_is_ascii(u_string)
这个函数接受一个Unicode字符串作为输入,并检查该字符串是否只包含ASCII字符。
UnicodeEncodeError
异常,这意味着字符串包含非ASCII字符,函数返回False。总结:这段代码提供了两种方式来处理和检查字符串:一种是将任何类型的字符串转换为本地字符串表示形式,另一种是检查一个Unicode串是否只包含ASCII字符。