• day6随笔


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

    ##生成器
    #########################################################################################################
    import time
    #导入time模块,用于给生产者生成包子定义时间,比如银行处理完成之后告诉用户信息
    def consumer(name):
    #消费者模块
    print("准备吃包子了%s!...."%name)
    while True:
    #循环,死循环,可以使用yield来处理,并且保持在某一个状态
    baozi = yield
    #使用yield来接收send()传递过来的参数,yield与send()结合使用
    print("包子[%s]来了,被[%s]吃了!" %(baozi,name))

    def producer(name):
    #定义生成这模型
    c = consumer("A")
    #消费者来买包子
    c2 = consumer("B")
    c.__next__()
    #调用consumer()函数
    c2.__next__()
    print("老子开始做包子啦!...")
    for i in range(1,10):
    time.sleep(1)
    #生产者每秒钟做两个包子
    print("做了2个包子!")
    c.send(i)
    #把生产者生成的包子传递给yield
    c2.send(i)

    producer("alex")
    # # consumer('a')
    # next(consumer('a'))
    #####################################################################################################

    #############os模块##################################################################################
    import os
    os.path.abspath(path) #返回绝对路径
    os.path.basename(path) #返回文件名
    os.path.commonprefix(list) #返回list(多个路径)中,所有path共有的最长的路径。
    os.path.dirname(path) #返回文件路径
    os.path.exists(path) #路径存在则返回True,路径损坏返回False
    os.path.lexists #路径存在则返回True,路径损坏也返回True
    os.path.expanduser(path) #把path中包含的"~"和"~user"转换成用户目录
    os.path.expandvars(path) #根据环境变量的值替换path中包含的”$name”和”${name}”
    os.path.getatime(path) #返回最后一次进入此path的时间。
    os.path.getmtime(path) #返回在此path下最后一次修改的时间。
    os.path.getctime(path) #返回path的大小
    os.path.getsize(path) #返回文件大小,如果文件不存在就返回错误
    os.path.isabs(path) #判断是否为绝对路径
    os.path.isfile(path) #判断路径是否为文件
    os.path.isdir(path) #判断路径是否为目录
    os.path.islink(path) #判断路径是否为链接
    os.path.ismount(path) #判断路径是否为挂载点()
    os.path.join(path1[, path2[, ...]]) #把目录和文件名合成一个路径
    os.path.normcase(path) #转换path的大小写和斜杠
    os.path.normpath(path) #规范path字符串形式
    os.path.realpath(path) #返回path的真实路径
    os.path.relpath(path[, start]) #从start开始计算相对路径
    os.path.samefile(path1, path2) #判断目录或文件是否相同
    os.path.sameopenfile(fp1, fp2) #判断fp1和fp2是否指向同一文件
    os.path.samestat(stat1, stat2) #判断stat tuple stat1和stat2是否指向同一个文件
    os.path.split(path) #把路径分割成dirname和basename,返回一个元组
    os.path.splitdrive(path) #一般用在windows下,返回驱动器名和路径组成的元组
    os.path.splitext(path) #分割路径,返回路径名和文件扩展名的元组
    os.path.splitunc(path) #把路径分割为加载点与文件
    os.path.walk(path, visit, arg) #遍历path,进入每个目录都调用visit函数,visit函数必须有
    3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有
    文件名,args则为walk的第三个参数
    os.path.supports_unicode_filenames #设置是否支持unicode路径名
    #sys.path是以这个函数所在文件位置为基准的
    pre_path = os.path.abspath('\day6\test\')
    print(sys.path.append(pre_path))
    # print(test)
    #os.makedirs('test')
    #print(os.path.abspath('..\day6\'))
    print(os.path.exists('H:python17day6\test'))
    ############################################################################################################
    ######################################hashlib###############################################################
    import hashlib

    # hashtest = hashlib.md5()
    # hashtest.update('admin')
    # print(hashtest.hexdigest())

    # test = hashlib.sha1()
    # test.update('admin')
    # print(test.hexdigest())

    # hash = hashlib.md5()
    # hash.update('admin')
    # print(hash.hexdigest())
    # import hmac
    # h = hmac.new('Bruce')
    # h.update('test')
    # print(h.hexdigest())

    # import hashlib
    # m=hashlib.md5()
    # m.update('hello'.encode('utf8'))
    # print(m.hexdigest())
    # m.update('alvin'.encode('utf8'))
    # print(m.hexdigest())
    # m2=hashlib.md5()
    # m2.update('helloalvin'.encode('utf8'))
    # print(m2.hexdigest())
    # import hashlib
    #
    # hash = hashlib.sha256('898oaFs09f'.encode('utf8'))
    # hash.update('alvin'.encode('utf8'))
    # print (hash.hexdigest())

    # import hashlib
    # passwds=[
    # 'alex3714',
    # 'alex1313',
    # 'alex94139413',
    # 'alex123456',
    # '123456alex',
    # 'a123lex',
    # ]
    # def make_passwd_dic(passwds):
    # dic={}
    # for passwd in passwds:
    # m=hashlib.md5()
    # m.update(passwd.encode('utf-8'))
    # dic[passwd]=m.hexdigest()
    # return dic
    #
    # def break_code(cryptograph,passwd_dic):
    # for k,v in passwd_dic.items():
    # if v == cryptograph:
    # print('密码是===>33[46m%s33[0m' %k)
    #
    # cryptograph='aee949757a2e698417463d47acac93df'
    # break_code(cryptograph,make_passwd_dic(passwds))


    ###########################paramiko#######################################################
    import paramiko

    ssh = paramiko.SSHClient()
    ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
    ssh.connect('192.168.92.171', 22, 'root', '123.com')
    stdin, stdout, stderr = ssh.exec_command('df')
    print(stdout.read())
    ssh.close()


    ########################################logging#############################################################
    import logging
    简单将日志打印到屏幕:
    [python]
    view
    plain
    copy
    import logging

    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    输出:
    WARNING: root:warning
    message
    ERROR: root:error
    message
    CRITICAL: root:critical
    message
    可见,默认情况下Python的logging模块将日志打印到了标准输出中,且只显示了大于等于WARNING级别的日志,这说明默认的日志级别设置为WARNING(日志级别等级CRITICAL > ERROR > WARNING > INFO > DEBUG > NOTSET),默认的日志格式为日志级别:Logger名称:用户输出消息。

    灵活配置日志级别,日志格式,输出位置
    [python]
    view
    plain
    copy
    import logging

    logging.basicConfig(level=logging.DEBUG,
    format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
    datefmt='%a, %d %b %Y %H:%M:%S',
    filename='/tmp/test.log',
    filemode='w')

    logging.debug('debug message')
    logging.info('info message')
    logging.warning('warning message')
    logging.error('error message')
    logging.critical('critical message')
    查看输出:
    cat /tmp/test.log
    Mon, 05 May 2014 16:29:53 test_logging.py[line:9] DEBUG debug message
    Mon, 05 May 2014 16:29:53 test_logging.py[line:10] INFO info message
    Mon, 05 May 2014 16:29:53 test_logging.py[line:11] WARNING warning message
    Mon, 05 May 2014 16:29:53 test_logging.py[line:12] ERROR error message
    Mon, 05 May 2014 16:29:53 test_logging.py[line:13] CRITICAL critical message

    可见在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
    filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
    filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
    format:指定handler使用的日志显示格式。
    datefmt:指定日期时间格式。
    level:设置rootlogger(后边会讲解具体概念)的日志级别
    stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。

    format参数中可能用到的格式化串:
    %(name)s Logger的名字
    %(levelno)s 数字形式的日志级别
    %(levelname)s 文本形式的日志级别
    %(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
    %(filename)s 调用日志输出函数的模块的文件名
    %(module)s 调用日志输出函数的模块名
    %(funcName)s 调用日志输出函数的函数名
    %(lineno)d 调用日志输出函数的语句所在的代码行
    %(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
    %(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
    %(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
    %(thread)d 线程ID。可能没有
    %(threadName)s 线程名。可能没有
    %(process)d 进程ID。可能没有
    %(message)s用户输出的消息

    ########################################logging#############################################################

    #####################################json####################################################################
    import json
    dic={'name':'alvin','age':23,'sex':'male'}
    print(type(dic))
    j=json.dumps(dic)
    print(type(j),j)
    with open('test.json','w') as f:
    f.write(j)
    ##########################################json####################################################################

    ############################3面向对象################################################################
    从一组对象中提取相似的部分就是类,类所有对象都具有的特征和技能的结合体
    在python中,用变量表示特征,用函数表示技能,因而类是变量与函数的结合体,对象是变量与方法(指向类的函数)的结合体
    类有两种作用:属性引用和实例化
    1 属性引用(类名.属性)
    >>> Garen.camp #引用类的数据属性,该属性与所有对象/实例共享
    'Demacia'
    >>> Garen.attack #引用类的函数属性,该属性也共享
    <function Garen.attack at 0x101356510>
    >>> Garen.name='Garen' #增加属性
    >>> del Garen.name #删除属性
    2 实例化(__init__与self)
    类名加括号就是实例化,会自动触发__init__函数的运行,可以用它来为每个实例定制自己的特征
    class Garen: #定义英雄盖伦的类,不同的玩家可以用它实例出自己英雄;
    camp='Demacia' #所有玩家的英雄(盖伦)的阵营都是Demacia;
    def __init__(self,nickname,aggressivity=58,life_value=455): #英雄的初始攻击力58...;
    self.nickname=nickname #为自己的盖伦起个别名;
    self.aggressivity=aggressivity #英雄都有自己的攻击力;
    self.life_value=life_value #英雄都有自己的生命值;
    def attack(self,enemy): #普通攻击技能,enemy是敌人;
    enemy.life_value-=self.aggressivity #根据自己的攻击力,攻击敌人就减掉敌人的生命值。
    实例化
    >>> g1=Garen('草丛伦') #就是在执行Garen.__init__(g1,'草丛伦'),然后执行__init__内的代码g1.nickname=‘草丛伦’等

    类名称空间与对象/实例名称空间
    创建一个类就会创建一个类的名称空间,用来存储类中定义的所有名字,这些名字称为类的属性
    而类有两种属性:数据属性和函数属性
    其中类的数据属性是共享给所有对象的
    #################################################################################3
  • 相关阅读:
    Android 如何打造Android自定义的下拉列表框控件
    Andoid 更好的Android多线程下载框架
    apache-storm-1.0.2.tar.gz的集群搭建(3节点)(图文详解)(非HA和HA)
    访问Storm ui界面,出现org.apache.thrift7.transport.TTransportException: java.net.ConnectException: Connection refused的问题解决(图文详解)
    apache-storm-0.9.6.tar.gz的集群搭建(3节点)(图文详解)
    MetaSploit攻击实例讲解------工具Meterpreter常用功能介绍(kali linux 2016.2(rolling))(详细)
    [-] Failed to load plugin from /usr/share/metasploit-framework/plugins/db_autopwn: No classes were loaded from /usr/share/metasploit-framework/plugins/db_autopwn in the Msf::Plugin namespace.
    Kali linux 2016.2(Rolling)里Metasploit连接(包括默认和自定义)的PostgreSQL数据库之后的切换到指定的工作空间
    MetaSploit攻击实例讲解------Metasploit自动化攻击(包括kali linux 2016.2(rolling) 和 BT5)
    Kali linux 2016.2(Rolling)里Metasploit连接(包括默认和自定义)的PostgreSQL数据库
  • 原文地址:https://www.cnblogs.com/Bruce-yin/p/6972090.html
Copyright © 2020-2023  润新知