Skip to content

Commit 888b315

Browse files
committed
Add vk_api.utils.enable_debug_mode
1 parent 2e60d09 commit 888b315

1 file changed

Lines changed: 40 additions & 0 deletions

File tree

vk_api/utils.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,3 +92,43 @@ def cookies_to_list(cookies):
9292
def set_cookies_from_list(cookie_jar, l):
9393
for cookie in l:
9494
cookie_jar.set_cookie(cookie_from_dict(cookie))
95+
96+
97+
def enable_debug_mode(vk_session):
98+
""" Включает режим отладки:
99+
- Вывод сообщений лога
100+
- Вывод http запросов
101+
102+
:param vk_session: объект VkApi
103+
"""
104+
105+
import logging
106+
import sys
107+
import time
108+
109+
import requests
110+
111+
class DebugHTTPAdapter(requests.adapters.HTTPAdapter):
112+
def send(self, request, **kwargs):
113+
start = time.time()
114+
response = super(DebugHTTPAdapter, self).send(request, **kwargs)
115+
end = time.time()
116+
117+
total = end - start
118+
119+
print(
120+
'{:0.2f} {} {} {}'.format(
121+
total,
122+
request.method,
123+
request.url,
124+
response.history
125+
)
126+
)
127+
128+
return response
129+
130+
vk_session.http.mount('http://', DebugHTTPAdapter())
131+
vk_session.http.mount('https://', DebugHTTPAdapter())
132+
133+
vk_session.logger.setLevel(logging.INFO)
134+
vk_session.logger.addHandler(logging.StreamHandler(sys.stdout))

0 commit comments

Comments
 (0)