一点日常记录

日常的一些知识点记录

MySQL插数据

MySQL从表A中读取数据插入到表B

1
insert into B('字段a', '字段b', '字段c') select 字段a,字段b,字段c from A;

awk改数据

一个文件,内容为:

1
2
3
this is a dog
this is a cat
this is a pig

最后一列改成xxx

1
awk -F ' ' '{$4="xxx";print $0}' awkfile.log

http和https区别

https协议需要向CA机构申请CA证书

s代表https传输是由SSL/TSL+HTTP构建,相对于http的无状态,https可进行身份验证,加密传输,更加安全。

客户端使用https方式和web服务器通信步骤

  1. 客户端用https格式url请求web服务端
  2. web服务器收到请求后,将网站的证书信息(包含公钥)传送一份给客户端
  3. 客户端的浏览器和web服务器协商SSL/TSL连接的安全等级(信息加密等级)
  4. 客户端浏览器根据安全等级,建立会话密钥,然后利用网站公钥将密钥加密,传给网站
  5. 服务端用私钥解密

装饰器实现记录函数执行时间

装饰器就是python中把函数当作参数的一种实现,目的是增强函数的功能,比如给函数增加执行日志,给函数增加执行时间等等

写一个装饰器,记录函数的执行时间

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#!/usr/bin/env python3
# -*-coding: utf-8 -*-
"""
@author: kyle
@time: 2021/3/10 14:46
"""


from functools import wraps
import time

def get_time(func):
@wraps(func)
def decorators(*args, **kwargs):
now = time.time()
f = func(*args, **kwargs)
print(func.__name__ + " was called")
after = time.time()
time_use = after - now
print(func.__name__ + "执行时间为: " + str(format(time_use, '.5f')) + "s")
return f
# return func.__name__ + "执行时间为: " + str(format(time_use, '.5f')) + "s"
return decorators

@get_time
def get_test(x):
return x**8**6

print(get_test(8))

执行结果:

1
2
3
get_test was called
get_test执行时间为: 0.00349s
一串很长的数字。。

带参数的装饰器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from functools import wraps
import time


def get_log(level=None):
def decorate_log(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.time()
f = func(*args, **kwargs)
end = time.time()
time_used = end - start
if level == 'info':
print("Info: " + func.__name__ + " 执行时间为:" + format(time_used, '0.4f'))
elif level == 'debug':
print("Debug: " + func.__name__ + " 执行时间为:" + format(time_used, '0.4f'))
else:
print("Msg: " + func.__name__ + " 执行时间为:" + format(time_used, '0.4f'))
return f
return wrapper
return decorate_log


@get_log(level='info')
def calculate_num(n):
return n ** 6 ** 8

@get_log()
def calculate_num_2(n):
return n ** 6 ** 8

装饰器带可选参数,指定不同的参数实现不同的增强效果

文章目录
  1. MySQL插数据
  2. awk改数据
  3. http和https区别
    1. 客户端使用https方式和web服务器通信步骤
  4. 装饰器实现记录函数执行时间
    1. 带参数的装饰器
|