源工程地址: https://github.com/reorx/httpstat
一些对httpstat.py的注释代码:
#!/usr/bin/env python
# coding: utf-8
# # References:
# man curl
# https://curl.haxx.se/libcurl/c/curl_easy_getinfo.html
# https://curl.haxx.se/libcurl/c/easy_getinfo_options.html
# 如何使用curl指令统计请求的各个部分的时间:
# http://blog.kenweiner.com/2014/11/http-request-timings-with-curl.html
# https://github.com/reorx/httpstat
# 在开头加上from __future__ import print_function这句之后,
# 即使在python2.X,使用print就得像python3.X那样加括号使用。
# python2.X中print不需要括号,而在python3.X中则需要
# 参考: https://blog.csdn.net/xiaotao_1/article/details/79460365
from __future__ import print_function
# 用于生成临时文件和目录
# 参考: https://docs.python.org/zh-cn/3/library/tempfile.html#module-tempfile
import tempfile
# 子进程管理
# 参考: https://docs.python.org/zh-cn/3/library/subprocess.html?highlight=subprocess#
import subprocess
# sys.version_info: 一个包含版本号五部分的元组: major, minor, micro, releaselevel 和 serial。
# 参考: https://docs.python.org/zh-cn/3/library/sys.html?highlight=version_info#sys.version_info
PY3 = sys.version_info >= (3,0)
# xrange和range的区别参考:
# https://blog.csdn.net/weixin_44685869/article/details/106041246
# Python3中取消了xrange()的使用,结合成为了range()
if PY3:
xrange = range
# Env class is copied from https://github.com/reorx/getenv/blob/master/getenv.py
# python类定义的时候的object: python2中不继承object的类叫经典类,继承object的类叫做新式类
# 参考: https://www.cnblogs.com/wujingqiao/p/9668583.html#:~:text=python%E5%AE%9A%E4%B9%89%E7%B1%BB%20%28%29%E4%B8%AD%E5%86%99object%E5%92%8C%E4%B8%8D%E5%86%99%E7%9A%84%E5%8C%BA%E5%88%AB%201,python3%E4%B8%AD%EF%BC%8C%E7%B1%BB%E5%AE%9A%E4%B9%89%E9%BB%98%E8%AE%A4%E7%BB%A7%E6%89%BFobject%EF%BC%8C%E6%89%80%E4%BB%A5%E5%86%99%E4%B8%8D%E5%86%99%E6%B2%A1%E6%9C%89%E5%8C%BA%E5%88%AB%202%20%E4%BD%86%E5%9C%A8python2%E4%B8%AD%EF%BC%8C%E5%B9%B6%E4%B8%8D%E6%98%AF%E8%BF%99%E6%A0%B7
class Env(object):
# python中的类变量
# https://blog.csdn.net/feng98ren/article/details/80068036
prefix = 'HTTPSTAT'
_instances = []
def __init__(self, key):
self.key = key.format(prefix=self.prefix)
Env._instances.append(self)
def get(self, default=None):
return os.environ.get(self.key, default)
# Color code is copied from https://github.com/reorx/python-terminal-color/blob/master/color_simple.py
# 检测文件是否连接到一个终端设备
# 参考: https://www.runoob.com/python/file-isatty.html
ISATTY = sys.stdout.isatty()
# 参考: 如何在终端输出带颜色等格式的字符串
# https://www.cnblogs.com/goloving/p/15015053.html
# ANSI escape code: https://en.wikipedia.org/wiki/ANSI_escape_code#Colors
def make_color(code):
def color_func(s):
if not ISATTY:
return s
tpl = '\x1b[{}m{}\x1b[0m'
return tpl.format(code, s)
return color_func
# check curl args
# 参考: https://blog.csdn.net/watt1208/article/details/105163889/
# https://www.mit.edu/afs.new/sipb/user/ssen/src/curl-7.11.1/docs/curl.html#-D--dump-header
exclude_options = [
'-w', '--write-out', # -w / --write-out [format],完成后输出什么
'-D', '--dump-header', # 将协议头写入指定文件
'-o', '--output', # --output,将输出写入文件
'-s', '--silent', # --silent,不输出任何内容
]
# 关于LC_ALL 参考:https://blog.csdn.net/weixin_43529377/article/details/97917713
# python dict update函数: https://python-reference.readthedocs.io/en/latest/docs/dict/update.html
cmd_env.update(
LC_ALL='C',
)
# python json模块说明: https://docs.python.org/zh-cn/3/library/json.html
if metrics_only:
print(json.dumps(d, indent=2))
quit(None, 0)
# :^ 表示居中对齐结果(在可用空间内); :< 表示左对齐结果(在可用空间内)
# 参考: https://www.w3school.com.cn/python/ref_string_format.asp
def fmta(s):
return cyan('{:^7}'.format(str(s) + 'ms'))