Python Learning the first module Haproxy configuration operation

Source: Internet
Author: User

Shopping Cart Simulation

Demand:

    • According to the user input and output corresponding to the backend
    • The server information can be added backend and
    • Sever information to delete backend and sever information
    • When you add server information, the information is duplicated with the existing information and does not operate

Description

    • Python version is 3.6
    • The results of additions and deletions can be verified within haproxy.conf

haproxy.conf configuration file Contents:

GlobalLog127.0.0.1local2 Daemon Maxconn256Log127.0.0.1local2 infodefaults LogGlobalmode HTTP Timeout connect 5000ms timeout client 50000ms timeout server 50000ms opt Ion Dontlognulllisten Stats:8888stats enable stats URI/Admin Stats Auth admin:1234frontend oldboy.org Bind0.0.0.0:80option httplog option httpclose option forwardfor logGlobalACL www hdr_reg (host)-i www.oldboy.org use_backend www.oldboy.orgifwwwbackend www.oldboy.org Server100.1.7.9 Weight Maxconn 3000Backend www.qq.com Server100.1.7.10 Weight Maxconn 3000Backend www.cnblog.com Server100.1.7.14 Weight Maxconn 3000

Code:

#/usr/bin/env python#-*-coding:utf-8-*-# author:fioimport Osimport jsonarg = {' backend ': ' www.yahoo.com ', ' rec Ord ': {' IP ': ' 100.1.7.13 ', ' weight ': ' a ', ' maxconn ': '}}def ' select_info (url): flag = Fa LSE with Open ('./haproxy.conf ', ' r ') as Conf:for line in Conf:if line.strip (). startswith ("backend% s "% url"): flag = True Continue if Flag:record_info = Line.strip (  Flag = False Print (record_info) def insert_info (info): server_info = eval (info) url = server_info[' backend ' IP = server_info[' record ' [' IP '] weight = server_info[' record ' [' weight '] maxconn = Serve            r_info[' record ' [' maxconn '] flag = False with open ('./haproxy.conf ', ' r ') as Conf1:for line in Conf1:                If Line.strip (). startswith (' backend%s '% url): print ("config already Exists") flag = False Break ElsE:flag = True Continue if Flag:with open ('./haproxy.conf ', ' a ') as Conf2:  Conf2.write (' backend ' + URL + ' \ n ') conf2.write (' server%s weight%s maxconn%s\n '% (ip, weight,  MAXCONN)) print ("config%s Insert succeeded"% url) def delete_info (url): flag = False with open ('./haproxy.conf ', ' r+ ') as Conf1:with Open ('./haproxy_tmp.conf ', ' W ') as Conf2:for line1 in Conf1:if Line1.strip (). startswith (' backend%s '% url): conf2.write ("") flag = True Co                    Ntinue elif flag and Line1.strip (). startswith (' server '): conf2.write ("")                    Flag = False Continue Else:conf2.write (line1)  Continue Os.remove ('./haproxy.conf ') os.rename ('./haproxy_tmp.conf ', './haproxy.conf ') print (' backend%s content deleted '% Url) if __name__ = = "__main__ ": Print (" ############################### # Please choose how To: # 1.  Query # 2.  Insert # 3. Delete ############################### ') order_num = input ("selected Ordinal:") if order_num = = ' 1 ': URL = input ("please Select the domain name to query: ") select_info (URL) elif order_num = = ' 2 ': info = input (" "enter The complete configuration, sample format ({' backend ': ' Www.ya hoo.com ', ' record ': {' IP ': ' 100.1.7.13 ', ' weight ': ' + ', ' maxconn ': ' + '}} '): ins Ert_info (info) elif Order_num = = ' 3 ': url = input ("please Enter the domain name to be Deleted:") delete_info (url) else:print ( "please Enter the correct serial number!" ")

  

Python Learning the first module Haproxy configuration operation

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.