From 003cd12be734b05d7281579d3d377f79425531fa Mon Sep 17 00:00:00 2001 From: Simeon Simeonov Date: Sat, 10 Sep 2022 10:27:18 +0200 Subject: Remove obsolete code and use black alike code styling --- beinc_generic_client.py | 86 +++++++++++++++++++++++++++++++------------------ 1 file changed, 55 insertions(+), 31 deletions(-) (limited to 'beinc_generic_client.py') diff --git a/beinc_generic_client.py b/beinc_generic_client.py index ca3847a..af1e74f 100755 --- a/beinc_generic_client.py +++ b/beinc_generic_client.py @@ -2,7 +2,7 @@ # -*- coding: utf-8 -*- # Blackmore's Enhanced IRC-Notification Collection (BEINC) v4.0 -# Copyright (C) 2013-2020 Simeon Simeonov +# Copyright (C) 2013-2022 Simeon Simeonov # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by @@ -29,9 +29,8 @@ import sys import urllib.parse import urllib.request - __author__ = 'Simeon Simeonov' -__version__ = '4.1' +__version__ = '4.2' __license__ = 'GPL3' @@ -58,7 +57,7 @@ def fetch_password(args_password): sys.exit(errno.EACCES) elif os.path.isfile(args_password): try: - with io.open(args_password, 'r') as fp: + with io.open(args_password, 'r', encoding='utf-8') as fp: passwd = fp.readline() if passwd.strip(): return passwd.strip() @@ -86,10 +85,12 @@ def pull_notifications(ssl_context, args): data=urllib.parse.urlencode( ( ('resource_name', args.rname), - ('password', args.password) - )).encode('utf-8'), + ('password', args.password), + ) + ).encode('utf-8'), timeout=args.socket_timeout, - context=ssl_context) + context=ssl_context, + ) response_dict = json.loads(response.read().decode('utf-8')) if response.code != 200: raise socket.error(response_dict.get('message', '')) @@ -113,10 +114,12 @@ def push_notification(ssl_context, args): ('resource_name', args.rname), ('password', args.password), ('title', args.title), - ('message', args.message) - )).encode('utf-8'), + ('message', args.message), + ) + ).encode('utf-8'), timeout=args.socket_timeout, - context=ssl_context) + context=ssl_context, + ) response_dict = json.loads(response.read().decode('utf-8')) if response.code != 200: raise socket.error(response_dict.get('message', '')) @@ -125,91 +128,112 @@ def push_notification(ssl_context, args): def main(inargs=None): """main entry""" parser = argparse.ArgumentParser( - description='The following options are available') + description='The following options are available' + ) parser.add_argument( 'url', metavar='URL', type=str, - help='BEINC server destination URL') + help='BEINC server destination URL', + ) parser.add_argument( - '-c', '--cert-file', + '-c', + '--cert-file', metavar='FILE', type=str, dest='cert', default='', help='CA-cert to check the server-cert against' - '(default: Check disabled)') + '(default: Check disabled)', + ) parser.add_argument( '--ciphers', metavar='CIPHERS', type=str, dest='ciphers', default='', - help='Preferred ciphers list (default: auto)') + help='Preferred ciphers list (default: auto)', + ) parser.add_argument( '--disable-hostname-check', action='store_true', dest='disable_hostname_check', default=False, - help='Do not check whether server cert matches server hostname') + help='Do not check whether server cert matches server hostname', + ) parser.add_argument( - '-m', '--message', + '-m', + '--message', metavar='MESSAGE', type=str, dest='message', default='BEINC message', - help='BEINC message (default: "BEINC message")') + help='BEINC message (default: "BEINC message")', + ) parser.add_argument( - '-n', '--resource-name', + '-n', + '--resource-name', metavar='NAME', type=str, dest='rname', required=True, - help='The name of the BEINC-resource on the remote server') + help='The name of the BEINC-resource on the remote server', + ) parser.add_argument( - '-p', '--password', + '-p', + '--password', metavar='PASSWORD[FILE]', type=str, dest='password', default='', help='BEINC taget-password / text-file containing the target password' - ' (default & recommended: prompt for passwd)') + ' (default & recommended: prompt for passwd)', + ) parser.add_argument( '--pull', action='store_true', dest='pull', default=False, - help='Perform a pull operation (default: push)') + help='Perform a pull operation (default: push)', + ) parser.add_argument( - '-T', '--socket-timeout', + '-T', + '--socket-timeout', metavar='SECONDS', type=int, dest='socket_timeout', default=3, - help='Socket timeout in seconds (0=Python default) (default: 3)') + help='Socket timeout in seconds (0=Python default) (default: 3)', + ) parser.add_argument( - '-t', '--title', + '-t', + '--title', metavar='TITLE', type=str, dest='title', default='BEINC title', - help='BEINC title (default: "BEINC title"') + help='BEINC title (default: "BEINC title"', + ) parser.add_argument( - '-v', '--version', + '-v', + '--version', action='version', version=f'%(prog)s {__version__}', - help='display program-version and exit') + help='display program-version and exit', + ) args = parser.parse_args(inargs) if args.socket_timeout: socket.setdefaulttimeout(args.socket_timeout) args.password = fetch_password(args.password) try: - context = ssl.SSLContext(ssl.PROTOCOL_TLSv1_2) - context.verify_mode = ssl.CERT_NONE + context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT) if args.cert: context.verify_mode = ssl.CERT_REQUIRED context.load_verify_locations(cafile=os.path.expanduser(args.cert)) context.check_hostname = bool(not args.disable_hostname_check) + else: + context.check_hostname = False + context.verify_mode = ssl.CERT_NONE if args.ciphers: context.set_ciphers(args.ciphers) if args.pull: -- cgit v1.3