summaryrefslogtreecommitdiff
path: root/examples/getNTPserversNew.py
diff options
context:
space:
mode:
authorCengiz Kaygusuz <cngkaygusuz@gmail.com>2017-11-20 20:46:39 -0500
committerCengiz Kaygusuz <cngkaygusuz@gmail.com>2017-11-20 20:46:39 -0500
commit27e183a78c8062ed7c2bbb91655a5e56cd697bba (patch)
tree88fd355a0cc6da4c130582e092d702836596cbb2 /examples/getNTPserversNew.py
parent4ba589cf13588e90992e23deb5a9784340efd2cc (diff)
downloadpyparsing-git-27e183a78c8062ed7c2bbb91655a5e56cd697bba.tar.gz
Move src to root
Diffstat (limited to 'examples/getNTPserversNew.py')
-rw-r--r--examples/getNTPserversNew.py35
1 files changed, 35 insertions, 0 deletions
diff --git a/examples/getNTPserversNew.py b/examples/getNTPserversNew.py
new file mode 100644
index 0000000..14e710c
--- /dev/null
+++ b/examples/getNTPserversNew.py
@@ -0,0 +1,35 @@
+# getNTPserversNew.py
+#
+# Demonstration of the parsing module, implementing a HTML page scanner,
+# to extract a list of NTP time servers from the NIST web site.
+#
+# Copyright 2004-2010, by Paul McGuire
+# September, 2010 - updated to more current use of setResultsName, new NIST URL
+#
+from pyparsing import (Word, Combine, Suppress, SkipTo, nums, makeHTMLTags,
+ delimitedList, alphas, alphanums)
+try:
+ import urllib.request
+ urlopen = urllib.request.urlopen
+except ImportError:
+ import urllib
+ urlopen = urllib.urlopen
+
+integer = Word(nums)
+ipAddress = Combine( integer + "." + integer + "." + integer + "." + integer )
+hostname = delimitedList(Word(alphas,alphanums+"-_"),".",combine=True)
+tdStart,tdEnd = makeHTMLTags("td")
+timeServerPattern = (tdStart + hostname("hostname") + tdEnd +
+ tdStart + ipAddress("ipAddr") + tdEnd +
+ tdStart + SkipTo(tdEnd)("loc") + tdEnd)
+
+# get list of time servers
+nistTimeServerURL = "http://tf.nist.gov/tf-cgi/servers.cgi#"
+serverListPage = urlopen( nistTimeServerURL )
+serverListHTML = serverListPage.read().decode("UTF-8")
+serverListPage.close()
+
+addrs = {}
+for srvr,startloc,endloc in timeServerPattern.scanString( serverListHTML ):
+ print("%s (%s) - %s" % (srvr.ipAddr, srvr.hostname.strip(), srvr.loc.strip()))
+ addrs[srvr.ipAddr] = srvr.loc