Fixed reading and writing to file.

This commit is contained in:
tanshu 2016-03-20 00:33:37 +05:30
parent 82f290a206
commit 6530edf887
1 changed files with 9 additions and 5 deletions

View File

@ -1,5 +1,6 @@
import logging
import os
import re
import xmlrpc.client
from pyramid.httpexceptions import HTTPUnauthorized
@ -39,20 +40,23 @@ def update_view(request):
def load(file):
exp = re.compile(r"^([a-z.]+):([0-9]{1,3}(?:\.[0-9]{1,3}){3})$", re.I)
if not os.path.isfile(file):
return {}
current = {}
with open(file) as f:
for line in f:
domain, ip = line.split(':')
current[domain] = ip.rstrip('\n')
match = exp.match(line)
if match:
domain, ip = match.groups()
current[domain] = ip
return current
def update(file, db):
with open(file, 'w') as f:
f.writelines(['{0}:{1}'.format(key, value) for key, value in db.items()])
f.writelines(['{0}:{1}\n'.format(key, value) for key, value in db.items()])
@forbidden_view_config()