分类标签归档:python

mac 下安装pyenv


1、安装pyenv

brew  install pyenv

2、添加环境变量PYENV_ROOT

# 修改 ~/.bash_profile  添加
export PYENV_ROOT=/usr/local/var/pyenv
if which pyenv > /dev/null; then eval "$(pyenv init -)"; fi

3、安装 python

pyenv install 3.6.2

如果上面安装很慢可以将文件包下载下来,放到 $PYENV_ROOT/cache 目录中(提前创建好)

mkdir -p $PYENV_ROOT/cache
cd $PYENV_RO

Read more

python 操作elasticsearch


1、安装

pip install elastisearch

2、连接elasticsearch

from elasticsearch import Elasticsearch

es = Elasticsearch(hosts="192.168.28.25", port=9200, timeout=200)

3、创建索引

from elasticsearch import Elasticsearch
from datetime import datetime


class MyElasticsearch(object):
    """本

Read more

python 操作excel


  1. 安装
pip install xlwt  # xlwt库负责将数据导入生成Excel表格文件.
pip install xlrd  # xlrd库则负责将Excel表格中的数据取出来。
  1. 使用样例
import xlwt
from datetime import datetime


def excel():
    wb = xlwt.Workbook()
    # 添加一个表
    ws = wb.add_sheet('cpsapi.lejunwl.com访问异常ip')

    now_time = datetime.now().strftime('

Read more

python3.8 调用ansible 2.9.4 api


1.安装ansible相关模块

pip3 install ansible_runner
pip3 install ansible_inventory
pip3 install ansible_playbook

2、重写官方的回调函数并根据官方的example 封装一个类

ansible2.py

import json
import shutil
from ansible.module_utils.common.collections import ImmutableDict  # 用于添加选项。比如: 指定远程用户remote_user=None
from ansible.parsing.d

Read more

python 常用系统模块


1. os.system

如果执行成功,那么会返回0,表示命令执行成功。

否则,则是执行错误,有以下几种错误分类:

"OS error code   1:  Operation not permitted"
 "OS error code   2:  No such file or directory"
 "OS error code   3:  No such process"
 "OS error code   4:  Interrupted system call"
 "OS error code

Read more

python 调用ansible 接口


获取组或者主机

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import sys
from collections import namedtuple
# 核心类
# 用于读取YAML和JSON格式的文件
from ansible.parsing.dataloader import DataLoader
# 用于存储各类变量信息
from ansible.vars.manager import VariableManager
# 用于导入资产文件
from ansible.inventory.manager import InventoryMa

Read more