forked from sosoho/my-python-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsave_mysql.py
More file actions
64 lines (55 loc) · 2.07 KB
/
save_mysql.py
File metadata and controls
64 lines (55 loc) · 2.07 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
#!/bin/env python
# -*- coding:utf-8 -*-
# _author:kaliarch
import pymysql
import configparser
import spider
class MysqlOper:
# initial database information
def __init__(self, result_list):
config = configparser.ConfigParser()
config.read('db.conf')
self.host = config['mysql']['HOST']
self.port = int(config['mysql']['PORT'])
self.user = config['mysql']['USER']
self.passwd = config['mysql']['PASSWD']
self.db = config['mysql']['DB']
self.table = config['mysql']['TABLE']
self.charset = config['mysql']['CHARSET']
self.result_list = result_list
def mysql_save(self):
# create db cursor
try:
DB = pymysql.connect(self.host, self.user, self.passwd, self.db, port=self.port, charset=self.charset)
cursor = DB.cursor()
except Exception as e:
print("connect dbserver fail,Please see information:")
print(e)
exit(1)
# check and create tables
cursor.execute('show tables in pydb')
tables = cursor.fetchall()
flag = True
for tab in tables:
if self.table in tab:
flag = False
print('%s is exist' % self.table)
print(flag)
if flag:
cursor.execute(
'''create table pytab (id int unsigned not null primary key auto_increment, protocol varchar(10),content varchar(50))''')
else:
return 0
# write database
for values in self.result_list:
for prot, cont in values.items():
try:
cursor.execute("insert into pytab (protocol,content) value (%s,%s);", [prot, cont])
except Exception as e:
print("insert db occer error", e)
if __name__ == "__main__":
proxyhelper = spider.GetProxyIP(3)
res_pool = proxyhelper.get_ip()
proxy_ip = proxyhelper.right_proxies(res_pool)
dbhelper = MysqlOper(proxy_ip)
dbhelper.mysql_save()