Python
2016-02-03
import tempfile
import time
timestr = time.strftime("%Y%m%d%H%M%S")
临时文件
# tempfile.mktemp(suffix='', prefix='tmp', dir=None)
# tempfile.mkstemp(suffix=None, prefix=None, dir=None, text=False)
filepath_temp = tempfile.mktemp(suffix='.html', prefix='.cache_%s_' % timestr)
print(filepath_temp)
# /tmp/.cache_20210929205452_zlmi3pkf.html
filepath_temp = tempfile.mktemp(suffix='.html', prefix='.cache_%s_' % timestr,
dir='/opt/apps/markjour/tmp/')
print(filepath_temp)
# /opt/apps/markjour/tmp/.cache_20210929205452_mcr7nj3e.html
filepath_temp = tempfile.mkstemp(suffix='.html', prefix='.cache_%s_' % timestr, text=True)
mkstemp
和 mktemp
的区别:mkstemp
返回一个文件描述符,mktemp
返回一个文件路径。
mktemp
返回的路径理论上会被另一个进程使用,所以这是 UNSAFE 的,应该用 mkstemp
代替。
mktemp
从 Python 2.3 开始标记为 Deprecated,但是至今还是可以调用。
临时目录
# tempfile.mkdtemp(suffix=None, prefix=None, dir=None)
dirpath_temp = tempfile.mkdtemp()
print(dirpath_temp)
# /tmp/tmp1dj2cl0d
dirpath_temp = tempfile.mkdtemp(prefix='build_',
dir='/opt/apps/markjour/tmp/')
print(dirpath_temp)
# /opt/apps/markjour/tmp/build_9k5synh5
Python zip
2016-02-02
import zipfile
import os
zip_path = "/path/to/zip/file.zip"
extract_path = "/path/to/extract/directory"
if not os.path.exists(extract_path):
os.makedirs(extract_path)
with zipfile.ZipFile(zip_path, 'r') as fp:
# 直接解压
zip_ref.extractall(target_dir)
# 逐个解压
for filename in fp.namelist():
# 如果文件存在就先移除,避免 FileExistsError
extract_file_path = os.path.join(extract_path, filename)
if os.path.exists(extract_file_path):
# print("跳过解压缩文件:", filename)
# continue
os.remove(extract_file_path)
# 解压,会自动创建目录结构
fp.extract(filename, extract_path)
Linux
2016-02-01
文件描述符
File Descriptor
“一切皆文件” 设计思想来自早期 Unix,这是现在 Unix/Linux 界的一个非常重要的概念(根据 Plan9 操作系统的相关资料,这一条没有得到彻底的贯彻)。
允许打开的最大文件数
系统限制
cat /proc/sys/fs/file-nr
# 17781 0 9223372036854775807
# 已分配, 已使用, 最大值
cat /proc/sys/fs/file-max
# 9223372036854775807
sudo sysctl -a | grep file
# fs.file-max = 9223372036854775807
# fs.file-nr = 17813 0 9223372036854775807
修改:
echo 100000000 > /proc/sys/fs/file-max
sysctl fs.file-max 100000000
进程限制
# ulimit 是一个 Shell 内建命令
ulimit -a # 输出所有的相关限制
ulimit -n # 当前进程的最大打开文件数
ulimit -n 10240 # 修改当前进程的最大打开文件数
ps -ef | grep sshd | grep -Fv 'grep '
root 1080 1 0 10月16 ? 00:00:00 sshd: /usr/sbin/sshd -D [listener] 0 of 10-100 startups
sudo ls -l /proc/`pgrep sshd`/fd
总用量 0
lr-x------ 1 root root 64 10月 17 12:04 0 -> /dev/null
lrwx------ 1 root root 64 10月 17 12:04 1 -> 'socket:[25487]'
lrwx------ 1 root root 64 10月 17 12:04 2 -> 'socket:[25487]'
lrwx------ 1 root root 64 10月 17 12:04 3 -> 'socket:[28674]'
lrwx------ 1 root root 64 10月 17 12:04 4 -> 'socket:[28676]'
/etc/security/limits.conf
echo "* soft nofile 65535" | sudo tee -a /etc/security/limits.conf
echo "* hard nofile 65535" | sudo tee -a /etc/security/limits.conf
系统会根据 /etc/pam.d/login 中的 PAM 配置来为登录用户设置,
如果配置了 session required /lib/security/pam_limits.so
, 就会加载 /etc/security/limits.conf
设置用户的各种限制值。
<domain> <type> <item> <value>
- domain
- a user name
- a group name, with
@group
syntax
- the wildcard
*
, for default entry
- the wildcard
%
, can be also used with %group
syntax, for maxlogin limit
- NOTE: group and wildcard limits are not applied to root.
To apply a limit to the root user, must be the literal username root.
- type
- "soft" for enforcing the soft limits
- "hard" for enforcing hard limits
- item
core
- limits the core file size (KB)
data
- max data size (KB)
fsize
- maximum filesize (KB)
memlock
- max locked-in-memory address space (KB)
nofile
- max number of open file descriptors
rss
- max resident set size (KB)
stack
- max stack size (KB)
cpu
- max CPU time (MIN)
nproc
- max number of processes
as
- address space limit (KB)
maxlogins
- max number of logins for this user
maxsyslogins
- max number of logins on the system
priority
- the priority to run user process with
locks
- max number of file locks the user can hold
sigpending
- max number of pending signals
msgqueue
- max memory used by POSIX message queues (bytes)
nice
- max nice priority allowed to raise to values: [-20, 19]
rtprio
- max realtime priority
chroot
- change root to directory (Debian-specific)
Git
2016-01-31
删除远程分支时报错:
git push --delete origin new
error: 无法删除 'new':远程引用不存在
error: 无法推送一些引用到 'gitee.com:markjour/django-admin'
如果是英文环境就是报:
git push --delete origin new
error: unable to delete 'new': remote ref does not exist
error: failed to push some refs to 'gitee.com:markjour/django-admin'
一般是这个分支已经被别人删除了。
Solution
git branch -d -r origin/new
TLS SSH OpenSSH OpenSSL
2016-01-30
我仔细阅读原文,然后按照理解对原文做了重新整理。
- SSL 是通讯链路的附加层。可以包含很多协议。HTTPS, FTPS, .....
SSL 是一种国际标准的加密及身份认证通信协议,您用的浏览器就支持此协议。
SSL(Secure Sockets Layer)最初是由美国 Netscape 公司研究出来的,后来成为了 Internet 网上安全通讯与交易的标准。
SSL 协议使用通讯双方的客户证书以及 CA 根证书,允许客户/服务器应用以一种不能被偷听的方式通讯,在通讯双方间建立起了一条安全的、可信任的通讯通道。
它具备以下基本特征:信息保密性、信息完整性、相互鉴定。 主要用于提高应用程序之间数据的安全系数。
SSL 协议的整个概念可以被总结为:一个保证任何安装了安全套接字的客户和服务器间事务安全的协议,它涉及所有TC/IP应用程序。
- SSH 只是加密的 Shell,最初是用来替代 telnet 的。通过 port forward,也可以让其他协议通过 SSH 的隧道而起到加密的效果。
SSH 的英文全称是 Secure SHell。通过使用SSH,你可以把所有传输的数据进行加密,这样“中间人”这种攻击方式就不可能实现了,而且也能够防止 DNS 和 IP 欺骗。
还有一个额外的好处就是传输的数据是经过压缩的,所以可以加快传输的速度。
SSH 有很多功能,它既可以代替 telnet,又可以为 ftp、pop、甚至 ppp 提供一个安全的“通道”。
SSH 是由客户端和服务端的软件组成的,有两个不兼容的版本分别是:1.x 和 2.x。用 SSH 2.x 的客户程序是不能连接到 SSH 1.x 的服务程序上去的。OpenSSH 2.x 同时支持 SSH 1.x 和 2.x。
SSH 的安全验证是如何工作的从客户端来看,SSH 提供两种级别的安全验证。
- 第一种级别(基于口令的安全验证)只要你知道自己帐号和口令,就可以登录到远程主机。
所有传输的数据都会被加密,但是不能保证你正在连接的服务器就是你想连接的服务器。可能会有别的服务器在冒充真正的服务器,也就是受到“中间人”这种方式的攻击。
- 第二种级别(基于密匙的安全验证)需要依靠密匙,也就是你必须为自己创建一对密匙,并把公用密匙放在需要访问的服务器上。
如果你要连接到 SSH 服务器上,客户端软件就会向服务器发出请求,请求用你的密匙进行安全验证。
服务器收到请求之后,先在你在该服务器的家目录下寻找你的公用密匙,然后把它和你发送过来的公用密匙进行比较。
如果两个密匙一致,服务器就用公用密匙加密“质询”(challenge)并把它发送给客户端软件。
客户端软件收到“质询”之后就可以用你的私人密匙解密再把它发送给服务器。
用这种方式,你必须知道自己密匙的口令。
与第一种级别相比,第二种级别不需要在网络上传送口令。
第二种级别不仅加密所有传送的数据,而且“中间人”这种攻击方式也是不可能的(因为他没有你的私人密匙)。
但是整个登录的过程可能需要10秒。
-
OpenSSL 一个 C 语言函数库,是对 SSL 协议的实现。
OpenSSL 很优秀,所以很多涉及到数据加密、传输加密的地方都会使用 OpenSSL 的库来做。
-
OpenSSH 是对 SSH 协议的实现。
OpenSSH 利用 OpenSSL 提供的库。OpenSSL 中也有个叫做 openssl 的工具,是 OpenSSL 中的库的命令行接口。
从编译依赖上看,OpenSSH 依赖于 OpenSSL,没有 OpenSSL 的话 OpenSSH 就编译不过去,也运行不了。
- HTTPS 可以使用 TLS 或者 SSL 协议,而 OpenSSL 是 TLS、SSL 协议的开源实现,提供开发库和命令行程序。
可以理解成所有的 HTTPS 都使用了 OpenSSL。
脚本
# 查看使用了 libssl 库的程序
sudo grep -l "libssl" /proc/*/maps | tr -cd 0-9\\n | xargs -r ps u
References
- http://en.wikipedia.org/wiki/Secure_Shell
- http://baike.baidu.com/view/16147.htm
- http://www.zhihu.com/question/23341334
Linux
2016-01-28
先说明一下 Linux 的运行级别,然后使用 runlevel
、init
、chkconfig
等几个命令实现开机启动任务的管理。
HTTP Cookie
2016-01-28
Cookie 的相关知识总结
SSH XShell 开发工具
2016-01-27
算是 SSH 端口转发(隧道技术)的一种利用。
武汉
2016-01-23
武汉住房公积金的提取办法。
Python 字符编码
2016-01-23
关于 sys.setdefaultencoding
的一些问题。