Modifying the Haproxy configuration file with Python

Source: Internet
Author: User
Tags haproxy

Demand:

When the user enters the domain name, the following record is displayed

When the user needs to enter a record, add it to the domain you need

    GlobalLog127.0.0.1local2 Daemon Maxconn256Log127.0.0.1Local2 Info Defaults logGlobalmode HTTP Timeout connect 5000ms timeout client 50000ms timeout s erver 50000ms Option Dontlognull Listen 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.orgifwww backend www.oldboy.org server100.1.7.9 100.1.7.9 Weight Maxconn 3000Server100.1.7.92 100.1.7.93 Weight Maxconn 3000Server100.1.7.94 100.1.7.93 Weight Maxconn 3000Backend buy.oldboy.org Server100.1.7.90 100.1.7.90 Weight Maxconn 3000
configuration file
1 #! /usr/bin/env python2 #-*-coding:utf-8-*-3 4 defSelect (ARG):5     """6 require the user to enter a domain name, you can view the webserver of the sub-scope7 configuration files for Haproxy8 Ideas:9 A. Reading a file row by lineTen B. When I encounter the line of the backend + domain name, place its sub-scopes in an empty list to show the user One c. And then when I meet the beginning of the backend, it's OK to end the loop . A D. Because this sub-scope is required to be displayed, it should be taken out, so to be particularly visible, to be different, so we define a sign here to show this difference - :p Aram Arg: - : return: the     """ -result = [] -Flag =False -With open ("Haproxy.txt","R", encoding="Utf-8") as F: +          forLineinchF: -             ifLine.strip () = ="Backend%s"%ARG: +Flag =True A                 Continue at             ifLine.strip (). StartsWith ("Backend"): -Flag =False -             ifflag: - Result.append (line) -     returnresult -RET = SELECT ("www.oldboy.org") in  -  forIinchret: to     Print(i) + #++++++++++++++++++++++++++++ - defSele_add (yuming, record): the     """ * Increase: $ A. First check if there is no this domain name, if not added at the end can bePanax Notoginseng B. If so, in the detection of having the same sub-scope as you, - C. If there is, pass, if not on the last line to add the  + method One: A A. Querying the webserver domain name is the domain name that the user wants the B. Add a new record of the query to the newly-written file + C. Continue to write unexpected scopes and other content in addition to this domain name to the new file - Method Two: $ A. is to join the user needs to join a new record before the next domain name $ #也可以理解为先插入那行在写backend - B. In excluding whether it already exists - c. When the pseudo-code has been written, must not be implemented, so we have to modify the code, then we need to use the flag bit because only after the current domain name of the first backend the before adding a record, so the flag bit will need to be changed to a number when marking - :p Aram Arg:Wuyi : return: the     """ -  Wu     """ - Biaozhiwei = False About With open ("Haproxy.txt", "R", encoding= "Utf-8") as-old, open ("Haproxy2.txt", "W", encoding= "Utf-8") as New: $ res_list = Select (yuming) - res_list.append ("*8 + record +" \ n ") - For-line-in-old : - if line.strip () = = "Backend%s"% yuming: A Biaozhiwei = true# Note + New.write (line) the For i in Res_list: - New.write (i) $  the Continue the if Line.strip (). StartsWith ("backend"): the Biaozhiwei = False the if not Biaozhiwei: - New.write (line) in     """ the #No.2 the #Method Two: About #A. is to join the user needs to join a new record before the next domain name the #It can also be understood as inserting the line first in writing backend the #B. In excluding whether it already exists the #---------------- + #determine if the domain name exists, if there is a second to determine whether the record exists, if there is, pass, if not exist on the add -BZW = 2 theresult = []BayiA =0 theWith open ("Haproxy.txt","R", encoding="Utf-8") as old, open ("Haproxy2.txt","W", encoding="Utf-8") as NEW: the         if("Backend%s"% yuming)inchOld : -             Print("Cunz") -  the              forLineinchOld : the  the                 ifLine.strip () = ="Backend%s"%yuming: theBZW =0 - Result.append (line) the New.write (line) the                     Continue the                 ifLine.strip (). StartsWith ("Backend"):94BZW + = 1#because we're going to meet a lot of backend, so we're adding 1. the                     ifBZW = = 1: the                         if(" "*8 + record +"\ n")inchResult: the                             Pass98                         Else: AboutNew.write (" "*8 + record +"\ n") - New.write (line)101         Else:102             Print("Bucz")103With open ("Haproxy.txt","R", encoding="Utf-8") as old, open ("Haproxy2.txt","W", encoding="Utf-8") as NEW:104                  forLineinchOld : the New.write (line)106New.write ("\nbackend%s\n"%yuming)107New.write (" "*8 + record +"\ n")108 109Sele_add ("WWW.OLDBOY.ORGT","1233446")

Modifying the Haproxy configuration file with Python

Related Article

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.