class DjangoSource ProxySource source_type django_model def load self

 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
class DjangoSource(ProxySource):
source_type = 'django_model'
def load(self):
"""
Load proxy list from specified source and validate loaded data.
Each server could be in two forms:
* simple: "server:port"
* complex: "server:port:user:pwd"
"""
now = datetime.now()
expired = now + timedelta(seconds=self.read_timeout)
query = Proxy.objects.filter(Q(expiration=None) | Q(expiration__gt=expired))
lines = u"\n".join([unicode(p) for p in query])
self.read_time = time.time()
self.server_list = self.get_server_list(lines)
self.server_list_iterator = itertools.cycle(self.server_list)
class DjangoProxyList(ProxyList):
def __init__(self, proxy_type='http', **kwargs):
self.init_kwargs = deepcopy(kwargs)
self.source = DjangoSource('django', proxy_type=proxy_type, **kwargs)
self.source.load()
self.filter_config = {}
self.geoip_resolver = None
def get_random(self):
"""
Return random server from the list
"""
if self.source.reload():
self.apply_filter()
if self.source.server_list:
return choice(self.source.server_list)