Skip to content
This repository was archived by the owner on Sep 22, 2020. It is now read-only.

Commit 56fa308

Browse files
committed
add '--ignore-certificate' option
1 parent b163d35 commit 56fa308

2 files changed

Lines changed: 33 additions & 4 deletions

File tree

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This Python script is made for doing a complete backup of your Wordpress blog's
44

55
## Dependancies
66

7-
* Python 2.7
7+
* Python >= 2.7.9
88
* [Mechanize][1] package
99

1010
### On Fedora
@@ -21,8 +21,9 @@ This Python script is made for doing a complete backup of your Wordpress blog's
2121

2222
## How to
2323

24-
usage: wp-backup-data.py [-h] [-u USER] [-p PASSWORD] [-P] [-a ADDRESS]
25-
[-d DIRECTORY] [--http] [--https] [-v]
24+
usage: wp_backup_data.py [-h] [-u USER] [-p PASSWORD] [-P] [-a ADDRESS]
25+
[-d DIRECTORY] [--http] [--https]
26+
[--ignore-certificate] [-v]
2627

2728
Do a backup of your WordPress data
2829

@@ -40,6 +41,7 @@ This Python script is made for doing a complete backup of your Wordpress blog's
4041
directory where the backup file will be stored
4142
--http use HTTP as protocol
4243
--https use HTTPS as protocol (default)
44+
--ignore-certificate ignore invalid certificates
4345
-v, --version show program's version number and exit
4446

4547
Example: ./wp-backup-data.py -a blog.example.net -u user -P

wp_backup_data.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,13 @@
88
from getpass import getpass
99
from os import access, W_OK
1010
from 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

1319
class 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

Comments
 (0)