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
| #!/usr/bin/python
#coding=utf-8
#save in /var/www/excuite/extmail
#yum install -y MySQL-python
#apt-get install python-MySQLdb
import MySQLdb
def SqlResult(sqlcmd):
try:
conn = MySQLdb.connect(host='localhost',user='root',passwd='password',db='extmail',port=3306) # 定义连接,修改登陆账号成你自己的
cur = conn.cursor()
cur.execute(sqlcmd)
result = cur.fetchall()
cur.close()
conn.close()
return result
except MySQLdb.Error,e:
print 'mysql error msg:',e
def Books():
d = {}
domains = 'select domain from domain;'
for domain in SqlResult(domains):
root = domain[0].split('.')[0]
if root == 'defaultdomain': #defaltdomain改成你的默认域名
f = 'globabook.cf'
else:
f = 'globabook-'+root+'.cf'
d[domain[0]] = f
return d
def Users(domain):
us = ["Name,Mail,Company,Phone"]
usernames = "select name,username from mailbox where active='1' and domain='%s';" % domain
for user in SqlResult(usernames):
u='\n"'+user[0].decode("utf-8")+'","'+user[1]+'"'
us.append(u)
return us
def Main():
bs = Books()
for dm in bs:
f = bs.get(dm)
book = open(f,"w")
us = Users(dm)
for user in us:
book.write(user.encode("utf-8"))
book.close()
print "Congratulations, the %s is update!" % f
print "ALL Globabook Files Are Updated !"
Main()
|