Making "wheels" by yourself -- several methods commonly used in python

Keywords: Python JSON network socket

Because of the content of the work, we often need some python scripts. Over time, we found that some methods are often used, so we edited some common, popular and general methods by ourselves. I'm not talented, but I also hope to contribute to open source.

Finally, code HA is attached;

1, At present, only five methods are summarized in this document:

1. ping network (network IP) -- ping test method

2. Socket port (network IP, port) -- a method to detect whether the target network port is open (normal)

3. File format analysis (file path, format) -- a method of parsing json and yaml format files

4. List ﹣ (list ﹣ a, list ﹣ - a way to compare two arrays of data

5. DIC ﹣ contrast (DIC ﹣ a, DIC ﹣ b) --- a method of comparing data differences between two dictionaries

To use this method, you only need to import the wheel_xpt.py file;

2, Method introduction and example

1,ping_network(network_ip)
Use os module in python to test whether the target ip network is reachable, and return true or False;

Example: testing connectivity at www.baidu.com

from wheel_xpt import *

result = ping_network("www.baidu.com")
print(result)

Output results:

Ping www.a.shifen.com [61.135.169.121] with 32 bytes of data:
Reply from 61.135.169.121: byte = 32 time = 7ms TTL=53
 Reply from 61.135.169.121: byte = 32 time = 5ms TTL=53
 Reply from 61.135.169.121: byte = 32 time = 5ms TTL=53
 Reply from 61.135.169.121: byte = 32 time = 5ms TTL=53

Ping statistics of 61.135.169.121:
    Packet: sent = 4, received = 4, lost = 0 (0% lost),
Estimated time of round trip in milliseconds:
    Min = 5ms, max = 7ms, average = 5ms
 The network from this computer to www.baidu.com is smooth;
True

The first is the output of method connection test (annotation code can also cancel it), and the last True is the judgment result of connectivity;

Next, test a network that is not working. This time, use ip directly:

from wheel_xpt import *

result = ping_network("192.168.0.123")
print(result)

Output results:

Ping 192.168.0.123 with 32 bytes of data:
The request timed out.
The request timed out.
The request timed out.
The request timed out.

Ping statistics of 192.168.0.123:
    Packet: sent = 4, received = 0, lost = 4 (100% lost),
The network between this computer and 192.168.0.123 is not available;
False

The result of ping failure returns False.

2,socket_port(network_ip, port)
The socket module in python is used to check whether the ip port is open;

Example: let's check port 80 of csdn

from wheel_xpt import *

result = socket_port("www.csdn.net", "80")
print(result)

Output results:

Detecting the target ip port... Don't panic, wait
 Check www.csdn.net 80 port [normal]
True

If True is returned, the port is open. If False is returned, the target port is not open;

3,file_format_analysis(file_path,format)
Judge whether the file exists, and analyze the json and yaml formats

Example: Here we test and parse two files, a json format and a yaml format

from wheel_xpt import *

#Parsing files in json format
result_json = file_format_analysis(r"D:\my_files\python_test_file\json_yaml_test\role.json","json")
print(result_json)

print("====================================================")

#Parsing files in yaml format
result_yaml = file_format_analysis(r"D:\my_files\python_test_file\json_yaml_test\predata.yml","yaml")
print(result_yaml)

Output results: the data volume of the two files tested may be a little large;

D:\my_files\python_test_file\json_yaml_test\role.json File exists, proceed json Parsing.
{'MachineGroups': {'Machines': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8', 'a56c07003.cloud.c07.amtest8'], 'aarch64': [], 'sw_64': []}, 'ServerRoles': {'OpsClone#': ['a56c07001.cloud.c07.amtest8:10.14.4.1:docker010014004001', 'a56c07002.cloud.c07.amtest8:10.14.4.2:docker010014004002'], 'OpsCloneVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.11:docker010014004011'], 'OpsCloneWeb#': ['a56c07001.cloud.c07.amtest8:10.14.4.3:docker010014004003', 'a56c07002.cloud.c07.amtest8:10.14.4.19:docker010014004019'], 'OpsCloneWebVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.12:docker010014004012'], 'OpsDecider#': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8'], 'OpsDns#': ['a56c07001.cloud.c07.amtest8:10.14.4.5:docker010014004005', 'a56c07002.cloud.c07.amtest8:10.14.4.21:docker010014004021'], 'OpsDnsMaster#': ['a56c07001.cloud.c07.amtest8:10.14.4.6:docker010014004006', 'a56c07002.cloud.c07.amtest8:10.14.4.22:docker010014004022'], 'OpsDnsSlave#': ['a56c07001.cloud.c07.amtest8:10.14.4.7:docker010014004007', 'a56c07002.cloud.c07.amtest8:10.14.4.23:docker010014004023'], 'OpsDnsSlaveVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.8:docker010014004008', 'a56c07002.cloud.c07.amtest8:10.14.4.24:docker010014004024'], 'OpsDnsVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.4:docker010014004004', 'a56c07002.cloud.c07.amtest8:10.14.4.20:docker010014004020'], 'OpsMasterTagAgent#': ['a56c07001.cloud.c07.amtest8'], 'OpsMysql#': ['a56c07001.cloud.c07.amtest8:10.14.4.9:docker010014004009', 'a56c07002.cloud.c07.amtest8:10.14.4.25:docker010014004025'], 'OpsMysqlVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.13:docker010014004013'], 'OpsNtp#': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8'], 'OpsNtpVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.14:docker010014004014'], 'OpsOob#': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8'], 'OpsOobVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.15:docker010014004015'], 'OpsTc#': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8'], 'OpsYum#': ['a56c07001.cloud.c07.amtest8', 'a56c07002.cloud.c07.amtest8'], 'OpsYumVipAgent#': ['a56c07001.cloud.c07.amtest8:10.14.4.16:docker010014004016']}}
====================================================
D:\my_files\python_test_file\json_yaml_test\predata.yml File exists, proceed yaml Parsing.
{'dnsslave': [{'anycastVip': ['10.45.250.1/32', '10.45.250.2/32'], 'anycast_ntp_vips': ['10.45.68.0/32', '10.45.68.1/32'], 'anycast_yum_vips': ['10.45.68.2/32'], 'bgpConnections': [], 'bgpanycastVip': [], 'docker0_ip': '10.45.8.8', 'env': {'idcMap': {'amtest34': 'master'}, 'idc_room': 'amtest34', 'isCenterRegion': True, 'region': 'cn-qingdao-sg-d01', 'zone': 'cn-qingdao-sg-amtest34001-a'}, 'ip': '10.45.1.2', 'master': ['10.45.8.6', '10.45.8.22'], 'ospfConnections': [{'localIp': '10.45.152.222/30', 'localPort': 'eth4', 'localas': '65081', 'ospfArea': '0.0.0.201', 'ospfPassword': 'aliospf', 'protocol': 'ospf', 'remoteIp': '10.45.152.221/30', 'remoteas': '65021'}], 'ospfanycastVip': ['10.45.250.1/32', '10.45.250.2/32', '10.45.68.2/32', '10.45.68.0/32', '10.45.68.1/32'], 'resolv_lines': ['nameserver 10.45.250.1', 'nameserver 10.45.250.2', 'nameserver 10.45.8.21', 'nameserver 10.45.8.5'], 'role': 'slave', 'single_mode': False, 'slave': ['10.45.1.1', '10.45.1.2'], 'slave_vips': ['10.45.8.21', '10.45.8.5'], 'subnets': ['10', '172'], 'vips': [{'ip': '10.45.8.21', 'maskbit': 32, 'name': 'VIP_10.45.8.21', 'priority': '85', 'state': 'BACKUP', 'vip_interface': 'docker0', 'virtual_router_id': '50', 'vrrp_interface': 'docker0'}, {'ip': '10.45.8.5', 'maskbit': 32, 'name': 'VIP_10.45.8.5', 'priority': '90', 'state': 'MASTER', 'vip_interface': 'docker0', 'virtual_router_id': '51', 'vrrp_interface': 'docker0'}]}]}

Directly return the result of parsing. If you want to get a certain value in the JSON and yaml files, you can directly use result_json/yaml to get it;

4,list_contrast(list_a,list_b)
Judge whether the data in two arrays are the same, and output different data and intersection in the array; (the original array will not be damaged)

Example:

from wheel_xpt import *

list_1 = ['a','b','c','d']
list_2 = ['a','b','d','f','456']

result = list_contrast(list_1,list_2)
print(result)

Output results:

{'list_same': ['a', 'b', 'd'], 'list_different_a': ['c'], 'list_different_b': ['f', '456']}

Return a dictionary:

List? Same is the same value of two arrays;

List "different" a is the different value passed in the first array;

List "different" B is the different value passed in the second array;

5,dic_contrast(dic_a,dic_b)
Judge the data in two dictionaries, compare them, and return the same data and different data; (the original dictionary data will not be damaged)

Example: in order to look convenient, the dictionary data used for test comparison is minimized;

from wheel_xpt import *

dic_1 = {
    'a':'AI Xi',
    'b':'Zhao Xin',
    'c':'Jarvan IV',
    'd':'Nasus',
}
dic_2 = {
    'a':'AI Xi',
    'b':'Zhao Xin',
    'd':'De Marcia',
}

result = dic_contrast(dic_1,dic_2)
print(result)

Output results:

{'same': {'a': 'AI Xi', 'b': 'Zhao Xin'}, 'diff_a': {'c': 'Jarvan IV', 'd': 'Nasus'}, 'diff_b': {'d': 'De Marcia'}}

Return a dictionary:

Same is the same data in both dictionaries;

Diff A is different data from the first dictionary;

Diff? B is the different data passed into the second dictionary;

 

Well, I think it's easy to use. Although there are some comparison methods on the Internet, I don't think it's as easy to use what I wrote;

Three, source code

Finally, the source code of "wheel" is attached:

import os
import socket
import json
import yaml

"""Use python Medium os Module test objectives ip Whether the network is reachable, return to Trun or False"""
def ping_network(network_ip):
    #result = os.system("ping %s -w 3 -c 3" % (network_ip))     #Not applicable under windows, - c parameter is blocked;
    result = os.system("ping %s -w 3" % (network_ip))
    if result == 0:
        print("Native to %s Network patency;" % (network_ip))
        return True
    else:
        print("Native to %s Network barrier;" % (network_ip))
        return False

"""Use python Medium socket Module checking ip Whether the port of is open"""
def socket_port(network_ip, port):
    # global socket_port_number  # Define a global parameter to return the connectivity of ip target port: 0 is connectivity, 1 is blocking;

    print("Detecting target ip port......Don't panic. Just wait......")
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, 0)
    try:
        s.connect((network_ip, int(port)))
        s.shutdown(2)
        socket_port_number = 0
    except:
        socket_port_number = 1
        pass
    if socket_port_number == 0:
        print("Testing %s %s port[normal]." % (network_ip, port))
        return True
    else:
        print("Testing %s %s port[Close]." % (network_ip, port))
        return False

"""Determine whether the document exists, and json,yaml Format analysis"""
#Usage: pass in parameters in file format analysis(). File path is the absolute path of the file, and format is two fields [json and yaml].
def file_format_analysis(file_path,format):     #format can only be json, yaml.
    if os.path.exists(file_path) == True:
        if format == "json":
            print("%s File exists, proceed json Parsing." % (file_path))
            with open(file_path, 'rb') as f:
                file_json = json.load(f)
            return file_json
        elif format == "yaml":
            print("%s File exists, proceed yaml Parsing." % (file_path))
            with open(file_path, 'rb') as files:
                file_yaml = yaml.load(files,Loader=yaml.FullLoader)
            return file_yaml
        else:
            print("%s The file exists, only for judgment and no parsing." % (file_path))
            return True
    elif os.path.exists(file_path) == False:
        print("%s The file does not exist." % (file_path))
        return False
    else:
        print("judge %s Exception while file exists." % (file_path))
        exit()

#Judge whether the data in two arrays are the same, and output different data if they are different;
#Return a dictionary -- DIC u list:
#   Dic'lsit ['list'same '] = ["intersection of two arrays"]
#   Dic'lsit ['list'different'a '] = ["different values in list'a"]
#   Dic'lsit ['list'different'b '] = ["different values in list'b"]
def list_contrast(list_a,list_b):
    if len(list_a) == 0 or len(list_b) == 0:    #Determine whether the incoming number is empty
        print("Incoming arrays cannot be empty")
        exit()

    list_1 = list_a[:]
    list_2 = list_b[:]
    dic_list = {}   #Define a dictionary, which stores: 1. The same values of two arrays; 2. Different values of list ﹣ a array; 3. Different values of list ﹣ B array;
    list_same = []    #Define a list to store the same values
    for i in range(0,len(list_1)):  #In the case of the same amount of data, traverse an array,
        if list_1[i] in list_2:
            list_same.append(list_1[i])   #Store the same value in a list;
        else:
            pass
    dic_list['list_same'] = list_same   #Store the same value in the dictionary;

    for i in range(0,len(list_same)):   #Traversing the list containing the intersection of two arrays;
        list_1.remove(list_same[i])     #Delete the same value in list a, leaving different values;
        list_2.remove(list_same[i])     #Delete the same value in List B, leaving different values;
    dic_list['list_different_a'] = list_1   #Store different values in list a in the dictionary
    dic_list['list_different_b'] = list_2   #Store different values in List B in the dictionary

    return dic_list

"""How to judge the contents of two dictionaries"""
#   dic_all["same"] = {intersection of two dictionaries}
#   DIC u all ["diff u a"] = {different key values in the first parameter}
#   DIC u all ["diff u a"] = {different key values in the first parameter}
def dic_contrast(dic_a,dic_b):
    dic_all = {}
    dic_same = {}      #Define a dictionary to store the same key values in two dictionaries;
    dic_diff_a = {}     #Define a dictionary to store different key values in the first parameter;
    dic_diff_b = {}     #Define a dictionary, different key values in the first parameter;

    if len(dic_a) == 0 and len(dic_b) == 0:
        print("Incoming dictionary parameters cannot all be empty")
        exit()

    if len(dic_a) >= len(dic_b):    #Compare the number of two dictionaries, traverse more to compare with less;
        for i in dic_a.keys():      #Traversing the key of DIC a;
            if i in dic_b.keys():   #If the key in DIC UU a exists in DIC UU B
                if dic_a[i] == dic_b[i]:    #Then judge whether the values are equal;
                    dic_same[i] = dic_a[i]
                else:
                    dic_diff_a[i] = dic_a[i]
                    dic_diff_b[i] = dic_b[i]
            else:    #If the key in DIC a does not exist in DIC B
                dic_diff_a[i] = dic_a[i]
    else:
        for i in dic_b.keys():      #Traversing the key of DIC u b;
            if i in dic_a.keys():   #If the key in DIC UU B exists in DIC UU a
                if dic_b[i] == dic_a[i]:    #Then judge whether the values are equal;
                    dic_same[i] = dic_b[i]
                else:
                    dic_diff_a[i] = dic_a[i]
                    dic_diff_b[i] = dic_b[i]
            else:       #If the key in DIC UU B does not exist in DIC UU a
                dic_diff_b[i] = dic_b[i]

    dic_all["same"] = dic_same
    dic_all["diff_a"] = dic_diff_a
    dic_all["diff_b"] = dic_diff_b

    return dic_all

Life is short. I use python.
After that, we will continue to summarize some common methods to add;.

Posted by Nommy on Wed, 25 Dec 2019 01:32:27 -0800