Skip to content
This repository was archived by the owner on Feb 3, 2024. It is now read-only.

Commit 38f874e

Browse files
author
MooCow
authored
Merge pull request #238 from maarten-boot/development
Refactor and add getting the raw whois data also for unsupported tld's
2 parents 6b134b1 + c9a3707 commit 38f874e

File tree

8 files changed

+277
-72
lines changed

8 files changed

+277
-72
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,4 @@ reformat-code.sh
7070
t1.py
7171
typescript
7272
test.out
73+
tmp/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ Raise an issue https://github.com/DannyCork/python-whois/issues/new
6969
* add testing against static known data in dir: ./testdata/<domain>/output
7070
* test.sh will test all domains in testdata without actually calling whois, the input data is instead read from testdata/<domain>/input
7171

72+
2022-11-11: maarten_boot
73+
* add support for returning the raw data from the whois command: flag include_raw_whois_text
74+
* add support for handling unsupported domains via whois raw text only: flag return_raw_text_for_unsupported_tld
75+
7276
## Support
7377
* Python 3.x is supported.
7478
* Python 2.x IS NOT supported.

makeTestdataAll.sh

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#! /bin/bash
2+
3+
TMPDIR="./tmp"
4+
FORCE=0
5+
VERBOSE=1
6+
7+
prepTempDir()
8+
{
9+
mkdir -p "$TMPDIR" || {
10+
echo "FATAL: cannot make test dir: $TMPDIR" >&2
11+
exit 101
12+
}
13+
}
14+
15+
getAllTldSupported()
16+
{
17+
./test2.py -S
18+
}
19+
20+
makeTestDataOriginalOne()
21+
{
22+
local tld="$1"
23+
local domain="$2"
24+
25+
local d="$TMPDIR/$tld"
26+
mkdir -p "$d" || {
27+
echo "FATAL: cannot make tld directory: '$d'" >&2
28+
exit 101
29+
}
30+
31+
local zz="$domain.$tld"
32+
33+
host -t soa "$zz" |
34+
tee "$d/__dns-soa" |
35+
grep " has SOA record " || {
36+
# no soa record so that domain does not exist, cleanup the test dir
37+
rm -f "$d/input" "$d/output" "$d/__domain__$zz" "$d/__dns-soa" "$d/dns-ns"
38+
echo "INFO: no domain for $zz" >&2
39+
return 1
40+
}
41+
42+
host -t ns "$zz" > "$d/__dns-ns"
43+
44+
# what domain did we test
45+
touch "$d/__domain__$zz"
46+
47+
# force english, force no cache
48+
[ ! -s "$d/input" -o "$FORCE" = "1" ] && {
49+
LANG=EN whois --force-lookup "meta.$tld" >"$d/input" || {
50+
# whois has a problem
51+
rm -f "$d/input" "$d/output" "$d/__domain__$zz" "$d/__dns-soa" "$d/dns-ns"
52+
return 1
53+
}
54+
}
55+
56+
[ ! -s "$d/output" -o "$FORCE" = "1" ] && {
57+
./test2.py -d "meta.$tld" >"$d/output"
58+
}
59+
60+
return 0
61+
}
62+
63+
domainsToTry()
64+
{
65+
cat <<! |
66+
meta
67+
google
68+
!
69+
awk '
70+
/^[ \t]*$/ { next }
71+
/^[ \t]*;/ { next }
72+
/^[ \t]*#/ { next }
73+
{ print $1 }
74+
'
75+
}
76+
77+
makeTestDataOriginalAll()
78+
{
79+
getAllTldSupported |
80+
while read tld
81+
do
82+
echo "try: $tld"
83+
84+
domainsToTry |
85+
while read domain
86+
do
87+
makeTestDataOriginalOne "$tld" "$domain"
88+
[ -f "$TMPDIR/$tld/input" ] && {
89+
[ "$VERBOSE" ] && {
90+
ls -l "$TMPDIR/$tld/"
91+
}
92+
break
93+
}
94+
done
95+
done
96+
}
97+
98+
main()
99+
{
100+
prepTempDir
101+
makeTestDataOriginalAll
102+
}
103+
104+
main

test.sh

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
#! /usr/bin/bash
22

