-
Notifications
You must be signed in to change notification settings - Fork 1
/
RequestSender.py
64 lines (51 loc) · 2.32 KB
/
RequestSender.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# -*- coding: utf-8 -*-
# request sender
#Copyright (C) 2023 yamahubuki <[email protected]>
import datetime
import requests
import requests.exceptions
import traceback
import constants
import globalVars
from entities import Header, Request, Response, Traffic
from enumClasses import HeaderFieldType
from logging import getLogger
from requests.structures import CaseInsensitiveDict
log = getLogger("%s.%s" % (constants.LOG_PREFIX, "requestSender"))
class RequestSender:
@classmethod
def send(cls, request):
req = request.toRequests()
with requests.Session() as sess:
# 初期値いらないのでつぶす
sess.headers = CaseInsensitiveDict()
requested_at = datetime.datetime.now()
try:
res = sess.send(req, allow_redirects=False, timeout=10)
except requests.exceptions.Timeout as e:
return cls._errorResponse(_("タイムアウトが発生しました。"), request, requested_at, e)
except requests.exceptions.SSLError as e:
return cls._errorResponse(_("SSL通信の開始に失敗しました。"), request, requested_at, e)
except requests.exceptions.ConnectionError as e:
return cls._errorResponse(_("接続できませんでした。"), request, requested_at, e)
except requests.exceptions.TooManyRedirects as e:
return cls._errorResponse(_("リダイレクトの回数が上限を超えました。"), request, requested_at, e)
except requests.exceptions.RequestException as e: # 関連例外の親玉。ここには来ないでほしい。
return cls._errorResponse(_("エラーが発生しました。"), request, requested_at, e)
headers = []
for k,v in res.headers.items():
headers.append(Header.Header(k, HeaderFieldType.CONST, v))
try:
response = Response.Response(res.status_code, res.elapsed, headers, res.json(), res.reason)
except Exception as e:
response = Response.Response(res.status_code, res.elapsed, headers, res.text, res.reason)
traffic = Traffic.Traffic(request, response, requested_at)
globalVars.history.add(traffic)
return traffic.toTreeData()
@classmethod
def _errorResponse(cls, msg, request, requested_at, e):
log.error(traceback.format_exc())
response = Response.Response(None, None, [], str(e), msg)
traffic = Traffic.Traffic(request, response, requested_at)
globalVars.history.add(traffic)
return traffic.toTreeData()