This repository has been archived by the owner on Mar 2, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathproxy_requests.py
338 lines (308 loc) · 11.3 KB
/
proxy_requests.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import requests
from random import randint
from re import findall
# rootVIII
# pycodestyle validated
# 2018-2020
class ProxyRequests:
def __init__(self, url):
self.url = url
self.sockets = []
self.rdata = {
'headers': {},
'json': {},
'status_code': 0,
'timeout': 3.0,
'errs': [
'ConnectTimeout',
'ProxyError',
'SSLError',
'ReadTimeout',
'ConnectionError',
'ConnectTimeoutError'
]
}
self.empty_warn = 'Proxy Pool has been emptied'
self._acquire_sockets()
def _acquire_sockets(self):
r = requests.get('https://www.sslproxies.org/')
matches = findall(r"<td>\d+\.\d+\.\d+\.\d+</td><td>\d+</td>", r.text)
revised = [m.replace('<td>', '') for m in matches]
self.sockets = [s[:-5].replace('</td>', ':') for s in revised]
def _set_request_data(self, req, socket):
self.rdata['request'] = req.text
self.rdata['headers'] = req.headers
self.rdata['status_code'] = req.status_code
self.rdata['url'] = req.url
self.rdata['raw'] = req.content
self.rdata['proxy'] = socket
try:
self.rdata['json'] = req.json()
except Exception as err:
self.rdata['json'] = {type(err).__name__: str(err)}
def _rand_sock(self):
return randint(0, len(self.sockets) - 1)
def _is_err(self, err):
if type(err).__name__ not in self.rdata['errs']:
raise err
def _limit_succeeded(self):
raise Exception(self.empty_warn)
def get(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.get(
self.url,
timeout=self.rdata['timeout'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.get()
else:
self._limit_succeeded()
def get_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.get(
self.url,
timeout=self.rdata['timeout'],
proxies=proxies,
headers=self.rdata['headers'])
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.get_with_headers()
else:
self._limit_succeeded()
def post(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
json=data,
timeout=self.rdata['timeout'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post(data)
else:
self._limit_succeeded()
def post_with_headers(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
json=data,
timeout=self.rdata['timeout'],
headers=self.rdata['headers'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_with_headers(data)
else:
self._limit_succeeded()
def post_file(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
proxies=proxies,
timeout=self.rdata['timeout'],
files={'upload_file': open(self.rdata['file'], 'rb')})
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_file()
else:
self._limit_succeeded()
def post_file_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
files={'upload_file': open(self.rdata['file'], 'rb')},
timeout=self.rdata['timeout'],
headers=self.rdata['headers'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_file_with_headers()
else:
self._limit_succeeded()
def get_headers(self):
return self.rdata['headers']
def set_headers(self, outgoing_headers):
self.rdata['headers'] = outgoing_headers
def set_file(self, outgoing_file):
self.rdata['file'] = outgoing_file
def get_status_code(self):
return self.rdata['status_code']
def get_proxy_used(self):
return self.rdata['proxy']
def get_raw(self):
return self.rdata['raw']
def get_json(self):
return self.rdata['json']
def get_url(self):
return self.rdata['url']
def __str__(self):
return str(self.rdata['request'])
class ProxyRequestsBasicAuth(ProxyRequests):
def __init__(self, url, username, password):
super().__init__(url)
self.username = username
self.password = password
def get(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.get(
self.url,
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.get()
else:
self._limit_succeeded()
def get_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.get(
self.url,
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
proxies=proxies,
headers=self.rdata['headers'])
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.get_with_headers()
else:
self._limit_succeeded()
def post(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
json=data,
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post(data)
else:
self._limit_succeeded()
def post_with_headers(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
json=data,
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
headers=self.rdata['headers'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_with_headers(data)
else:
self._limit_succeeded()
def post_file(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
files={'upload_file': open(self.rdata['file'], 'rb')},
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_file()
else:
self._limit_succeeded()
def post_file_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(self._rand_sock())
proxies = {
'http': 'http://' + current_socket,
'https': 'https://' + current_socket
}
try:
request = requests.post(
self.url,
files={'upload_file': open(self.rdata['file'], 'rb')},
auth=(self.username, self.password),
timeout=self.rdata['timeout'],
headers=self.rdata['headers'],
proxies=proxies)
self._set_request_data(request, current_socket)
except Exception as e:
self._is_err(e)
self.post_file_with_headers()
else:
self._limit_succeeded()