![](https://img-blog.csdnimg.cn/20201014180756927.png?x-oss-process=image/resize,m_fixed,h_64,w_64)
python
木村戒奶
这个作者很懒,什么都没留下…
展开
-
(2022) netmiko - 2.批量登录交换机(加强版)
實現同時登入多台設備,提升效率!原创 2022-06-02 10:05:25 · 596 阅读 · 1 评论 -
好用的线程池ThreadPoolExecutor
from concurrent.futures import ThreadPoolExecutor, as_completedimport timedef myfunc(ip): print(f"{ip} start.") time.sleep(1) print(f"{ip} done.") return f"{ip} done."if __name__ == '__main__': ip_list = [ f"10.99.8.{host}"原创 2021-09-11 18:09:48 · 146 阅读 · 0 评论 -
logging的用法(Python)
使用logging來記錄日志import logginglog1 = logging.getLogger('MAN01')log1.setLevel(logging.DEBUG)fmt1 = logging.Formatter( '%(asctime)s [%(levelname)-8s] (%(name)s): %(msg)s', # 輸出格式 datefmt='%Y/%m/%d %H:%M:%S' # 日期格式)# 定義文件輸出fh_a = logging.File原创 2021-05-14 15:59:23 · 101 阅读 · 0 评论 -
Python多线程——信号量
import threadingimport timeimport randomdef run(n): semaphore.acquire() sleep_time = random.randint(1,30) print("run the thread:%s, sleep: %ss" % (n, sleep_time)) time.sleep(sleep_time) semaphore.release()if __name__ == .原创 2021-04-08 21:46:02 · 84 阅读 · 0 评论 -
Python之socket初探
效果:server和client建立对话,client发送字符串,server接收后返回大写字符串。server端代码如下:import socketserver = socket.socket()server.bind(('localhost', 6969)) #绑定监听端口server.listen()print("Start to listening...")conn,addr = server.accept() #等待连接print(conn, addr)data原创 2021-03-08 11:36:03 · 112 阅读 · 1 评论 -
python最简单的字符串转字典(str 2 dict)
代码如下:a = '''{'name': 'xiaoming', 'score':{'chinese': 95,'english': 93,'math': 98} }'''b = eval(a)print(type(a), a)print(type(b), b)运行如下:<class 'str'> {'name': 'xiaoming', 'score':{'chinese': 95,'english': 93,'math': 98} }<class 'dict'&原创 2021-03-04 17:37:04 · 582 阅读 · 1 评论 -
网络管理自动化 - nornir 3.0 入门(1)
1. 基础配置目录结构如下:.|-- config.yaml #配置文件,定义设备库的路径,线程等|-- inventory| |-- defaults.yaml #所有设备默认配置| |-- groups.yaml #组内设备共有配置| `-- hosts.yaml #单个设备配置|-- main.py #运行文件config.yaml 内容如下:---inventory: plugin: SimpleInventory ...原创 2021-03-01 19:34:15 · 1273 阅读 · 0 评论 -
python装饰器
python装饰器的运用,先写一个函数(CalculaTime),里面再生成一个函数(wrapper)名字可随意,*args和**kwargs把参数给传进去,func即你要包裹的函数,在不修改原有函数的情况下,添加一下功能(例如写log等),然后再返回这个生成的函数(wrapper)。@语法糖,添加在原有函数的头部,意义为用@后面的函数(calculateTime)来包裹下面的函数(sayHello,sayMyname),等价于以下两行:f1 = calculateTime(sayHello)f原创 2021-01-23 13:08:54 · 66 阅读 · 0 评论 -
多设备和服务器通过python实现批量自动化下载
需求:有80台设备,2台服务器,每台服务器只允许同时5个连接。import queueimport threading, timedef rest(q_c, q_s): while not q_c.empty(): #检测客户端的序列是否已经为空 q_client = q_c.get() #提取客户端 q_server = q_s.get() #提取可用的服务器 print('%s start to download from %s.原创 2020-11-24 15:14:52 · 155 阅读 · 0 评论 -
python字典筛选
统计高于平均分的同学student = { '01':{'name':'xiaoming', 'score':93}, '02':{'name':'xiaohong', 'score':96}, '03':{'name':'liming', 'score':78}, '04':{'name':'zhangsan', 'score':88},}total = 0for stu in student.values(): total += stu['scor原创 2020-11-23 21:22:48 · 1385 阅读 · 0 评论 -
批量更改华为交换机配置 by Python
模拟场景和需求:有100台华为的S5700交换机已经在正常运作,通过SSH连接,用户名和密码一致。由于业务需要,统一更改部分接口的配置。目录结构:set_commands.py # 执行文件cmds.txt # 需要更改的配置命令文件内容:set_commands.py:from netmiko import ConnectHandlerfrom netmiko.ssh_exception import NetMikoAuthenticationException...原创 2020-11-19 14:50:34 · 1844 阅读 · 1 评论 -
python常用格式化字符串方法
coding中经常会使用到格式化字符串,总结一下各种方法:(python version >3.5)first_name = "Ben"last_name = "Becker"full_name1 = f"{first_name} {last_name}" #f代表format的简写>>> 'Ben Becker'full_name2 = "{fname} {lname}".format(fname=first_name, lname=last_name)>.原创 2020-11-12 11:17:23 · 120 阅读 · 0 评论 -
華爲S5700交換機自動化升級固件
#!/usr/bin/python3# -*- coding: utf-8 -*-from netmiko import ConnectHandlerfrom netmiko.ssh_exception import NetMikoAuthenticationException, NetMikoTimeoutExceptionimport threadingimport timeimport datetimeimport queuedef conn_info(ip)...原创 2020-08-10 22:26:52 · 1117 阅读 · 0 评论 -
python利用queue对多线程做并发数量控制
原理是:开启20个thread,每一个thread会从queue中取走一个参数然后做相应的操作,运作一遍后,检查queue中还有没有参数,有的话继续操作,没有的话就退出thread。简单点就是:queue里面坐满了顾客,我同时开了20个柜台(threading)来处理顾客需求,1个柜台处理完一个顾客后,假如还有顾客在等待就继续处理,没有的话就关闭柜台,直到全部的顾客都处理完毕。import queueimport threading, timedef rest(que): while原创 2020-08-01 23:01:52 · 1674 阅读 · 0 评论 -
Python GUI(Tkinter)初探
Python version: 3.7.0效果:代码:import tkinter as tkdef openPath(): print("aaa")frm = tk.Tk()frm.title('Auto Rename File Tool V1.0')frm.geometry("380x80")label1 = tk.Label(frm,text='Fi...原创 2019-06-14 18:35:47 · 166 阅读 · 0 评论 -
Python批量修改文件名(删除指定关键字)
因下载的视频文件大多数含有视频网站的url或者包含其他不要的字符串,用python自动修改。目前缺点:1,需要把.py放在目录内运行代码如下:import os, rewhile True: keyword = input("请输入你要删除的字符串:") if len(keyword)==0 or keyword.isspace(): ...原创 2019-06-14 18:15:56 · 2371 阅读 · 2 评论 -
netmiko自动获取extreme交换机信息
目的:通过netmiko实现多线程连接extreme switch,获取model,sn,version等信息。说明:目录下建立一个devices.txt文件,每行一个设备信息,格式:ipaddress username password(中间用1个空格分隔)代码如下:import re, threadingfrom netmiko import ConnectHandlers...原创 2019-06-19 12:07:19 · 843 阅读 · 0 评论