Update main.py

This commit is contained in:
Saifeddine ALOUI 2024-01-16 10:03:38 +01:00 committed by GitHub
parent 88d99133b7
commit 300850c303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 17 additions and 2 deletions

View File

@ -35,7 +35,8 @@ def get_authorized_users(filename):
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--config',default="config.ini", help='Path to the authorized users list')
parser.add_argument('--config', default="config.ini", help='Path to the authorized users list')
parser.add_argument('--log_path', default="access_log.txt", help='Path to the access log file')
parser.add_argument('--users_list', default="authorized_users.txt", help='Path to the config file')
parser.add_argument('--port', type=int, default=8000, help='Port number for the server')
args = parser.parse_args()
@ -43,7 +44,21 @@ def main():
authorized_users = get_authorized_users(args.users_list)
class RequestHandler(BaseHTTPRequestHandler):
def add_access_log_entry(user, ip_address):
log_file_path = Path(sys.argv[1])
if not log_file_path.exists():
with open(log_file_path, mode='w', newline='') as csvfile:
fieldnames = ['time_stamp', 'user_name', 'ip_address']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
writer.writeheader()
with open(log_file_path, mode='a', newline='') as csvfile:
fieldnames = ['time_stamp', 'user_name', 'ip_address']
writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
row = {'time_stamp': str(datetime.datetime.now()), 'user_name': user, 'ip_address': ip_address}
writer.writerow(row)
def _send_response(self, response):
self.send_response(response.status_code)
self.send_header('Content-type', response.headers['content-type'])