forked from awolfly9/IPProxyTool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
137 lines (105 loc) · 3.91 KB
/
utils.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#-*- coding: utf-8 -*-
import logging
import os
import re
import subprocess
import traceback
import time
import datetime
# 自定义的日志输出
def log(msg, level = logging.DEBUG):
logging.log(level, msg)
print('%s [%s], msg:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'), level, msg))
if level == logging.WARNING or level == logging.ERROR:
for line in traceback.format_stack():
print(line.strip())
for line in traceback.format_stack():
logging.log(level, line.strip())
# 服务器使用,清理端口占用
def kill_ports(ports):
for port in ports:
log('kill %s start' % port)
popen = subprocess.Popen('lsof -i:%s' % port, shell = True, stdout = subprocess.PIPE)
(data, err) = popen.communicate()
log('data:\n%s \nerr:\n%s' % (data, err))
pattern = re.compile(r'\b\d+\b', re.S)
pids = re.findall(pattern, data)
log('pids:%s' % str(pids))
for pid in pids:
if pid != '' and pid != None:
try:
log('pid:%s' % pid)
popen = subprocess.Popen('kill -9 %s' % pid, shell = True, stdout = subprocess.PIPE)
(data, err) = popen.communicate()
log('data:\n%s \nerr:\n%s' % (data, err))
except Exception, e:
log('kill_ports exception:%s' % e)
log('kill %s finish' % port)
time.sleep(1)
# 获取创建存储代理 ip 表的命令
def get_create_table_command(table_name):
command = (
"CREATE TABLE IF NOT EXISTS {} ("
"`id` INT(8) NOT NULL AUTO_INCREMENT,"
"`ip` CHAR(25) NOT NULL UNIQUE,"
"`port` INT(4) NOT NULL,"
"`country` TEXT DEFAULT NULL,"
"`anonymity` INT(2) DEFAULT NULL,"
"`https` CHAR(4) DEFAULT NULL ,"
"`speed` FLOAT DEFAULT NULL,"
"`source` CHAR(20) DEFAULT NULL,"
"`save_time` TIMESTAMP NOT NULL,"
"PRIMARY KEY(id)"
") ENGINE=InnoDB".format(table_name))
return command
# 获取插入代理 ip 表的命令
def get_insert_data_command(table_name):
command = ("INSERT IGNORE INTO {} "
"(id, ip, port, country, anonymity, https, speed, source, save_time)"
"VALUES(%s, %s, %s, %s, %s, %s, %s, %s, %s)".format(table_name))
return command
# 获取删除指定 id 的命令
def get_delete_data_command(table_name, id):
command = ("DELETE FROM {0} WHERE id={1}".format(table_name, id))
return command
# 获取更新指定 id 的命令
def get_update_data_command(table_name, id, speed):
command = ("UPDATE {0} SET speed={1} WHERE id={2}".format(table_name, speed, id))
return command
# 获取表的长度
def get_table_length(sql, table_name):
try:
command = ('SELECT COUNT(*) from {}'.format(table_name))
sql.execute(command)
(count,) = sql.cursor.fetchone()
log('get_table_length results:%s' % str(count))
return count
except:
return 0
# 通过指定 id 得到代理信息
def get_proxy_info(sql, table_name, id):
command = ('SELECT * FROM {0} limit {1},1;'.format(table_name, id))
result = sql.query_one(command)
if result != None:
data = {
'id': result[0],
'ip': result[1],
'port': result[2],
'country': result[3],
'anonymity': result[4],
'https': result[5],
'speed': result[6],
'source': result[7],
'save_time': result[8],
}
return data
return None
# 插入代理
def sql_insert_proxy(sql, table_name, proxy):
command = get_insert_data_command(table_name)
msg = (None, proxy.ip, proxy.port, proxy.country, proxy.anonymity, proxy.https, proxy.speed, proxy.source, None)
sql.insert_data(command, msg)
def make_dir(dir):
log('make dir:%s' % dir)
if not os.path.exists(dir):
os.makedirs(dir)