python

1,本机python2.7升级到3.6

一般老的虚机安装时python默认时2.7版本,需要升级

]# which python
/usr/bin/python

ls -lah /usr/bin/python
lrwxrwxrwx. 1 root root 7 Apr 22 2020 /usr/bin/python -> python2

安装python3.6后:

ls -lah /usr/bin/ |grep python
lrwxrwxrwx. 1 root root 7 Apr 22 2020 python -> python2
lrwxrwxrwx. 1 root root 9 Apr 22 2020 python2 -> python2.7
-rwxr-xr-x. 1 root root 7.1K Oct 31 2018 python2.7
lrwxrwxrwx. 1 root root 9 Feb 10 16:46 python3 -> python3.6
-rwxr-xr-x. 2 root root 12K Nov 17 2020 python3.6
-rwxr-xr-x. 2 root root 12K Nov 17 2020 python3.6m

升级为python3.6过程:

 yum install epel-release
yum install python36
cd /usr/bin/
删除老软连接,创建新软连接
rm /usr/bin/python
ln -s /usr/bin/python3.6 /usr/bin/python
python --version


-------------------------------------------------------------------------------
python 打印数组所有元素:
print(*a,sep = '\n')

python一维数组切分成二维数组:使用numpy库
tables为待转换的一维数组,reshape参数,第一个表述列数,第二个是所有元素除以列数算出来的,换句话说转换完,数组元素需要一致。reshape(列数,行数)
    tables_thread=numpy.array(tables).reshape(2,int(len(tables)/2))

---------------------------------------------------------------------------------
python多进程
import time
import random
from multiprocessing import Process


class Run(Process):
    def __init__(self,name):
        super().__init__()
        self.name=name
    def run(self):
        print('%s runing' %self.name)
        time.sleep(random.randrange(1,5))
        print('%s runing end' %self.name)

p1=Run('anne')
p2=Run('alex')
p3=Run('ab')
p4=Run('hey')
p1.start() #start会自动调用run
p2.start()
p3.start()
p4.start()
p1.join() #等待p1进程停止
p2.join()
p3.join()
p4.join()
print('主线程')

#注意上面的代码是主进程等待子进程,等待的是主进程,所以等待的总时间是子进程中耗费时间最长的那个进程运行的时间

#上述启动进程与join进程可以简写为
# p_l=[p1,p2,p3,p4]
# 
# for p in p_l:
#     p.start()
# 
# for p in p_l:
#     p.join()

-----------------------------------------------------------------------------
python获取ansible 非playbook情况下的返回值:
比如
ansible windows -m win_shell -a 'certutil -hashfile D:\\\\v0.0.0\\\\app\\\\csscand.exe  MD5' 
标准输出打印:

1.1.1.1 | CHANGED | rc=0 >>
MD5 ��ϣ(�ļ� D:\\v0.0.0\\app\\csscand.exe):
b2 63 72 db  92 21 2d 32 5d 8f 20
CertUtil: -hashfile ����ɹ���ɡ�

2.2.2.2 | CHANGED | rc=0 >>
MD5 ��ϣ(�ļ� D:\\v0.0.0\\app\\csscand.exe):
b2 63 72 db  92 21 2d 32 5d 8f 20
CertUtil: -hashfile ����ɹ���ɡ�

3.3.3.3 | CHANGED | rc=0 >>
MD5 ��ϣ(�ļ� D:\\v0.0.0\\app\\csscand.exe):
b2 63 72 db  92 21 2d 32 5d 8f 2000000000
CertUtil: -hashfile ����ɹ���ɡ�

想要获取各个文件的md5,用来在程序中判断是否每个文件的md5是一致的,也就是同一个文件。

那么就要把输出获取到,并存入变量。对于playbook的情况下,是有return value 注入变量提供的。

而对于非playbook的情况下,可以先把输出存到临时文件,然后从临时文件提取,并且因为文件输出

的规律性巧妙的应用取模操作,获取到输出信息,代码如下:

import os

ansible_windows_hashfile_csscand = "b2 63 72 db  92 21 2d 32 5d 8f 20"

os.system("ansible windows -m win_shell -a 'certutil -hashfile D:\\\\v0.0.0\\\\app\\\\csscand.exe MD5' > ansible_windows_hashfile_csscand.out")

i = 1
with open("ansible_windows_hashfile_csscand.out", 'rb') as fp:
    for line in fp.readlines():
        if i % 5 == 1:
            line_arr = line.split("|")
        if i % 5 == 3:
            if line.strip() != ansible_windows_hashfile_csscand:
                  print(line)
                  print(ansible_windows_hashfile_csscand)
                  print(line_arr[0])
        i = i + 1

--------------------------------------------------------------------------------
python mysql mongo开发环境
pip3 install mysql 这个需要:
yum install mysql-devel gcc gcc-devel python3-devel
yum install zlib-devel bzip2-devel openssl-devel ncurses-devel zx-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel python-devel
总之,所有依赖全装上
注意改成python3 后 yum使用的话,需要切换回python2
pip3 install mysql-connector
因为我现在用的mongodb版本是3.4 ,所以需要低版本的pymongo
pip指定版本安装:
pip install scipy==0.15.1
使用两个等号


python的隔离性,或者说Linux多用户的隔离性?
现象是a用户可以正常操作ansible,切换b用户登陆后,就提示缺少pywinrm包,b用户用pip装上这个包,就也能使用ansible访问Windows了。

--------------------------------------------------------------------------
python库:
shutil库 ,可以递归删除非空文件
shutil.rmtree(path)

git模块
pip install gitpython

configparser:
https://docs.python.org/zh-cn/3.11/library/configparser.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值