-
Notifications
You must be signed in to change notification settings - Fork 35
/
example.py
66 lines (51 loc) · 1.5 KB
/
example.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# @author [email protected]
# @site https://chenjiehua.me
# @date 2016-09-06
#
"""proxy ip example
"""
import csv
import random
import requests
retrymax = 3
timeout = 30
proxycsv = "proxyip.csv"
target = "http://www.baidu.com"
def main():
total_weight, proxyip = load_proxyip(proxycsv)
for i in range(retrymax):
ip = get_proxyip(total_weight, proxyip)
print "Using proxy:", ip
try:
r = requests.get(target, proxies={"http": "http://" + ip}, timeout=timeout)
print "Http resp code:", r.status_code
break
except Exception as e:
print e
def load_proxyip(fpath):
""" proxyip list with weight
"""
total_weight, proxyip = 0, []
with open(fpath) as f:
csvreader = csv.DictReader(f, restval=0, delimiter=",",
quotechar="\"", quoting=csv.QUOTE_MINIMAL)
for row in csvreader:
ip = row["ip"] + ":" + row["port"]
weight = 1/float(row["speed"])
total_weight += weight
proxyip.append((ip, weight))
return total_weight, proxyip
def get_proxyip(total_weight, proxyip):
r = random.uniform(0, total_weight)
upto = 0
for ip, weight in proxyip:
if upto + weight >= r:
return ip
upto += weight
print "Error: total_weight=%s, random_weight=%s" % (total_weight, r)
return "localhost:8888"
if __name__ == "__main__":
main()