Skip to content

Commit

Permalink
peerexplorerLookup now for NXT and HZ
Browse files Browse the repository at this point in the history
  • Loading branch information
altsheets committed Jan 23, 2016
1 parent 0b950c5 commit 7700744
Show file tree
Hide file tree
Showing 6 changed files with 727 additions and 81 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Expand Up @@ -2,4 +2,7 @@
/planning/
/obsolete/
.project
.pydevproject
.pydevproject

# GFM View files:
*.md.html
13 changes: 10 additions & 3 deletions chaincountdown.py
Expand Up @@ -35,18 +35,18 @@
'''

from config import TIMEOUT
from config import SERVER, BLOCKSPERMINUTE, GENESIS
from config import SERVERS, BLOCKSPERMINUTE, GENESIS

import server

import urllib2, urllib, json, time


def queryApi(data, pfn):
def queryApiOneServer(data, server):
"""urlencode data parameters, urlopen, result to json
returns (True, json) or (False, error message)
"""
url = SERVER[pfn] + "?%s" % urllib.urlencode(data)
url = server + "?%s" % urllib.urlencode(data)

try:
f=urllib2.urlopen(url, timeout=TIMEOUT)
Expand All @@ -61,6 +61,13 @@ def queryApi(data, pfn):
else:
return True, apiResult

def queryApi(data, pfn):
for oneServer in SERVERS[pfn]:
success, result = queryApiOneServer(data, oneServer)
if success: break # as soon as one server delivers, be happy
print "Server '%s' resulted in '%s'. Trying next one ..." % (oneServer, result)
return success, result

def getBlockchainStatus(pfn):
"api getBlockchainStatus"
data = {"requestType" : "getBlockchainStatus"}
Expand Down
21 changes: 21 additions & 0 deletions config.py
Expand Up @@ -14,13 +14,34 @@

# coins specifics:

"""
SERVER ={
"nxt" : "http://jnxt.org:7876/nxt",
# "nxt" : "http://localhost:7876/nxt",
"nhz" : "http://api.nhzcrypto.org:7776/nhz",
# "nhz": "http://localhost:7776/nhz",
}
"""

SERVERS ={
"nxt" : [
# "http://dummyToCauseTrouble.org:7876/nxt",
"http://jnxt.org:7876/nxt",
"http://nxt.cybermailing.com:7876/nxt",
"http://multi.djaeger.net:7876/nxt",
"http://nxt.sx:7876/nxt",
"http://abctc.vps.nxtcrypto.org:7876/nxt",
"http://daemon.bevolved.net:7876/nxt"
],

"nhz" : [
# "http://dummyToCauseTrouble.org:7776/nhz",
"http://api.nhzcrypto.org:7776/nhz"
],
# "nhz": "http://localhost:7776/nhz",
}


BLOCKSPERMINUTE = {"nxt": 1 / 1.87, # measured at block height 607838 (see https://nxtforum.org/nrs-releases/nrs-v1-7-4/msg205165/#msg205165 )
"nhz": 1 / 1.72} # measured at block height 539874
Expand Down
4 changes: 2 additions & 2 deletions server.py
Expand Up @@ -16,7 +16,7 @@
import chaincountdown, imaging, helpers

from config import PORT_NUMBER, INFOPAGE, STYLESHEET, SUGGESTION
from config import SERVER
from config import SERVERS
from config import FILENAMEPIC, IMAGETYPES
from config import FONTS, IMAGEDEFAULTS, FONTSIZEFOOTER_MIN
from config import BASEDIR, TEMPDIRNAME
Expand Down Expand Up @@ -60,7 +60,7 @@ def parseUrl(self):
pfn = o.path.split("/")[1].lower()
if pfn =="hz": pfn="nhz"
if pfn=="": raise Exception("no platform specified")
if pfn not in SERVER.keys():
if pfn not in SERVERS.keys():
raise ParsingException(self.NOT_IMPLEMENTED,
"Unknown chain. Contact me if you want your coin implemented here.")

Expand Down
101 changes: 82 additions & 19 deletions tools/peerexplorerLookup.py
Expand Up @@ -2,8 +2,11 @@
'''
@title: peerexplorerLookup.py
@summary: Queries peerexplorer for nxt nodes with open API,
then looks up the domainnames for all results.
@summary: NXT: Queries peerexplorer for nxt nodes with open API.
HZ: Queries localhost for 'getPeers'
BOTH: Then looks up the domainnames for all results,
and does fancy sorting:
Final table is:
* sorted by domain, in cool ordering: from TLD to the front
Expand All @@ -12,7 +15,7 @@
@related: Made for chaincountdown.py when jnxt.org was down.
https://github.com/altsheets/chaincountdown
@version: v0.14
@version: v0.15
@since: 2016/01/23
@author: AltSheets
Expand Down Expand Up @@ -41,16 +44,18 @@

