Home  >  Article  >  Backend Development  >  python学习之编写查询ip程序

python学习之编写查询ip程序

WBOY
WBOYOriginal
2016-06-10 15:06:071064browse

公司服务器上的ip最少的也有100多个,有时候查到一个站的Ip, 不想通过OA去查,自己就用自己最近学的python知识,结合数据库,编写了一python小程序。实现只要输入主ip就能查到这台服务器的子ip,输入子ip能查到此ip所在的主服务器。

功能示例:

使用 -m 参数 指定主服务器地址,即查询此服务器上所有的子ip

使用 -s 参数 指定子ip, 即可查询此子ip所在的服务器主ip地址

使用 -h 或 -help 参数可打印help

使用 -v 或-version参数可打印版本

如果程序参数不合法,则提示帮助

好了,功能就这么多,我们来看看怎么用python 实现的。

一,我们先看一下数据库,看一下他的结构,其实数据库里很简单,只记录了ip的对应关系。

如下图

二,我们来看一下程序是怎么写的, 先贴一下程序。

#! /usr/bin/python
#Filename select.py
import MySQLdb,os,sys
 
try:
  conn = MySQLdb.connect("localhost","root","密码","ips",charset="utf8")
except MySQLdb.OperationalError, message:
  print "link error"
 
def masterip(ip):

  sql="select secip from ip_master where masterip='%s'" %ip
  cursor=conn.cursor()
  n=cursor.execute(sql)
  cds=cursor.fetchall()
  for cd in cds:
    for col in cd:
      print "%s" % (col)

  cursor.close()
  conn.close()
 
def secip(ip):

  sql="select masterip from ip_master where secip='%s'" %ip
  cursor=conn.cursor()
  n=cursor.execute(sql)
  cds=cursor.fetchall()

  for cd in cds:
    for col in cd:
      print "%s" % (col)

  cursor.close()
  conn.close()


 
if len(sys.argv)<2:
  print "You have an error in you syntax,please you -help,-h for help"
  sys.exit()


 
if "-h"==sys.argv[1] or "-help"==sys.argv[1]:
  print '''\
This program select master ips and slave ips.
Options include:
-s slave ip :use slave ip to select msterip
-m masterip :use master ip to select slaveip
-h;-help  :help
-v;-version :prints version '''
  sys.exit()

 
elif "-v"==sys.argv[1] or "-version"==sys.argv[1]:
  print "Version is 0.1"
  sys.exit()

 
elif "-s"==sys.argv[1]:
  if len(sys.argv)<3:
    print "You have an error in you syntax,please you -help,-h for help"
    sys.exit()
  ip=sys.argv[2]

  secip(ip)

 
elif "-m"==sys.argv[1]:
  if len(sys.argv)<3:
    print "You have an error in you syntax,please you -help,-h for help"
    sys.exit()
  ip=sys.argv[2]

  masterip(ip)
 
else:
  print "You have an error in you syntax,please you -help,-h for help"

三.对程序进行解释

#! /usr/bin/python         
import MySQLdb,os,sys   #加载 mysqldb os sys
try:
  conn = MySQLdb.connect("localhost","root","密码","ips",charset="utf8")
except MySQLdb.OperationalError, message:
  print "link error"
 
#尝试利用括号里的信息去连接数据库,如果连接数据库不成功刚打印link error! 

 
def masterip(ip):
  sql="select secip from ip_master where masterip='%s'" %ip
  cursor=conn.cursor()
  n=cursor.execute(sql)
  cds=cursor.fetchall()
  for cd in cds:
    for col in cd:
      print "%s" % (col)
  cursor.close()
  conn.close()
 
# 定义一个masterip函数, 括号里的ip 为参数,在下面的sql语句里用到。sql后面是查询语句。 利用上面括号里定义的ip 去查询 子ip。再利用for 循环逐个打印出ip!

 
def secip(ip):
  sql="select masterip from ip_master where secip='%s'" %ip
  cursor=conn.cursor()
  n=cursor.execute(sql)
  cds=cursor.fetchall()
  for cd in cds:
    for col in cd:
      print "%s" % (col)
  cursor.close()
  conn.close()
 
# 定义一个secip函数, 括号里的ip 为参数,在下面的sql语句里用到。sql后面是查询语句。 利用上面括号里定义的ip 去查询 主ip。再利用for 循环逐个打印出ip!

 
if len(sys.argv)<2:
  print "You have an error in you syntax,please you -help,-h for help"
  sys.exit()
 
# 判断命令行参数,如果命令行参数小于2,(命令本身就属于一个参数)就打印提示信息,并退出,此行的意思就是半数命令后面有没有跟参数,如果没有跟参数,就直接退出。
 
if "-h"==sys.argv[1] or "-help"==sys.argv[1]:
  print '''\
This program select master ips and slave ips.
Options include:
-s slave ip :use slave ip to select msterip
-m masterip :use master ip to select slaveip
-h;-help  :help
-v;-version :prints version '''
  sys.exit()
 
#判断命令行第一个数据是不是 -h (注,命令行参数是从0开始,0 也就是命令本身),如果是 -h的话,就打印帮助信息,并退出。
 
elif "-v"==sys.argv[1] or "-version"==sys.argv[1]:
  print "Version is 0.1"
  sys.exit()
 
#判断命令行第一个数据是不是 -v (注,命令行参数是从0开始,0 也就是命令本身),如果是 -v的话,就打印版本信息,并退出。

 
elif "-s"==sys.argv[1]:
  if len(sys.argv)<3:
    print "You have an error in you syntax,please you -help,-h for help"
    sys.exit()
  ip=sys.argv[2]
  secip(ip)
 
#判断命令行第一个数据是不是 -s (注,命令行参数是从0开始,0 也就是命令本身),如果是 -s的话,判断命令行参数是否小于3,也就是 -s 后面有没有跟参数(ip), 如果没跟的话,就打印提示信息,并退出 。如果跟了的话,就把-s 后面的参数给 ip这个变量,并执行 secip() 这个函数。

 
elif "-m"==sys.argv[1]:
  if len(sys.argv)<3:
    print "You have an error in you syntax,please you -help,-h for help"
    sys.exit()
  ip=sys.argv[2]
  masterip(ip)
 
#判断命令行第一个数据是不是 -m (注,命令行参数是从0开始,0 也就是命令本身),如果是 -m的话,判断命令行参数是否小于3,也就是 -m 后面有没有跟参数(ip), 如果没跟的话,就打印提示信息,并退出 。如果跟了的话,就把-m 后面的参数给 ip这个变量,并执行 masterip() 这个函数。

 
else:
  print "You have an error in you syntax,please you -help,-h for help"

如果跟上面所有的参数都不符合,就直接打印帮助信息。

Ok!程序完了。很简单,但好像也很实用。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn