python socket编程:简单实现render_template和密码hash

在Flask中有render_template函数,用于解析jinja2模板,现在我们来简单实现它。代码如下:


首先需要导入jinja2模块中的Environment和FileSystemLoader,用os.path获取到模板的存放位置templates_path,将其作为参数传给FileSystemLoader实例化出一个loader对象,再将loader传递给Environment实例一个env对象,env对象的get_template方法获取模板temp,调用temp.render返回渲染后的内容,使用**kwargs来传参数给模板。

密码的加密,可以使用hashlib这个库,可以实现md5、sha1、sha256等多种hash方式,例如:

传入的string,generate_hash会返回sha256加密之后的字符串,值得一提的是sha256.update(new_string)的update方法,sha256.update("Hello World")和依次sha256.update("Hello ")、sha256.update("World")返回的结果是一样。

猜你喜欢

转载自blog.csdn.net/qq_40925239/article/details/89516814
今日推荐