• 2016/09/12


    1. Python作业

      HAproxy配置文件操作 -> 完成

    def fetch(backend):
    	result = []
    	with open('ha.conf', 'r', encoding='utf-8') as f:
    		flag = False
    		for line in f:
    			if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    				flag = True
    				continue
    			if flag and line.strip().startswith('backend'):
    				flag = False
    				break
    			if flag and line.strip():
    				result.append(line.strip())
    	return result
    
    
    def add(backend, record):
    	record_list = fetch(backend)
    	#print(record_list)
    	if not record_list: 	# backend不存在
    		with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    			for line in old:
    				new.write(line)
    			new.write('
    
    backend ' + backend + '
    ')
    			new.write(' ' * 8 + record + '
    ')
    	else:		# backend存在
    		if record in record_list:			# record已经存在
    			import shutil
    			shutil.copy('ha.conf', 'new.conf')
    		else:						# backend存在,record不存在
    			rd_list = record.split()
    			ip_str = ' '.join(rd_list[1:3])
    			#print(ip_str)
    			record_str = ''.join(record_list)
    			#print(record_str)
    			if ip_str not in record_str:						# backend存在,record中sever中ip不存在:
    				#print(record_list)
    				record_list.append(record)
    				with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    					flag = False
    					for line in old:
    						if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    							flag = True
    							new.write('
    ' + line)
    							for new_line in record_list:
    								new.write(' ' * 8 + new_line + '
    ')
    							new.write('
    ')
    							continue
    						if flag == True and line.strip().startswith('backend'):
    							flag = False
    							new.write(line)
    							continue
    						if not flag and line.strip():
    							new.write(line)
    			else:					# backend存在,record中sever中ip存在:
    				#print(record_list)
    				for i in record_list:
    					if ip_str in i:
    						record_list.remove(i)
    				record_list.append(record)
    				with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    					flag = False
    					for line in old:
    						if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    							flag = True
    							new.write('
    ' + line)
    							for new_line in record_list:
    								new.write(' ' * 8 + new_line + '
    ')
    							new.write('
    ')
    							continue
    						if flag == True and line.strip().startswith('backend'):
    							flag = False
    							new.write(line)
    							continue
    						if not flag and line.strip():
    							new.write(line)
    	import shutil
    	shutil.copy('new.conf','ha.conf')
    
    def modify_1(backend, new_backend):			# 仅更改backend
    	record_list = fetch(backend)
    	if not record_list:
    		print('backend not exist')
    	else:
    		with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    			flag = False
    			for line in old:
    				if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    					flag = True
    					new.write('
    backend ' + new_backend + '
    ')
    					for new_line in record_list:
    						new.write(' ' * 8 + new_line + '
    ')
    					new.write('
    ')
    					continue
    				if flag == True and line.strip().startswith('backend'):
    					flag = False
    					new.write(line)
    					continue
    				if not flag and line.strip():
    					new.write(line)
    	shutil.copy('new.conf', 'ha.conf')
    
    
    def modify_2(backend, record, new_backend, new_record):		# 更改backend下的record
    	record_list = fetch(backend)
    	if not record_list:
    		print('backend not exist')
    	elif record not in record_list:
    		print('record not exist')
    	else:
    		for i in record_list:
    			if record == i:
    				record_list.remove(i)
    		record_list.append(new_record)
    		with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    			flag = False
    			for line in old:
    				if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    					flag = True
    					new.write('
    backend ' + new_backend + '
    ')
    					for new_line in record_list:
    						new.write(' ' * 8 + new_line + '
    ')
    					new.write('
    ')
    					continue
    				if flag == True and line.strip().startswith('backend'):
    					flag = False
    					new.write(line)
    					continue
    				if not flag and line.strip():
    					new.write(line)
    	shutil.copy('new.conf', 'ha.conf')
    
    
    def delete(backend, record):
    	record_list = fetch(backend)
    	if not record_list:
    		print('backend not exist')
    	elif record not in record_list:
    		print('record not exist')
    	else:
    		with open('ha.conf', 'r') as old, open('new.conf', 'w') as new:
    			if len(record_list) > 1: 		# 不删backend,只删一项record
    				for i in record_list:
    					if i == record:
    						record_list.remove(i)
    				flag = False
    				for line in old:
    					if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    						flag = True
    						new.write('
    ' + line)
    						for new_line in record_list:
    							new.write(' ' * 8 + new_line + '
    ')
    						new.write('
    ')
    						continue
    					if flag == True and line.strip().startswith('backend'):
    						flag = False
    						new.write(line)
    						continue
    					if not flag and line.strip():
    						new.write(line)
    
    			else:			# backend和record都删
    				for i in record_list:
    					if i == record:
    						record_list.remove(i)
    				flag = False
    				for line in old:
    					if line.strip().startswith('backend') and line.strip() == 'backend ' + backend:
    						flag = True
    						#new.write('
    ' + line)
    						for new_line in record_list:
    							new.write(' ' * 8 + new_line + '
    ')
    						new.write('
    ')
    						continue
    					if flag == True and line.strip().startswith('backend'):
    						flag = False
    						new.write(line)
    						continue
    					if not flag and line.strip():
    						new.write(line)
    	shutil.copy('new.conf', 'ha.conf')
    
    
    # bk = 'www.oldboy.org'
    # bk2 = 'test.oldboy.org'
    # rd = 'server 100.1.7.9 100.1.7.9 weight 20 maxconn 3000'
    # rd2 = 'server 100.1.7.1 100.1.7.1 weight 10 maxconn 1000'
    #
    # modify_2(bk,rd,bk2,rd2)
    
    while(1):
    	import time
    	import shutil
    	shutil.copy('ha.conf', 'ha.backup' + time.strftime('%Y-%m-%d-%H-%M-%S'))
    	choice = int(input('''
    HAproxy配置文件操作:
    1、输入backend,显示对应server信息
    2、添加backend和server信息
    3、修改backend和server信息
    4、删除backend和server信息
    ***************************************
    请输入需要选择的操作编号:'''))
    
    	if choice == 1:
    		bk = input('请输入需查询的backend:')
    		for i in fetch(bk):
    			print(i)
    	elif choice == 2:
    		bk = input('请输入需添加的backend:')
    		rd = input('请输入需添加的server:')
    		add(bk,rd)
    	elif choice == 3:
    		choice2 = input('''
    	1、仅修改backend
    	2、修改backend和server
    	请选择:
    	''')
    		if choice2 == 1:
    			bk = input('请输入修改前的backend:')
    			bk2 = input('请输入修改后的backend:')
    			modify_1(bk, bk2)
    		elif choice2 == 2:
    			bk = input('请输入修改前的backend:')
    			bk2 = input('请输入修改后的backend:')
    			rd = input('请输入修改前的server:')
    			rd2 = input('请输入修改后的server:')
    			modify_2(bk, rd, bk2, rd2)
    		else:
    			print('编号输入错误')
    	elif choice == 4:
    		bk = input('请输入要删除的backend:')
    		rd = input('请输入要删除的server:')
    		delete(bk, rd)
    	else:
    		print('编号输入错误')
    

      

    2. MOOC - 程序设计基础

      第3周:程序设计方法基础-2 -> 完成

        1)raptor制作流程图

          - The Rapid Algorithmic Prototyping Tool for Ordered Reasoning --用于有 序推理的快速算法原型工具 

        2)自底向上的程序设计方法

        3)自顶向下、逐步求精的程序设计方法

        4)结构化程序设计方法

        5)模块化程序设计方法

        6)面向对象程序设计方法

    3. MOOC课表

      

    4. Python视频

      装饰器学习

     

     

  • 相关阅读:
    ORM补充文件
    auth.User.groups: (fields.E304) Reverse accessor for 'User.groups'
    JS BOM和DOM
    ctrl+shift+上箭头 改变代码位置
    盒子半透明
    多背景颜色写图片后面
    心跳
    动画
    仿站小工具哈哈哈
    旋转案例
  • 原文地址:https://www.cnblogs.com/wnzhong/p/5866020.html
Copyright © 2020-2023  润新知