一、python脚本的规范:
每个脚本都有自己的规范,以下的规范不是强制的,但可以使你的脚本规范、易懂、方便使用。
#!/usr/bin/env python
# -- coding: utf-8 --
这个写在开头,定义脚本编码。现在多数都是UTF8格式,所以写脚本尽量用这个编码,遇到中文可以做编码处理,字符串编码处理主要就是encode和decode。
import os,urllib,MySQLdb,time,platform
导入需要的模块。
main():
pass
定义函数
if name == "main":
main()
这个就是说脚本从这里往下执行,如果是其他的脚本调用这个脚本,这个脚本不至于执行其他的部分。
提示:以上是整个脚本中的规范,大家在写脚本的时候尽量这么做。
二、python的缩进
python的对缩进要求很严格,缩进不对,就会报语法错误;python中缩进就是一个tab键或是4个空格,4个空格比较麻烦,直接一个tab键简单,所以没有特别的需求,缩进一般用tab键。缩进类似于分层,同一缩进就是相同的层次。见如下实例:
if a==0:
print a
else:
print b
三、每一个功能对应一个函数
这一点最重要,每一个功能就写一个函数,脚本清晰易懂,其他复用这个功能也方便,脚本也不冗余。不建议一个函数里面有好多功能,使函数模块化。
四、系统命令的引用
引用系统命令的时候,特别是linux命令,一定要写命令的全路径,比如:
os.popen("/sbin/ifconfig eth0").read()
如果写成如下命令:
os.popen("ifconfig eth0").read()
这样也是没有问题的,手动执行脚本时会执行,但是脚本做cron的时候,就不会执行了。所以要特别注意要写命令的全路径。
五、异常处理
try:
pass
except Exception,e:
print e
其中e就是错误信息。try的异常处理这么写就足够用了,还有其他的方法,不常用。
下例是一个获取本地ip地址,从数据库查询ip的用途,去连接一个URL,判断这个URL是否可以用,并写日志。主要讲python操作数据库的常用用法。
#!/usr/bin/env python
# -- coding: utf-8 --
import os,urllib,MySQLdb,time,platform
def logw(text):
logfile = "/tmp/websocket.log"
if os.path.isfile(logfile):
if (os.path.getsize(logfile)/1024/1024) > 100:
os.remove(logfile)
now = time.strftime("%Y-%m-%d %H:%M:%S")
tt = str(now) + "\t" + str(text) + "\n"
f = open(logfile,'a+')
f.write(tt)
f.close()
def getidcname(ip):
try:
conn = MySQLdb.connect(host = '192.168.8.43',port=3306,user = 'readapp',passwd = '123456',charset='utf8',connecttimeout=20)
cursor = conn.cursor()#查询出的结果是元组形式,元组和列表基本一样。
#cursor = conn.cursor(cursorclass = MySQLdb.cursors.DictCursor)#查询结果是字典形式。
sql = "select host,user from mysql.user where host='%s'" % ip#python中执行sql语句一次只能是一个sql语句,一次只执行一条,如果用分号分开写多条的话是会报错的,如果是多条sql语句可以多写几个sql和cursor.execute()来分开执行。
cursor.execute(sql)#执行sql语句。
#cursor.executemany("""insert into dist_sniffer.sniffer_order_day values(%s,%s,%s,%s,%s,%s,%s,%s,%s) """,values)#执行组合插入数据库的时候可以用这个,每个%s代表一个数据库字段,values是一个元组或是一个列表。
alldata = cursor.fetchall()#接收sql执行结果,如果是写操作的,这个就不用了。
#conn.commit()如果是写操作,需要这个去提交。
cursor.close()
conn.close()#关闭数据库回话。
return alldata[0][0].encode('UTF8')#如果是写操作的话就没有返回值了。
except Exception,e:
return 0
def get_ip():
os = platform.system()
if os == "Linux":
ip = os.popen("/sbin/ifconfig eth0|grep 'inet addr'").read().strip().split(":")[1].split()[0]
elif os == "Windows":
import wmi
c=wmi.WMI()
network = c.Win32_NetworkAdapterConfiguration (IPEnabled=1)
for interface in network:
if interface.DefaultIPGateway:
ip = interface.IPAddress[0]
return ip
#print interface.IPAddress[0],interface.MACAddress,interface.IPSubnet[0],interface.DefaultIPGateway[0],interface.DNSServerSearchOrder[0],interface.DNSServerSearchOrder[1]
#获取出网的ip地址、MAC地址、子网掩码、默认网关、DNS
def web_status():
ip = get_ip()
idc_name = get_idcname(ip)
url = "http://www.text.com/index.php?idc_ip=%s&idc_name=%s" % (ip,idc_name)
get = urllib.urlopen(url)
if get.getcode() == 200:
aa = int(get.read().strip())
if aa == 1:
text = "Webservice return OK"
else:
text = "Webservice return Error"
else:
text = "Conect webservice Error"
print text
log_w(text)
if __name == "__main":
web_status()
一开始就要养成一个好习惯,这样对以后python编程是十分有益的。