forked from rootVIII/proxy_requests
-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy_requests.py
executable file
·311 lines (288 loc) · 12.5 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
#! /usr/bin/python3
import requests
import re
import json
from requests.auth import HTTPBasicAuth
class ProxyRequests:
def __init__(self, url, country):
self.sockets = []
self.url = url
self.country = country
if self.country == "Any" :
self.country = None
self.proxy = ""
self.request = ""
self.headers = {}
self.file_dict = {}
if country != "":
self.__acquire_sockets()
self.status_code = ""
self.proxy_used = ""
# get a list of sockets from sslproxies.org
def __acquire_sockets(self):
r = requests.get("https://www.sslproxies.org/")
if self.country is not None:
matches = re.findall(r"<td>\d+.\d+.\d+.\d+</td><td>\d+</td><td>"+self.country+"</td>", r.text)
if len(matches) < 1:
matches = re.findall(r"<td>\d+.\d+.\d+.\d+</td><td>\d+</td>", r.text)
else :
matches = re.findall(r"<td>\d+.\d+.\d+.\d+</td><td>\d+</td>", r.text)
revised_list = [m1.replace("<td>", "") for m1 in matches]
for socket_str in revised_list:
if self.country is not None:
self.sockets.append(socket_str[:-5].replace("</td>", ":").replace(":"+self.country, ""))
else:
self.sockets.append(socket_str[:-5].replace("</td>", ":"))
# recursively try proxy sockets until successful GET
def get(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.get(self.url, timeout=3.0, proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.get()
else:
try:
request = requests.get(self.url, timeout=3.0)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
except:
print('working...')
self.get()
# recursively try proxy sockets until successful GET with headers
def get_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.get(self.url, timeout=3.0,headers=self.headers, proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.get_with_headers()
else:
try:
request = requests.get(self.url, timeout=3.0,headers=self.headers)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
except:
print('working...')
self.get_with_headers()
# recursively try proxy sockets until successful POST
def post(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url, json=data, timeout=3.0, proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post(data)
else :
try:
request = requests.post(self.url, json=data, timeout=3.0)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
except:
print('working...')
self.post(data)
# recursively try proxy sockets until successful POST with headers
def post_with_headers(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url,
json=data,
timeout=3.0,
headers=self.headers,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post_with_headers(data)
else :
try:
request = requests.post(self.url,
json=data,
timeout=3.0,
headers=self.headers)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
except:
print('working...')
self.post_with_headers(data)
# recursively try proxy sockets until successful POST with file
def post_file(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url,
files=self.file_dict,
timeout=3.0,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post_file()
else :
try:
request = requests.post(self.url,
files=self.file_dict,
timeout=3.0)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
except:
print('working...')
self.post_file()
# not intended for string or html... a string may work but should be for a json dict response
def to_json(self):
try:
json_resp = json.dumps(json.JSONDecoder().decode(self.request))
except:
return json.dumps({'error': 'No JSON Object could be decoded'})
return json_resp
def get_headers(self):
return self.headers
def set_headers(self, outgoing_headers):
self.headers = outgoing_headers
def set_file(self, outgoing_file):
self.file_dict = outgoing_file
def get_status_code(self):
return self.status_code
def get_proxy_used(self):
return str(self.proxy_used)
def __str__(self):
return str(self.request)
class ProxyRequestsBasicAuth(ProxyRequests):
def __init__(self, url, username, password, country):
super().__init__(url,country)
self.username = username
self.password = password
# recursively try proxy sockets until successful GET (overrided method)
def get(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.get(self.url,
auth=(self.username, self.password),
timeout=3.0,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.get()
# recursively try proxy sockets until successful GET with Headers (overrided method)
def get_with_headers(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.get(self.url,
auth=(self.username, self.password),
timeout=3.0,
headers=self.headers,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.get_with_headers()
# recursively try proxy sockets until successful POST (overrided method)
def post(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url,
json=data,
auth=(self.username, self.password),
timeout=3.0,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post(data)
# recursively try proxy sockets until successful POST with headers (overrided method)
def post_with_headers(self, data):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url,
json=data,
auth=(self.username, self.password),
timeout=3.0,
headers=self.headers,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post_with_headers(data)
# recursively try proxy sockets until successful POST with file
def post_file(self):
if len(self.sockets) > 0:
current_socket = self.sockets.pop(0)
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.post(self.url,
files=self.file_dict,
auth=(self.username, self.password),
timeout=3.0,
proxies=proxies)
self.request = request.text
self.headers = request.headers
self.status_code = request.status_code
self.proxy_used = current_socket
except:
print('working...')
self.post_file()
def __str__(self):
return str(self.request)
if __name__ == "__main__":
# example post with headers
r = ProxyRequests("https://postman-echo.com/post","")
r.set_headers({"name": "rootVIII", "secret_message": "7Yufs9KIfj33d"})
r.post_with_headers({"key1": "value1", "key2": "value2"})
print(r)
print(r.get_headers())
print(r.get_status_code())
# print(r.to_json())
print(r.get_proxy_used())