Programming and querying ip programs for python learning

Source: Internet
Author: User
This article mainly introduces how to compile and query an ip address program in python. if you need more than 100 ip addresses on the company's servers, you can check the Ip address of a website, if you don't want to query through OA, you can use your recent python knowledge and database to write a python applet. To achieve this, you only need to enter the primary ip address to find the sub-ip address of this server, and enter the sub-ip address to find the master server where this ip address is located.

Function example:

Use the-m parameter to specify the master server address, that is, to query all the sub-ip addresses on the server.

Use the-s parameter to specify the stator ip address to query the master ip address of the server where the child ip address is located.

Use the-h or-help parameter to print help

Use the-v or-version parameter to print the version.

If the program parameter is invalid, a prompt is displayed.

Well, there are so many features. let's take a look at how to implement it in python.

First, let's take a look at the database and look at its structure. In fact, the database is very simple and only records the correspondence between ip addresses.

For example

2. let's take a look at how the program was written. paste the program first.

#! /Usr/bin/python # Filename select. pyimport MySQLdb, OS, sys try: conn = MySQLdb. connect ("localhost", "root", "password", "ips", charset = "utf8") cannot be MySQLdb. operationalError, message: print "link error" def masterip (ip): SQL = "select secip from ip_master where masterip = '% s'" % ip cursor = conn. cursor () n1_cursor.exe cute (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 () n1_cursor.exe cute (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"

3. explain the program

#! /Usr/bin/python import MySQLdb, OS, sys # load mysqldb OS systry: conn = MySQLdb. connect ("localhost", "root", "password", "ips", charset = "utf8") cannot be MySQLdb. operationalError, message: print "link error" # try to connect to the database using the information in brackets. if the connection to the database fails, just print the link error! Def masterip (ip): SQL = "select secip from ip_master where masterip = '% s'" % ip cursor = conn. cursor () n1_cursor.exe cute (SQL) cds = cursor. fetchall () for cd in cds: for col in cd: print "% s" % (col) cursor. close () conn. close () # defines a masterip function. ip addresses in brackets are used as parameters in the following SQL statement. The SQL statement is followed by a query statement. Use the ip address defined in the brackets above to query the subip address. Use the for loop to print the ip addresses one by one! Def secip (ip): SQL = "select masterip from ip_master where secip = '% s'" % ip cursor = conn. cursor () n1_cursor.exe cute (SQL) cds = cursor. fetchall () for cd in cds: for col in cd: print "% s" % (col) cursor. close () conn. close () # defines a secip function. ip addresses in brackets are used as parameters in the following SQL statement. The SQL statement is followed by a query statement. Use the ip address defined in the brackets above to query the master ip address. Use the for loop to print the ip addresses one by one! If len (sys. argv) <2: print "You have an error in you syntax, please you-help,-h for help" sys. exit () # determine the command line parameter. if the command line parameter is smaller than 2 (the command itself is a parameter), print the prompt and exit, this line means that there is no parameter followed by half of the commands. if there is no parameter followed, exit directly. 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 () # determine whether the first data in the command line is-h (note: the command line parameter starts from 0, and 0 is the command itself). if it is-h, print the help information and exit. Elif "-v" = sys. argv [1] or "-version" = sys. argv [1]: print "Version is 0.1" sys. exit () # determine whether the first data in the command line is-v (note: the command line parameter starts from 0, and 0 is the command itself). if it is-v, print the version information and 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) # determine whether the first data in the command line is-s (note: the command line parameter starts from 0, and 0 is the command itself ), if it is-s, determine whether the command line parameter is less than 3, that is, whether there is a parameter (ip) after-s. If it is not followed, print the prompt information and exit. If the parameter is followed, the parameter following-s is assigned to the ip variable and the secip () function is executed. 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) # determine whether the first data in the command line is-m (note: the command line parameter starts from 0, and 0 is the command itself ), if it is-m, determine whether the command line parameter is less than 3, that is, whether there is a parameter (ip) after-m. if it is not followed, print the prompt information and exit. If it is followed, the parameter after-m is given to the ip variable and the masterip () function is executed. Else: print "You have an error in you syntax, please you-help,-h for help"

If it does not match all the preceding parameters, the help information is printed directly.

OK! The program is finished. Very simple, but it seems very practical.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.