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('\n\nbackend ' + backend + '\n')
			new.write(' ' * 8 + record + '\n')
	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('\n' + line)
							for new_line in record_list:
								new.write(' ' * 8 + new_line + '\n')
							new.write('\n')
							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('\n' + line)
							for new_line in record_list:
								new.write(' ' * 8 + new_line + '\n')
							new.write('\n')
							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('\nbackend ' + new_backend + '\n')
					for new_line in record_list:
						new.write(' ' * 8 + new_line + '\n')
					new.write('\n')
					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('\nbackend ' + new_backend + '\n')
					for new_line in record_list:
						new.write(' ' * 8 + new_line + '\n')
					new.write('\n')
					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('\n' + line)
						for new_line in record_list:
							new.write(' ' * 8 + new_line + '\n')
						new.write('\n')
						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('\n' + line)
						for new_line in record_list:
							new.write(' ' * 8 + new_line + '\n')
						new.write('\n')
						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视频

  装饰器学习

 

 

posted on 2016-09-12 19:45  你的踏板车要滑向哪里  阅读(163)  评论(0编辑  收藏  举报

导航