Repository navigation
Expand file tree
/
Copy pathviews.py
More file actions
76 lines (69 loc) · 2.08 KB
/
Copy pathviews.py
File metadata and controls
76 lines (69 loc) · 2.08 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
from django.http import JsonResponse, HttpResponse
# Create your views here.
from IPPool.models import ProxyIP
def index(requests):
"""
返回到说明页
:param requests:
:return:
"""
context = '<h3>1.访问接口http://{运行服务器的ip}/api/fetch/ 随机返回一个代理ip信息</h3> <br/>' \
'<h3>2.访问接口http://{运行服务器的ip}/api/random/{个数}, 随机返回指定个数</h3> <br/>'
return HttpResponse(context)
def fetch(requests):
"""
随机返回一个ip信息
:param requests:
:return:
"""
proxy_info = ProxyIP.objects.order_by('?').first()
if proxy_info:
data = {
'code': '200',
'msg': 'success',
'ip_info': {
'protocol': proxy_info.get_protocol_display(),
'types': proxy_info.get_types_display(),
'ip': proxy_info.ip,
'port': proxy_info.port,
'speed': proxy_info.speed,
'ip_address':proxy_info.ip_address,
'verify_time': proxy_info.verify_time.strftime("%Y-%m-%d %H:%M:%S")
}
}
else:
data = {
'code': '200',
'msg': 'Data is empty',
'ip_info': ''
}
return JsonResponse(data)
def random(requests, num):
"""
随机返回num个数个ip
:param requests:
:param num:
:return:
"""
try:
n = int(num)
except ValueError:
n = 1
ip_info_list = ProxyIP.objects.order_by('?')[:n]
ip_info = []
for proxy_info in ip_info_list:
ip_info.append({
'protocol': proxy_info.get_protocol_display(),
'types': proxy_info.get_types_display(),
'ip': proxy_info.ip,
'port': proxy_info.port,
'speed': proxy_info.speed,
'ip_address': proxy_info.ip_address,
'verify_time': proxy_info.verify_time.strftime("%Y-%m-%d %H:%M:%S")
})
data = {
'code': '200',
'msg': 'success',
'ip_info': ip_info
}
return JsonResponse(data)