forked from qiyeboy/IPProxyPool
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapiServer.py
More file actions
73 lines (66 loc) · 2.58 KB
/
Copy pathapiServer.py
File metadata and controls
73 lines (66 loc) · 2.58 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
#coding:utf-8
'''
定义几个关键字,count type,protocol,country,area,
'''
import urllib
from config import API_PORT
from db.SQLiteHelper import SqliteHelper
__author__ = 'Xaxdus'
import BaseHTTPServer
import json
import urlparse
import logging
logger = logging.getLogger('api')
# keylist=['count', 'types','protocol','country','area']
class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
"""
"""
dict={}
parsed_path = urlparse.urlparse(self.path)
try:
query = urllib.unquote(parsed_path.query)
logger.info("query %s" %query)
if query.find('&')!=-1:
params = query.split('&')
for param in params:
dict[param.split('=')[0]]=param.split('=')[1]
else:
dict[query.split('=')[0]]=query.split('=')[1]
sqlHelper = SqliteHelper()
# 处理删除代理的请求
if dict.has_key('delete'):
condition="ip='" + dict['ip'] + "' AND port=" + dict['port']
sqlHelper.delete(SqliteHelper.tableName, condition)
self.send_response(200)
self.end_headers()
self.wfile.write("Success delete proxy: " + dict['ip'] + ":" + dict['port'])
else:
str_count=''
conditions=[]
for key in dict:
if key =='count':
str_count = 'LIMIT 0,%s'% dict[key]
if key =='country' or key =='area':
conditions .append(key+" LIKE '"+dict[key]+"%'")
elif key =='types' or key =='protocol' or key =='country' or key =='area':
conditions .append(key+"="+dict[key])
if len(conditions)>1:
conditions = ' AND '.join(conditions)
else:
conditions =conditions[0]
result = sqlHelper.select(sqlHelper.tableName,conditions,str_count)
# print type(result)
# for r in result:
# print r
data = [{'ip':item[0], 'port': item[1]} for item in result]
data = json.dumps(data)
self.send_response(200)
self.end_headers()
self.wfile.write(data)
except Exception,e:
logger.warning(str(e))
self.send_response(404)
if __name__=='__main__':
server = BaseHTTPServer.HTTPServer(('0.0.0.0',API_PORT), WebRequestHandler)
server.serve_forever()