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
|
||||||
@ -14,7 +15,7 @@ def update_view(request):
|
|||||||
file = request.registry.settings['biforst.file']
|
file = request.registry.settings['biforst.file']
|
||||||
username = request.registry.settings['webfaction.username']
|
username = request.registry.settings['webfaction.username']
|
||||||
password = request.registry.settings['webfaction.password']
|
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 current_ip is None:
|
||||||
if 'X-Forwarded-For' in request.headers:
|
if 'X-Forwarded-For' in request.headers:
|
||||||
current_ip = request.headers['X-Forwarded-For']
|
current_ip = request.headers['X-Forwarded-For']
|
||||||
@ -39,24 +40,27 @@ 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()
|
||||||
def basic_challenge(request):
|
def basic_challenge(request):
|
||||||
response = HTTPUnauthorized()
|
response = HTTPUnauthorized()
|
||||||
response.headers.update(forget(request))
|
response.headers.update(forget(request))
|
||||||
return response
|
return response
|
||||||
|
Loading…
Reference in New Issue
Block a user