forked from DevAlone/proxy_py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
http_client.py
99 lines (77 loc) · 2.68 KB
/
http_client.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from proxy_py import settings
from fake_useragent import UserAgent
from aiosocks.connector import ProxyConnector, ProxyClientRequest
import aiohttp
import json
class HttpClientResult:
text = None
aiohttp_response = None
@staticmethod
async def make(aiohttp_response):
obj = HttpClientResult()
obj.aiohttp_response = aiohttp_response
obj.text = await obj.aiohttp_response.text()
return obj
def as_text(self):
return self.text
def as_json(self):
return json.loads(self.text)
# TODO: complete cookies saving
class HttpClient:
"""
Simple class for making http requests,
user-agent is set to random one in constructor
"""
_aiohttp_connector = None
def __init__(self):
self.user_agent = UserAgent().random
self.timeout = 60
if HttpClient._aiohttp_connector is None:
HttpClient._aiohttp_connector = ProxyConnector(
remote_resolve=True,
limit=settings.NUMBER_OF_SIMULTANEOUS_REQUESTS,
limit_per_host=settings.NUMBER_OF_SIMULTANEOUS_REQUESTS_PER_HOST,
)
self.proxy_address = None
async def get(self, url):
"""
send HTTP GET request
:param url:
:return:
"""
return await self.request('GET', url, None)
async def post(self, url, data):
"""
send HTTP POST request
:param url:
:param data:
:return:
"""
return await self.request('POST', url, data)
async def request(self, method, url, data) -> HttpClientResult:
headers = {
'User-Agent': self.user_agent,
}
async with aiohttp.ClientSession(connector=HttpClient._aiohttp_connector,
connector_owner=False,
request_class=ProxyClientRequest
) as session:
async with session.request(method,
url=url,
data=data,
proxy=self.proxy_address,
timeout=self.timeout,
headers=headers) as response:
return await HttpClientResult.make(response)
@staticmethod
async def clean():
HttpClient._aiohttp_connector.close()
async def get_text(url):
"""
fast method for sending get response without creating extra objects
:param url:
:return:
"""
return (await HttpClient().get(url)).as_text()
async def get_json(url):
return (await HttpClient().get(url)).as_json()