python基础——内置模块2(sys,加密,时间)

一,sys模块
(1)argv :返回脚本名称和执行脚本时传递的参数
示例:
建立一个py文件
在这里插入图片描述

代码
import sys
print(sys.argv)
运行
E:\python>python sys.py
['sys.py']

E:\python>python sys.py 4 58 24 5
['sys.py', '4', '58', '24', '5']

(2)exit
可以有参数,默认为0,正常退出

>>> help(sys.exit)
Help on built-in function exit in module sys:

exit(status=None, /)
    Exit the interpreter by raising SystemExit(status).

    If the status is omitted or None, it defaults to zero (i.e., success).  
      If the status is an integer, it will be used as the system exit status.
    If it is another kind of object, it will be printed and the system
    exit status will be one (i.e., failure).

(3)获取操作

获取系统默认编码
>>> sys.getdefaultencoding()
'utf-8'
获取系统文件的编码
>>> sys.getfilesystemencoding()
'utf-8'

系统对于递归的层次限制
>>> sys.getrecursionlimit()
1000
可修改,但不建议修改
 sys.setrecursionlimit()

(4)path:所有系统变量路径

>>> sys.path
['', 'E:\\python\\bianyiqi\\python38.zip', 'E:\\python\\bianyiqi\\DLLs',
 'E:\\python\\bianyiqi\\lib', 'E:\\python\\bianyiqi', 'E:\\python\\bianyiqi\\lib\\site-packages']

因为import,首先会在当前路径找要导入法的模块,依次向下找,找到就不找了
第一个表示当前路径
(5)了解方法

|-- 'platform'			# 获取系统平台
|-- 'setrecursionlimit'(重要)	# 设置系统对于递归的层数限制,不建议使用方法
|-- 'stderr',			# 标准错误输出
|-- 'stdin',			# 标准输入    input函数
|-- 'stdout'			# 标准输出     print函数
|-- 'thread_info'		# 系统线程信息
|-- 'version'			# 解释器版本
|-- 'version_info'		# 解析器内核版本信息
1>>> sys.platform
'win32'

>>> sys.thread_info
sys.thread_info(name='nt', lock=None, version=None)
>>> sys.thread_info.name      由上可得,可作为对象
'nt'2>>> sys.version      #属性
'3.8.1 (tags/v3.8.1:1b293b6, Dec 18 2019, 22:39:24) [MSC v.1916 32 bit (Intel)]'

>>> sys.version_info
sys.version_info(major=3, minor=8, micro=1, releaselevel='final', serial=0)

二,加密模块
hash算法:
将任何东西加密为32位的16进制的字符串
md5是hashlib模块里的一个方法(是hash算法的一种),用来加密算法的,

>>> import hashlib
>>> dir(hashlib)
>>> hashlib.md5("123456".encode("utf-8"))        必须为字节,所以要将字符串转成字节
<md5 HASH object @ 0x01827BE0>

>>> md = hashlib.md5("123456".encode("utf-8"))
>>> md.hexdigest()       转化为16进制
'e10adc3949ba59abbe56e057f20f883e'
存在碰撞,所以使用盐值混淆(校验时不用,密码用)
>>> md.update("xixixi".encode("utf-8"))    编码随便写,建议使用utf-8,上下的编码一致
>>> md.hexdigest()
'6189f1d00312d272789b6b3652500ee4'
就算破解也是盐值加原本的密码,并不会找到实际密码
import hmac
 hmac.new("123456".encode("utf-8"),"hahaha".encode('utf-8'),"MD5")
 密码安全性更高,即使用hash算法,又使用对称加密。第二个encode编码单引和双引转化后是一样的,最开始返回的地址不同。
 

三,时间模块
python专门用来操作时间和日期的模块
方法:

>>> import time

(1>>> time.sleep(6)    让当前线程暂停,单位秒

(2>>> time.time()
1583939714.5854864
时间戳:时间标识,指1970-1-1 0-0-0到当前获取时间的秒数,运用此,可以实现时间的定位。



(3>>> time.ctime()         返回当前时间
'Wed Mar 11 23:16:16 2020'
>>> time.ctime(66666666)     返回距离开始据66666666的时间
'Fri Feb 11 22:31:06 1972'4>>> time.localtime()      可看做对象
time.struct_time(tm_year=2020, tm_mon=3, tm_mday=11, tm_hour=23, tm_min=16, tm_sec=48, tm_wday=2, tm_yday=71, tm_isdst=0)
>>> t = time.localtime()
>>> t.tm_year
2020
>>> t.tm_mday
11
>>> print("当前时间{}-{}".format(t.tm_year,t.tm_mon))   格式化时间
当前时间2020-3
>>> time.ctime(time.time()+6666)   距离当前时间后的6666'Thu Mar 12 01:13:58 2020'     返回的是UTC标准时间



(5)
```python
>>> t = time.ctime()

>>> time.strftime("%Y-%m-%d")     将一个时间对象格式化为特定字符串
'2020-03-11'

>>> help(time.strftime)
 %Y  Year with century as a decimal number.
    %m  Month as a decimal number [01,12].
    %d  Day of the month as a decimal number [01,31].
    %H  Hour (24-hour clock) as a decimal number [00,23].    %M  Minute as a decimal number [00,59].
    %S  Second as a decimal number [00,61].
    %z  Time zone offset from UTC.
    %a  Locale's abbreviated weekday name.
    %A  Locale's full weekday name.
    %b  Locale's abbreviated month name.
    %B  Locale's full month name.
    %c  Locale's appropriate date and time representation.
    %I  Hour (12-hour clock) as a decimal number [01,12].    %p  Locale's equivalent of either AM or PM.


>>> time.strptime("2013-2-2","%Y-%m-%d")    
time.struct_time(tm_year=2013, tm_mon=2, tm_mday=2, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=5, tm_yday=33, tm_isdst=-1)
返回设置时间,要设置时分秒 继续添加即可,不写,默认0

四,datetime

>>> import datetime
>>> from datetime import datetime
>>> datetime.now()
datetime.datetime(2020, 3, 11, 23, 39, 29, 75621)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值