import urllib2, urlparse, json, socket, threading, timeit

TIMEOUT=8

def queryPeerexplorer(server="http://www.peerexplorer.com", command="/api_openapi"):
def queryAPI(server="http://www.peerexplorer.com", command="/api_openapi", timeout=TIMEOUT):
"""get all IP addresses"""

url=urlparse.urljoin(server, command)
# print url
try:
f=urllib2.urlopen(url)
f=urllib2.urlopen(url, timeout=timeout)
answer = f.read()
f.close()
# print answer
apiResult = json.loads(answer)
except Exception as e:
return False, e
Expand Down Expand Up @@ -126,18 +131,9 @@ def sortIPaddresses(ips):
for i in range(len(ips)):
ips[i] = ips[i].replace(" ", "")

def makeNXTnodesTable():
"""Put everything together: Query peerexplorer, query DNS, sort, print"""

command="/api_openapi"
IPs = queryPeerexplorer(command=command)

if not IPs[0]:
print "peerexplorer didn't want to play with me: ", IPs[1]
return False
def nodesTableWithDomainNames(IPs):
"""Put everything together: Query DNS, sort, print"""

IPs=IPs[1]["peers"]
print "Got %d node IPs from Peerexplorer, for command %s" % (len(IPs), command)
print "Now looking up domain names, patience please ..."
NXTnodes=lookupIPs(IPs)

Expand All @@ -156,10 +152,77 @@ def makeNXTnodesTable():
for IP in noDomains:
print "%15s" % IP

return True


if __name__=="__main__":
makeNXTnodesTable()
def nodesTableWithDomainNames_NXT():
"""Query peerexplorer, then IP-->DNS table"""

command="/api_openapi"
IPs = queryAPI(command=command)

if not IPs[0]:
print "peerexplorer didn't want to play with me: ", IPs[1]
return False

IPs=IPs[1]["peers"]
print "Got %d node IPs from Peerexplorer, for command %s" % (len(IPs), command)

nodesTableWithDomainNames(IPs)


def findHZnodes(hzserver="http://localhost:7776"):

success, peers=queryAPI(server=hzserver, command="/nhz?requestType=getPeers")

if not success:
print "%s didn't want to play with me: %s" % (hzserver, peers)
return False

peers=peers["peers"]
print "Got %d peers from %s, now checking which ones have open API ..." % (len(peers), hzserver)
print "Patience please, timeout is %d seconds." % TIMEOUT

started=timeit.default_timer()

def openAPIthread(IP, openAPI, printLock):
"check :7776, then print (with printLock = in orderly fashion)"

hzserver="http://%s:7776" % IP
success, test=queryAPI(server=hzserver, command="/nhz?requestType=getMyInfo")

answerTime=timeit.default_timer()-started
printLock.acquire()
print "(%.3fs) %27s: %s" % (answerTime, hzserver, success)
printLock.release()
if success: openAPI.append(IP)

openAPI=[]
threads, printLock=[], threading.Lock()
for IP in peers:
t=threading.Thread(target=openAPIthread, args=(IP, openAPI, printLock))
threads.append(t)
for t in threads: t.start()
for t in threads: t.join()

print "Ready. Found %d nodes with open API." % len(openAPI)
# print openAPI

return openAPI



def nodesTableWithDomainNames_HZ():
"""Query localhost for peers, then IP-->DNS table"""

openAPI_IPs = findHZnodes()
print
nodesTableWithDomainNames(openAPI_IPs)
print "These are all IPs with open API!"


if __name__=="__main__":
print ("-" * 50 + "\n") * 2 + "\nNXT:\n\n" + ("-" * 50 + "\n") * 2
nodesTableWithDomainNames_NXT()
print ("-" * 50 + "\n") * 2 + "\nHZ :\n\n" + ("-" * 50 + "\n") * 2
nodesTableWithDomainNames_HZ()

0 comments on commit 7700744

Please sign in to comment.