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

Commit 9553f9e

Browse files
committed
use regex for address verification
1 parent bc58619 commit 9553f9e

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ This Python script is made for doing a complete backup of your Wordpress blog's
3434
-P, --prompt-for-password
3535
prompt for password to use
3636
-a ADDRESS, --address ADDRESS
37-
root address of the WordPress blog (example:
38-
'blog.example.net')
37+
root address of the WordPress blog (examples:
38+
'blog.example.net' or '192.168.20.53')
3939
--http use HTTP as protocol
4040
--https use HTTPS as protocol (default)
4141
-v, --version show program's version number and exit

wp-backup-data.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,27 @@
66
from argparse import ArgumentParser
77
from getpass import getpass
88
from sys import exit
9+
from re import compile, IGNORECASE
910

1011
def check_address(address):
1112
if not address or address.strip() == "":
1213
raise ValueError('address required')
13-
elif len(address) < 4:
14-
raise ValueError("address too short")
14+
15+
# These regex come from Django project
16+
# https://github.com/django/django/blob/master/django/core/validators.py
17+
ul = '\u00a1-\uffff'
18+
ipv4_re = r'(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)(?:\.(?:25[0-5]|2[0-4]\d|[0-1]?\d?\d)){3}'
19+
hostname_re = r'[a-z' + ul + r'0-9](?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9])?'
20+
domain_re = r'(?:\.[a-z' + ul + r'0-9]+(?:[a-z' + ul + r'0-9-]*[a-z' + ul + r'0-9]+)*)*'
21+
tld_re = r'\.[a-z' + ul + r']{2,}\.?'
22+
host_re = '(' + hostname_re + domain_re + tld_re + '|localhost)'
23+
regex = compile(
24+
r'(?:' + ipv4_re + '|' + host_re + ')'
25+
r'$', IGNORECASE
26+
)
27+
28+
if not regex.match(address):
29+
raise ValueError('not a valid address')
1530

1631
def check_username(username):
1732
if not username or username.strip() == "":
@@ -49,7 +64,7 @@ def check_prompt_field(checker, field = "", password = False):
4964
ap.add_argument("-u", "--user", help="username to use")
5065
ap.add_argument("-p", "--password", help="password to use")
5166
ap.add_argument("-P", "--prompt-for-password", dest="prompt_pwd", action="store_true", help="prompt for password to use")
52-
ap.add_argument("-a", "--address", help="root address of the WordPress blog (example: 'blog.example.net')")
67+
ap.add_argument("-a", "--address", help="root address of the WordPress blog (examples: 'blog.example.net' or '192.168.20.53')")
5368
ap.add_argument("--http", dest="https", action="store_false", help="use HTTP as protocol")
5469
ap.add_argument("--https", dest="https", action="store_true", help="use HTTPS as protocol (default)")
5570
ap.add_argument("-v", "--version", action="version", version="%(prog)s 1.1.0")
@@ -59,7 +74,7 @@ def check_prompt_field(checker, field = "", password = False):
5974

6075
try:
6176
if not args["address"]:
62-
address = check_prompt_field(check_address, 'URL: ')
77+
address = check_prompt_field(check_address, 'Address: ')
6378
else:
6479
address = check_field(check_address, args["address"])
6580

0 commit comments

Comments
 (0)