qdyxmas's Room

Done is better than perfect...

使用python查看https证书到期时间

# coding: utf-8 
# 查询域名证书到期情况

import re
import time
import subprocess
from datetime import datetime
from io import StringIO

def main(domain):
    f = StringIO()
    comm = f"curl -Ivs https://{} --connect-timeout 10".format(domain)

    result = subprocess.getstatusoutput(comm)
    f.write(result[1])

    m = re.search('start date: (.*?)\n.*?expire date: (.*?)\n.*?common name: (.*?)\n.*?issuer: CN=(.*?)\n', f.getvalue(), re.S)
    start_date = m.group(1)
    expire_date = m.group(2)
    common_name = m.group(3)
    issuer = m.group(4)

    # time 字符串转时间数组
    start_date = time.strptime(start_date, "%b %d %H:%M:%S %Y GMT")
    start_date_st = time.strftime("%Y-%m-%d %H:%M:%S", start_date)
    # datetime 字符串转时间数组
    expire_date = datetime.strptime(expire_date, "%b %d %H:%M:%S %Y GMT")
    expire_date_st = datetime.strftime(expire_date,"%Y-%m-%d %H:%M:%S")

    # 剩余天数
    remaining = (expire_date-datetime.now()).days

    print ('域名:', domain)
    print ('通用名:', common_name)
    print ('开始时间:', start_date_st)
    print ('到期时间:', expire_date_st)
    print (f'剩余时间: {remaining}天')
    print ('颁发机构:', issuer)
    print ('*'*30)

    time.sleep(0.5)

if __name__ == "__main__":
    domains = ['www.baidu.com', 'www.jd.com'] 
    for domain in domains:
        main(domain)

阅读全文 »

jmeter HTTP2

https://github.com/grpc/grpc/blob/master/doc/PROTOCOL-HTTP2.md https://dzone.com/articles/learning-about-the-headers-used-for-grpc-over-http

阅读全文 »

python grpc安装

参考地址:https://segmentfault.com/a/1190000015220713?utm_source=channel-hottest 参考地址:https://www.lijiaocn.com/%E6%8A%80%E5%B7%A7/2019/02/22/grpc-benchmark-method.html 参考地址:https://grpc.io/docs/guides/concepts.html

阅读全文 »

使用python的retrying库处理尝试多次请求

功能:

一般装饰器api 特定的停止条件(限制尝试次数) 特定的等待条件(每次尝试之间的指数增长的时间等待) 自定义的异常进行尝试 自定义的异常进行尝试返回结果 最简单的一个使用方法是无论有任何异常出现,都会一直重新调用一个函数、方法,直到返回一个值 import random from retrying import retry

@retry
def do_something_unreliable():
    if random.randint(0, 10) > 1:
        print "just have a test"
        raise IOError("Broken sauce, everything is hosed!!!111one")
    else:
        return "Awesome sauce!"

print do_something_unreliable()

运行该段代码,你会发现每次随机打印的“just have a test”这句话次数不一致 安装

简单的安装retrying:

pip install retrying

例子 正如你上边看到的例子,默认的行为会一直重试,没有时间等待

@retry
def never_give_up_never_surrender():
    print "Retry forever ignoring Exceptions, don't wait between retries"

@retry(stop_max_attempt_number=7)
def stop_after_7_attempts():
    print "Stopping after 7 attempts"

我们没有太多的时间,所以在每个尝试需要加个时间限制(多少s后停止尝试)

@retry(stop_max_delay=10000)
def stop_after_10_s():
    print "Stopping after 10 seconds"

大多数事情并不是需要尽可能快的执行,所以加一些时间等待(每个尝试间加固定时间等待)

@retry(wait_fixed=2000)
def wait_2_s():
    print "Wait 2 second between retries"

一些最好是随机的时间等待(每个尝试随机时间等待)

@retry(wait_random_min=1000, wait_random_max=2000)
def wait_random_1_to_2_s():
    print "Randomly wait 1 to 2 seconds between retries"

再一次,在重新尝试分布式服务和其他远程端点时,很难击败指数级回退(不会翻译,大概就是每次尝试的等待时间以指数形式增长)

@retry(wait_exponential_multiplier=1000, wait_exponential_max=10000)
def wait_exponential_1000():
    print "Wait 2^x * 1000 milliseconds between each retry, up to 10 seconds, then 10 seconds afterwards"

我们有一些处理重新尝试的选项,它们会引起特定的或一般的异常,就像这里的情况一样(根据异常重试)

def retry_if_io_error(exception):
    """Return True if we should retry (in this case when it's an IOError), False otherwise"""
    return isinstance(exception, IOError)

@retry(retry_on_exception=retry_if_io_error)
def might_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors"

@retry(retry_on_exception=retry_if_io_error, wrap_exception=True)
def only_raise_retry_error_when_not_io_error():
    print "Retry forever with no wait if an IOError occurs, raise any other errors wrapped in RetryError"

我们也可以使用函数的结果来改变重新尝试的行为

def retry_if_result_none(result):
    """Return True if we should retry (in this case when result is None), False otherwise"""
    return result is None

@retry(retry_on_result=retry_if_result_none)
def might_return_none():
    print "Retry forever ignoring Exceptions with no wait if return value is None"

任何停止、等待等的组合也会被支持,使你可以自由地混合和匹配。骚男,尝试起来吧! 原文:https://pypi.org/project/retrying/

阅读全文 »