-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathHTTPThread.py
More file actions
67 lines (51 loc) · 1.88 KB
/
HTTPThread.py
File metadata and controls
67 lines (51 loc) · 1.88 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
# This file is part of the OpenWire Framework.
#
# OpenWire is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# OpenWire is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with OpenWIre. If not, see <http://www.gnu.org/licenses/>.
import httplib2
import threading
import Queue
# HTTPThread handles concurrent HTTP Requests
class HTTPThread(threading.Thread):
def __init__(self, options_q, reqult_q, proxy=None, timeout=30):
super(HTTPThread, self).__init__()
self.name = self.getName()
self.options_q = options_q
self.result_q = result_q
self.options = options()
self.timeout = timeout
self.h = None
self.proxy = proxy
def run(self):
while not self.stoprequest.isSet():
try:
options = self.options_q.get(True, 0.05)
for key, val in options:
setattr(self.options, key, val)
result = self.requestData()
self.result_q.put(result)
except:
continue
def join(self, timeout=0.5):
self.stoprequest.set()
super(HTTPThread, self).join(0.05)
def requestData(self):
if hasattr(self.options, 'proxy'):
proxy = self.proxy.split(":")
self.h = httplib2.Http(proxy_info = httplib2.ProxyInfo(socks.PROXY_TYPE_HTTP, proxy[0], int(proxy[1])), timeout=self.timeout)
else:
self.h = httplib2.Http(timeout=self.timeout)
if not hasattr(self.options, 'url') or not hasattr(self.options, 'method'):
return False
# Dummy class used as container for dynamic variables
class options: pass