光环大数据培训 实现服务器数据采集脚本技术点总结
一、从别的目录导入模块的两种方式
1
2
sys.path.append(“想要导入的文件的存放目录”)
import
模块名
1
2
BASE_DIR
=
os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
1
import
MySQLdb
1
2
3
4
5
6
7
8
9
10
11
def
exec_curr(sql):
try
:
#打开数据库连接
cnn
=
MySQLdb.connect(host
=
host,port
=
port,user
=
db_user,passwd
=
db_passwd,charset
=
'utf8'
,db
=
db)
#使用cursor()方法获取操作游标
cur
=
cnn.cursor()
#使用execute方法执行SQL语句
cur.execute(sql)
cnn.commit()
except
MySQLdb.Error,e:
print
e
1
2
3
4
5
6
7
8
9
10
11
12
13
def
connet_curr(host
=
host,port
=
port,db
=
db,db_user
=
db_user,db_passwd
=
db_passwd,sql
=
"select ip from nosql_ip"
):
result
=
[]
try
:
cnn
=
MySQLdb.connect(host
=
host,port
=
port,user
=
db_user,passwd
=
db_passwd,charset
=
'utf8'
,db
=
db)
cur
=
cnn.cursor()
cur.execute(sql)
#获取所有记录列表
rows
=
cur.fetchall()
for
row
in
rows:
result.append(row)
return
result
except
MySQLdb.Error,e:
print
e
1
2
pickle.dumps(dic)
pickle.loads(byte_data)
1
2
str
=
json.dumps(dic)
dic_obj
=
json.loads(dic_str)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
假设字典为 info
=
{
'staff1'
:
"小明"
,
'staff2'
:
"小红"
,
'staff3'
:
"小李"
}
1
、增加操作
info[
'staff4'
]
=
“小刘”
2
、修改操作
info[
'staff3'
]
=
"小樊"
3
、删除操作
1
)info.pop(
"staff1"
)
2
)
del
info[
'staff3'
]
4
、查询操作
1
)
"staff2"
in
info
2
)info.get(
"staff2"
) 如果一个key不存在,只返回
None
3
)info[
"staff2"
] 如果一个key不存在,就报错
5
、嵌套字典的查询只需要 :字典名[ ][ ] . . .
6
、循环字典
1
)
for
key
in
info:
print
(key,info[key])
2
)
for
k,v
in
info.items():
#会先把dict转成list
print
(k,v)
1
2
3
4
5
6
7
def
ssh_execute_shell(host,command):
ssh_shell
=
(
'''/usr/bin/ssh -n -i 跳板机的私钥地址 -p 26387 -o '''
'''StrictHostKeyChecking=no -o ConnectTimeout=2 root@'%s' '''
''' "%s" '''
%
(host, command))
p
=
subprocess.Popen(ssh_shell,shell
=
True
,stdout
=
subprocess.PIPE,stderr
=
subprocess.PIPE)
out
=
p.communicate()
return
out
1
2
3
4
5
6
7
8
9
from
multiprocessing
import
Process
import
time
def
f(name):
time.sleep(
2
)
print
(
'hello'
, name)
if
__name__
=
=
'__main__'
:
p
=
Process(target
=
f, args
=
(
'process1'
,))
p.start()
p.join()
1
2
3
4
5
6
7
8
9
10
11
from
multiprocessing
import
Process,Pool
import
time
def
Foo(i):
time.sleep(
2
)
return
i
+
100
pool
=
Pool(
5
)
for
i
in
range
(
10
):
pool.apply_async(func
=
Foo, args
=
(i,))
#pool.apply(func=Foo, args=(i,))
pool.close()
pool.join()
#进程池中进程执行完毕后再关闭,如果注释,那么程序直接关闭。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import
smtplib
from
email.MIMEText
import
MIMEText
mailto_list
=
[
"收件人用户名@staff.sina.com.cn"
]
mail_host
=
"mail.staff.sina.com.cn"
mail_user
=
"发件人用户名"
mail_pass
=
"发件人邮箱密码"
mail_postfix
=
"staff.sina.com.cn"
def
send_mail(to_list, sub, content):
me
=
"hello"
+
"<"
+
mail_user
+
"@"
+
mail_postfix
+
">"
msg
=
MIMEText(content, _subtype
=
'html'
, _charset
=
'UTF-8'
)
msg[
'Subject'
]
=
sub
msg[
'From'
]
=
me
msg[
'To'
]
=
";"
.join(to_list)
try
:
s
=
smtplib.SMTP()
s.connect(mail_host)
s.login(mail_user, mail_pass)
s.sendmail(me, to_list, msg.as_string())
s.close()
return
True
except
Exception as e:
print
str
(e)
return
False
大数据培训哪家好、大数据培训靠谱吗、大数据培训排行榜、大数据培训费用、大数据培训多少钱等相关问题,欢迎咨询光环大数据官网在线客服:hadoop.aura.cn
大数据培训、人工智能培训、Python培训、大数据培训机构、大数据培训班、数据分析培训、大数据可视化培训,就选光环大数据!光环大数据,聘请专业的大数据领域知名讲师,确保教学的整体质量与教学水准。讲师团及时掌握时代潮流技术,将前沿技能融入教学中,确保学生所学知识顺应时代所需。通过深入浅出、通俗易懂的教学方式,指导学生更快的掌握技能知识,成就上万个高薪就业学子。 更多问题咨询,欢迎点击------>>>>在线客服!