88from getpass import getpass
99from os import access , W_OK
1010from os .path import isdir
11- from sys import exit , stderr , stdout
11+ from sys import exit , stderr , stdout , version_info
12+
13+ if version_info [2 ] > 8 :
14+ from ssl import CertificateError
15+ else :
16+ class CertificateError (Exception ):
17+ pass
1218
1319class check_dir (Action ):
1420 def __call__ (self , parser , namespace , values , option_string = None ):
@@ -81,8 +87,10 @@ def check_prompt_field(checker, field = "", password = False):
8187 ap .add_argument ("-d" , "--directory" , action = check_dir , default = "." , help = "directory where the backup file will be stored" )
8288 ap .add_argument ("--http" , dest = "https" , action = "store_false" , help = "use HTTP as protocol" )
8389 ap .add_argument ("--https" , dest = "https" , action = "store_true" , help = "use HTTPS as protocol (default)" )
90+ ap .add_argument ("--ignore-certificate" , dest = "ignore-cert" , action = "store_true" , help = "ignore invalid certificates" )
8491 ap .add_argument ("-v" , "--version" , action = "version" , version = "%(prog)s 1.2.0" )
8592 ap .set_defaults (https = True )
93+ ap .set_defaults (ssl = False )
8694 ap .set_defaults (prompt_pwd = False )
8795
8896 try :
@@ -108,6 +116,21 @@ def check_prompt_field(checker, field = "", password = False):
108116 else :
109117 protocol = "http://"
110118
119+ # Python by default attempts to perform certificate validation since v2.7.9
120+ if args ["ignore-cert" ] and version_info [2 ] > 8 :
121+ from functools import wraps
122+ from httplib import HTTPSConnection
123+ from ssl import _create_unverified_context
124+
125+ old_init = HTTPSConnection .__init__
126+
127+ @wraps (HTTPSConnection .__init__ )
128+ def ignore_cert (self , * args , ** kwargs ):
129+ kwargs ["context" ] = _create_unverified_context ()
130+ old_init (self , * args , ** kwargs )
131+
132+ HTTPSConnection .__init__ = ignore_cert
133+
111134 directory = args ["directory" ]
112135
113136 br = Browser ()
@@ -144,3 +167,7 @@ def check_prompt_field(checker, field = "", password = False):
144167 except ArgumentTypeError as e :
145168 stderr .write ("\n {0}\n " .format (e ))
146169 exit (1 )
170+ except CertificateError as e :
171+ stderr .write ("\n {0}\n " .format (e ))
172+ stderr .write ("You can use the '--ignore-certificate' option to ignore this error\n " )
173+ exit (1 )
0 commit comments