From 6530edf88706d40f72e0810c0283b66b6138b6df Mon Sep 17 00:00:00 2001 From: tanshu Date: Sun, 20 Mar 2016 00:33:37 +0530 Subject: [PATCH] Fixed reading and writing to file. --- bifrost/views.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/bifrost/views.py b/bifrost/views.py index 08c31c0..882ba83 100644 --- a/bifrost/views.py +++ b/bifrost/views.py @@ -1,5 +1,6 @@ import logging import os +import re import xmlrpc.client from pyramid.httpexceptions import HTTPUnauthorized @@ -14,7 +15,7 @@ def update_view(request): file = request.registry.settings['biforst.file'] username = request.registry.settings['webfaction.username'] password = request.registry.settings['webfaction.password'] - current_ip = request.GET.get('ip',None) + current_ip = request.GET.get('ip', None) if current_ip is None: if 'X-Forwarded-For' in request.headers: current_ip = request.headers['X-Forwarded-For'] @@ -39,24 +40,27 @@ 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() def basic_challenge(request): response = HTTPUnauthorized() response.headers.update(forget(request)) - return response \ No newline at end of file + return response