- 论坛徽章:
- 2
|
本帖最后由 honghs 于 2014-12-01 13:10 编辑
1.你是如何学习Python的?看书?看视频?还是?
看电子书,视频太花时间了
2.你工作中有用到Python吗?主要用来做什么?
工作中用到的python也不多。主要是做系统管理,小工具之类的
3.你觉得Python跟其他脚本语言比优势在哪?
linux默认安装有python,可以跨平台。
4.谈谈Python在自动化运维中的应用。
正在研究自动化运维。向天斯学习吧,原来在帝都那边,在电梯里边经常见到天斯,可惜没机会认识啊
5.给出一个你自己写的Python脚本(最好有详细注释)。- #取数据库里边的网关参数,尝试连接各个网关,其实就是特定的ip和端口啦。
- #Check GateWay Port
- #-*-coding:utf-8-*-
- import cx_Oracle
- import os
- import sys
- import logging
- import logging.handlers
- import socket
- from configobj import ConfigObj
-
- #get Oracle Connect
- def getOraConnect(user,password,serer,port,sid):
- try:
- oraConnect = cx_Oracle.connect(user,password,serer+':'+port+'/'+sid)
- if oraConnect!=None:
- return oraConnect
- else:
- return None
- except Exception,ex:
- print ex.message
- logging.warn(ex.message)
-
- def connectServer(ip,port,timeout=10):
- #test server
- sk = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
- sk.settimeout(timeout)
- try:
- sk.connect((ip,port))
- return True
- except Exception,ex:
- logging.warn(ex.message)
- return False
- finally:
- sk.close()
-
- def checkGaetWay(oraConn):
- #get GateWay Prameters
- gwserver=[]
- gwDict={}
- channelid=""
- gateWayIP=""
- gateWayName=""
- gateWayPort=0
- gateWaySql="""
- SELECT b.id,
- b.channelname,
- CASE c.fieldname
- WHEN '网关端口号'
- THEN 'PORT'
- WHEN '网关端口'
- THEN 'PORT'
- WHEN '网关地址'
- THEN 'IP'
- WHEN '数据库IP'
- THEN 'IP'
- WHEN '数据库端口号'
- THEN 'PORT'
- WHEN '数据库端口'
- THEN 'PORT'
- WHEN '网关下行端口'
- THEN 'PORT'
- ELSE c.fieldname
- END AS fieldname ,
- a.value
- FROM t_gateway_params A
- INNER JOIN v_channel B
- ON B.id=a.channelid
- INNER JOIN t_gateway_metadata c
- ON c.id =a.gatewaymetadataid
- AND c.fieldname IN ('网关地址','网关端口号','数据库IP','数据库端口','网关端口','数据库端口号','网关下行端口')
- ORDER BY a.channelid
- """
- oracur = oraConn.cursor()
- oracur.execute(gateWaySql)
- orars = oracur.fetchall()
- rowcount = oracur.rowcount
- if rowcount>0:
- for rs in orars:
- channelid= rs[0]
- gateWayName=rs[1]
- if gwDict.has_key(channelid):
- gwServer = gwDict[channelid]
- if (str(rs[2])=="IP"):
- gwServer[1]= str(rs[3])
- if (str(rs[2])=="PORT"):
- gwServer[2]=int(rs[3])
- gwDict[channelid]= gwServer
- else:
- gwServer = [gateWayName,"127.0.0.1",0]
- if (str(rs[2])=="IP"):
- gwServer[1]= str(rs[3])
- if (str(rs[2])=="PORT"):
- gwServer[2]=int(rs[3])
- gwDict[channelid]= gwServer
- #
- print 'All GateWay Connect Test Begin..'
- logging.info('All GateWay Connect Tes Begin..' )
- for gw in gwDict:
- gwServer=gwDict[gw]
- gateWayName= gwServer[0]
- gateWayIP= gwServer[1]
- gateWayPort=gwServer[2]
- if connectServer(gateWayIP, gateWayPort,5):
- print "connect to %s ,ip %s ,port %s success" %(gateWayName,gateWayIP,gateWayPort)
- logging.info("connect to %s ,ip %s ,port %s success" %(gateWayName,gateWayIP,gateWayPort))
- else:
- print "connect to %s ,ip %s ,port %s fail" %(gateWayName,gateWayIP,gateWayPort)
- logging.warn("connect to %s ,ip %s ,port %s fail" %(gateWayName,gateWayIP,gateWayPort) )
- print 'All GateWay Connect Test End...'
- logging.info('All GateWay Connect Tes End..' )
- else:
- print "Can't Get GateWay Parameters..."
- logging.warn("Can't Get GateWay Parameters...")
-
-
- def main():
- #get oracle config
- orauser=configobj["orauser"]
- orapassword=configobj["orapassword"]
- oraserver=configobj["oraserver"]
- oraport=configobj["oraport"]
- orasid=configobj["orasid"]
-
- logging.info("Check GateWay Begin...")
- oraConn = getOraConnect(orauser, orapassword, oraserver, oraport, orasid)
- if oraConn!=None:
- checkGaetWay(oraConn)
- oraConn.close()
- logging.info("Check GateWay End...")
- if __name__=="__main__":
- configobj=ConfigObj("CheckGateWay.conf")
- logging.basicConfig(filename='CheckGateWay.log',level=logging.DEBUG)
- os.environ['NLS_LANG'] = 'SIMPLIFIED CHINESE_CHINA.UTF8'
- main()
-
复制代码 |
|