-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproxy_requests.py
More file actions
208 lines (188 loc) · 8.23 KB
/
proxy_requests.py
File metadata and controls
208 lines (188 loc) · 8.23 KB
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
#! /usr/bin/python3
from lxml import html
from requests.auth import HTTPBasicAuth
import re
import json
import requests
class ProxyRequests:
def __init__(self, url):
self.sockets = []
self.url = url
self.proxy = ""
self.request = None
self.headers = {}
self.file_dict = {}
self.status_code = ""
self.proxy_used = ""
self.__acquire_sockets()
# get a list of sockets from sslproxies.org
def __acquire_sockets(self):
r = requests.get("https://www.sslproxies.org/")
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:
self.sockets.append(socket_str[:-5].replace("</td>", ":"))
self.proxy_used = self.sockets.pop(0)
# try proxy sockets until successful GET
def get(self):
retries = 30
for retry in range(retries):
if len(self.sockets) > 0:
current_socket = self.proxy_used
proxies = {"http": "http://" + current_socket, "https": "https://" + current_socket}
try:
request = requests.get(self.url, timeout=3.0, proxies=proxies)
self.request = request
self.headers = request.headers
self.status_code = request.status_code
except Exception as e:
self.proxy_used = self.sockets.pop(0)
continue
break
else:
print("Acquiring new sockets")
self.__acquire_sockets()
return self.request
# 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)
# 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)
# 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()
# not intended for string or html... a string may work but should be for a json dict response
def to_json(self):
return json.dumps(json.JSONDecoder().decode(self.request))
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.text)
class ProxyRequestsBasicAuth(ProxyRequests):
def __init__(self, url, username, password):
super().__init__(url)
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 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.text)