1
from base64 import b64encode
3
from ..packages import six
6
ACCEPT_ENCODING = 'gzip,deflate'
9
def make_headers(keep_alive=None, accept_encoding=None, user_agent=None,
10
basic_auth=None, proxy_basic_auth=None):
12
Shortcuts for generating request headers.
15
If ``True``, adds 'connection: keep-alive' header.
17
:param accept_encoding:
18
Can be a boolean, list, or string.
19
``True`` translates to 'gzip,deflate'.
20
List will get joined by comma.
21
String will be used as provided.
24
String representing the user-agent you want, such as
28
Colon-separated username:password string for 'authorization: basic ...'
31
:param proxy_basic_auth:
32
Colon-separated username:password string for 'proxy-authorization: basic ...'
37
>>> make_headers(keep_alive=True, user_agent="Batman/1.0")
38
{'connection': 'keep-alive', 'user-agent': 'Batman/1.0'}
39
>>> make_headers(accept_encoding=True)
40
{'accept-encoding': 'gzip,deflate'}
44
if isinstance(accept_encoding, str):
46
elif isinstance(accept_encoding, list):
47
accept_encoding = ','.join(accept_encoding)
49
accept_encoding = ACCEPT_ENCODING
50
headers['accept-encoding'] = accept_encoding
53
headers['user-agent'] = user_agent
56
headers['connection'] = 'keep-alive'
59
headers['authorization'] = 'Basic ' + \
60
b64encode(six.b(basic_auth)).decode('utf-8')
63
headers['proxy-authorization'] = 'Basic ' + \
64
b64encode(six.b(proxy_basic_auth)).decode('utf-8')