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
@ -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
return response