资源简介
将该文件放在库目录下的requests\packages\urllib3下,代替原来的即可
代码片段和文件信息
from __future__ import absolute_import
import email.utils
import mimetypes
from .packages import six
def guess_content_type(filename default=‘application/octet-stream‘):
“““
Guess the “Content-Type“ of a file.
:param filename:
The filename to guess the “Content-Type“ of using :mod:‘mimetypes‘.
:param default:
If no “Content-Type“ can be guessed default to ‘default‘.
“““
if filename:
return mimetypes.guess_type(filename)[0] or default
return default
def format_header_param(name value):
“““
Helper function to format and quote a single header parameter.
Particularly useful for header parameters which might contain
non-ASCII values like file names. This follows RFC 2231 as
suggested by RFC 2388 Section 4.4.
:param name:
The name of the parameter a string expected to be ASCII only.
:param value:
The value of the parameter provided as a unicode string.
“““
if not any(ch in value for ch in ‘“\\\r\n‘):
result = ‘%s=“%s“‘ % (name value)
try:
result.encode(‘ascii‘)
except UnicodeEncodeError:
pass
else:
return result
if not six.PY3: # Python 2:
value = value.encode(‘utf-8‘)
value = email.utils.encode_rfc2231(value ‘utf-8‘)
# value = ‘%s*=%s‘ % (name value)
value = ‘%s=“%s“‘ % (name value.decode(‘utf8‘))
return value
class RequestField(object):
“““
A data container for request body parameters.
:param name:
The name of this request field.
:param data:
The data/value body.
:param filename:
An optional filename of the request field.
:param headers:
An optional dict-like object of headers to initially use for the field.
“““
def __init__(self name data filename=None headers=None):
self._name = name
self._filename = filename
self.data = data
self.headers = {}
if headers:
self.headers = dict(headers)
@classmethod
def from_tuples(cls fieldname value):
“““
A :class:‘~urllib3.fields.RequestField‘ factory from old-style tuple parameters.
Supports constructing :class:‘~urllib3.fields.RequestField‘ from
parameter of key/value strings AND key/filetuple. A filetuple is a
(filename data MIME type) tuple where the MIME type is optional.
For example::
‘foo‘: ‘bar‘
‘fakefile‘: (‘foofile.txt‘ ‘contents of foofile‘)
‘realfile‘: (‘barfile.txt‘ open(‘realfile‘).read())
‘typedfile‘: (‘bazfile.bin‘ open(‘bazfile‘).read() ‘image/jpeg‘)
‘nonamefile‘: ‘contents of nonamefile field‘
Field names and filenames must be unicode.
“““
if isinstance(value tuple):
if len(value) == 3:
filename data content_type = value
else:
- 上一篇:基于LSTM的RNN网络人体骨骼关节点检测
- 下一篇:从百度地图POI
评论
共有 条评论