#285 Python3 中的 Bytes 索引取值

2019-01-13

今天研究 secure cookie (安全的 Cookie) 的时候发现这个问题,觉得很好玩,之前还没有遇到过,没有注意。

a = b'hello'
a[0]
# 104

Python3 对 Bytes 类型索引取值返回的是一个整数!!!

这个以后需要小心,如果要像我们预期的那样,取一个字符的话,应该这样:

a = b'hello'
a[0:1]
# h

#283 日程邀请邮件

2019-01-10

经过测试,至少 QQ 和 网易邮箱的 WebMail 都提供了日程的支持。
Thunderbird 只能通过日历 App 打开附件 (ics 文件) 的方式添加日程。

效果

event mail

原理

在邮件中插入了一个 text/calendar 类型的附件:

Content-Type: text/calendar; charset=utf-8; method=REQUEST;
    name=ATT1547083200618.ics
Content-Transfer-Encoding: base64
Content-Disposition: inline; filename="ATT1547083200618.ics"

解析出来这样一个 iCal 文件:

BEGIN:VCALENDAR
PRODID:-//Netease//WebMail
VERSION:2.0
METHOD:REQUEST
BEGIN:VEVENT
SUMMARY:日程:上午十点的面试
LOCATION:小会议室
DTSTART:20190110T013000Z
DTEND:20190110T023000Z
UID:11c27eb7-adab-4549-8aea-efadcec7bb6c
SEQUENCE:0
STATUS:CONFIRMED
ORGANIZER;CN=张三:mailto:zhangsan@example.com
ATTENDEE:mailto:lisi@example.com
END:VEVENT
END:VCALENDAR

回复

这个回复就各异了,没有同意的格式。
QQ 邮件只会有一个 自动回复: xxxx,不知道是接受还是拒绝。
QQ 企业邮件会有详细的信息,附带了原日程,并有一句话:xxx 已经接受你的邀请: 日程:xxx

#282 常见 Web 缓存服务

2019-01-06

Apache,Nginx 都有缓存功能,再加上 Redis 做动态数据的缓存,再加上 CDN,所以我还没有用过专门的缓存服务。
但是这些服务真是大名鼎鼎,即便不用,我也可以先列出来做个简单的了解。

#281 Git Submodule

2019-01-03

命令

usage: git submodule [--quiet] [--cached]
   or: git submodule [--quiet] add [-b <branch>] [-f|--force] [--name <name>] [--reference <repository>] [--] <repository> [<path>]
   or: git submodule [--quiet] status [--cached] [--recursive] [--] [<path>...]
   or: git submodule [--quiet] init [--] [<path>...]
   or: git submodule [--quiet] deinit [-f|--force] (--all| [--] <path>...)
   or: git submodule [--quiet] update [--init [--filter=<filter-spec>]] [--remote] [-N|--no-fetch] [-f|--force] [--checkout|--merge|--rebase] [--[no-]recommend-shallow] [--reference <repository>] [--recursive] [--[no-]single-branch] [--] [<path>...]
   or: git submodule [--quiet] set-branch (--default|--branch <branch>) [--] <path>
   or: git submodule [--quiet] set-url [--] <path> <newurl>
   or: git submodule [--quiet] summary [--cached|--files] [--summary-limit <n>] [commit] [--] [<path>...]
   or: git submodule [--quiet] foreach [--recursive] <command>
   or: git submodule [--quiet] sync [--recursive] [--] [<path>...]
   or: git submodule [--quiet] absorbgitdirs [--] [<path>...]

相关文件

  1. .gitmodules

    [submodule "<moduleName>"]
        path = <moduleDir>
        url = <repoAddr>
    
  2. .git/config 中有相近的 section:

    [submodule "<moduleName>"]
        active = true
        url = <repoAddr>
    
  3. .git 目录在 .git/modules/<moduleName>

克隆

参考:https://stackoverflow.com/questions/3796927/how-to-git-clone-including-submodules

git clone --recurse-submodules -j8 github.com:shouce/shouce.git # 2.13+
git clone --recursive -j8 github.com:shouce/shouce.git # 1.6.5+

-j 表示子模块并发操作,每次 n 个子模块。

针对更老的版本或者以存在的库:

git clone github.com:shouce/shouce.git
cd shouce
git submodule update --init --recursive

发现一个 clone 参数 --[no-]shallow-submodules,可以使每个子模块仓库的克隆 deepth 为 1,应该是用得上的。

已存在的项目

可能是克隆的时候没有克隆子仓库,也可能是后面添加进来的子仓库。

$ git submodule init
Submodule '<moduleName>' (<repoAddr>) registered for path '<moduleDir>'

$ git submodule update
Cloning into '<moduleFullPath>'...
Submodule path '<moduleDir>': checked out '<commitID>'

或者二合一:

git submodule update --init --recursive

后面有更新就进入子模块 git pull。

添加子模块

git submodule add -b dev --name devtools gitee.com:catroll/devtools tools/dev
# [submodule "devtools"]
#      path = tools/dev
#      url = gitee.com:catroll/devtools
#      branch = dev

删除子模块

$ git submodule deinit <moduleDir>
Cleared directory '<moduleDir>'
Submodule '<moduleName>' (<remoteAddr>) unregistered for path '<moduleDir>'

作用:

  1. 清空子模块目录下的所有文件
  2. 去掉 .git/config 子模块配置

这个操作之后:git status 没有任何变化,.gitmodule 还保留着。

然后:

  1. 修改 .gitmodules
  2. 删除子模块目录
  3. 提交变更
  4. 推送到远程仓库

#279 Python 定时任务的简单部署

2018-12-20

部署

# curl: (35) SSL connect error
# StackOverflow: You are using a very old version of curl.
# yum upgrade curl

# 安装 pyenv
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

# 可用版本列表
pyenv install -l
# 使用国内 Python 镜像,相当于给 pyenv 加速
wget https://mirrors.sohu.com/python/3.6.7/Python-3.6.7.tar.xz -P ~/.pyenv/cache/
# 安装(优先使用缓存目录中的文件,会检查缓存校验码是否正确)
pyenv install 3.6.7

运行方式

假定:

  • 项目路径:/path/to/project/
  • 定时任务命令:python main.py

crontab 配置

早上 01:15 执行某某定时任务:

15 1 * * * /bin/bash /path/to/project/cron.sh

脚本 cron.sh

pyenv + pip 模式

#!/bin/bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

cd /path/to/project/

# 创建并激活虚拟环境,安装依赖
# $PYENV_ROOT/versions/3.6.7/envs/tasks/
pyenv virtualenv 3.6.7 tasks

if [ $? -ne 0 ]; then
    pyenv activate tasks
    pip install -r requirements.txt
else
    pyenv activate tasks
fi

# 运行脚本
python main.py

pyenv + pipenv 模式

#!/bin/bash

# 环境变量
PYENV_ROOT="$HOME/.pyenv"
PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

cd /path/to/project/

# 选择 Python 版本
pyenv local 3.6.7

pip show pipenv > /dev/null

if [ $? -ne 0 ]; then
    # 安装 pipenv
    pip install pipenv
    # 创建虚拟环境,安装依赖
    pipenv install
fi

# 运行脚本
pipenv run python main.py