Fixed reading and writing to file.
This commit is contained in:
parent
82f290a206
commit
6530edf887
@ -1,5 +1,6 @@
|
|||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
|
import re
|
||||||
import xmlrpc.client
|
import xmlrpc.client
|
||||||
|
|
||||||
from pyramid.httpexceptions import HTTPUnauthorized
|
from pyramid.httpexceptions import HTTPUnauthorized
|
||||||
@ -39,20 +40,23 @@ def update_view(request):
|
|||||||
|
|
||||||
|
|
||||||
def load(file):
|
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):
|
if not os.path.isfile(file):
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
current = {}
|
current = {}
|
||||||
with open(file) as f:
|
with open(file) as f:
|
||||||
for line in f:
|
for line in f:
|
||||||
domain, ip = line.split(':')
|
match = exp.match(line)
|
||||||
current[domain] = ip.rstrip('\n')
|
if match:
|
||||||
|
domain, ip = match.groups()
|
||||||
|
current[domain] = ip
|
||||||
return current
|
return current
|
||||||
|
|
||||||
|
|
||||||
def update(file, db):
|
def update(file, db):
|
||||||
with open(file, 'w') as f:
|
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()
|
@forbidden_view_config()
|
||||||
|
Loading…
Reference in New Issue
Block a user