33
# signal whois module that we are testing, this reads data from testdata/<domain>/in
4-
export TEST_WHOIS_PYTHON="1"
5-
6-
TestDataDir="./testdata"
4+
prepPath()
5+
{
6+
local xpath="$1"
7+
TestDataDir=$( realpath "$xpath" )
8+
export TEST_WHOIS_PYTHON="$TestDataDir"
9+
}
710

811
get_testdomains()
912
{
@@ -24,11 +27,16 @@ testOneDomain()
2427

2528
main()
2629
{
30+
prepPath "testdata" # set a default
31+
[ -d "$1" ] && { # if a argument and is a dir use that for testing
32+
prepPath "$1"
33+
}
34+
2735
get_testdomains |
2836
while read line
2937
do
3038
testOneDomain $(basename $line)
3139
done
3240
}
3341

34-
main
42+
main $*

test2.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -226,8 +226,9 @@ def main(argv):
226226
try:
227227
opts, args = getopt.getopt(
228228
argv,
229-
"pvIhaf:d:D:r:H:",
229+
"SpvIhaf:d:D:r:H:",
230230
[
231+
"SupportedTld",
231232
"print",
232233
"verbose",
233234
"IgnoreReturncode",
@@ -259,9 +260,14 @@ def main(argv):
259260
fileData = {}
260261

261262
for opt, arg in opts:
263+
if opt in ("-S", "SupportedTld"):
264+
for tld in sorted(whois.validTlds()):
265+
print(tld)
266+
sys.exit(0)
267+
262268
if opt == "-h":
263269
usage()
264-
sys.exit()
270+
sys.exit(0)
265271

266272
if opt in ("-a", "--all"):
267273
testAllTld = True

whois/_1_query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,12 @@ def testWhoisPythonFromStaticTestData(
120120
verbose: bool = False,
121121
) -> str:
122122
domain = ".".join(dl)
123-
124-
pathToTestFile = f"./testdata/{domain}/input"
123+
testDir = os.getenv("TEST_WHOIS_PYTHON")
124+
pathToTestFile = f"{testDir}/{domain}/input"
125125
if os.path.exists(pathToTestFile):
126-
with open(pathToTestFile, mode="r") as f:
127-
return f.read()
126+
with open(pathToTestFile, mode="rb") as f: # switch to binary mode as that is what Popen uses
127+
# make sure the data is treated exactly the same as the output of Popen
128+
return f.read().decode(errors="ignore")
128129

129130
raise WhoisCommandFailed("")
130131

@@ -135,7 +136,7 @@ def _do_whois_query(
135136
server: Optional[str] = None,
136137
verbose: bool = False,
137138
) -> str:
138-
# TODO: if getenv[TEST_WHOIS_PYTON] fake whois by reading static data from a file
139+
# if getenv[TEST_WHOIS_PYTON] fake whois by reading static data from a file
139140
# this wasy we can actually implemnt a test run with known data in and expected data out
140141
if os.getenv("TEST_WHOIS_PYTHON"):
141142
return testWhoisPythonFromStaticTestData(dl, ignore_returncode, server, verbose)
@@ -150,7 +151,6 @@ def _do_whois_query(
150151
env={"LANG": "en"} if dl[-1] in ".jp" else None,
151152
)
152153

153-
# print(p.stdout.read()+' '+p.stderr.read())
154154
r = p.communicate()[0].decode(errors="ignore")
155155
if ignore_returncode is False and p.returncode not in [0, 1]:
156156
raise WhoisCommandFailed(r)

whois/_3_adjust.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,16 @@ def __init__(
3636
whois_str: Optional[str] = None,
3737
verbose: bool = False,
3838
include_raw_whois_text: bool = False,
39+
return_raw_text_for_unsupported_tld: bool = False,
3940
):
4041
if include_raw_whois_text and whois_str is not None:
4142
self.text = whois_str
4243

44+
if return_raw_text_for_unsupported_tld is True:
45+
self.tld = data["tld"]
46+
self.name = data["domain_name"][0].strip().lower()
47+
return
48+
4349
self.name = data["domain_name"][0].strip().lower()
4450
self.tld = data["tld"]
4551

0 commit comments

Comments
 (0)