8月 11

python 中 json读写

import json

# 读取json文件
def read_json(file_name):
    json_file = open("%s" % file_name, "rb")
    get_json = json.load(json_file)
    json_file.close()
    return get_json

# 写入json文件
def write_json(file_name, json_context):
    json_file = open("%s" % file_name, "wb")
    to_json = json.dumps(json_context)
    json_file.write(to_json)
    json_file.close()
6月 23

zabbix 异常IP地址登陆告警

首先在服务器上建立白名单
比如连接是
http://www.simonzhang.net/whitelist.txt
可以访问的ip地址,一行一个
部分zabbix检查python代码如下。

login_status = 0
import urllib2
get_whitelist = urllib2.urlopen("http://www.simonzhang.net/whitelist.txt").read()
_whitelist = get_whitelist[:-1].split('\n')
get_last_list = os.popen("/usr/bin/last -10|head -10|awk ' ''{print $3}'").readlines()
for login_ip in get_last_list:
    if login_ip[:-1] not in _whitelist:
        login_status = 1
return login_status

如果有在白名单外的IP登陆则告警。因为是内网每5秒检查一下,白名单很小,所以不考虑访问频率问题。

6月 10

flask 使用ssl加密连接

flask版本为0.10.1,从安全考虑使用https。开始尝试了很多方法都报错,最后在google找到最简单的方法成功了。
生成SSL证书部分,系统安装OpenSSL。命令如下,csr文件在运行过程中用不到。
openssl genrsa -out simonzhang.net.key 1024
openssl req -new -key simonzhang.net.key -out simonzhang.net.csr
openssl x509 -days 3650 -req -in simonzhang.net.csr -signkey simonzhang.net.key -out simonzhang.net.crt

flask的启动部分配置如下
if __name__ == ‘__main__’:
context = (‘simonzhang.net.crt’, ‘simonzhang.net.key’)
app.run(host = ‘0.0.0.0’, port=9000, debug = True, ssl_context=context)

之前安装很多包,不知道具体引用到那个先记录一下。
pyOpenSSL
pycparser
sffi
ipaddress
enum34
pyasn1
idna
cryptography