0

I have the following code which takes a list of domains from my Neo4j database, performs a lookup on the IP and then creates a relationship if one does not already exist. It works fine up until the last few line of code where the relationships are created. I'm getting the following error. I've confirmed that the lists have two items - the domain and IP so I'm not sure why its creating an error:

  File "C:\Python26\beta7_whois4j_monitor_debug.py", line 63, in createrels
  rels1 = graph_db.get_or_create_relationships((whoisnodes[0], "links", whoisnodes[1]))
  IndexError: list index out of range

Here is the code:

whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")

for i in domains:
    list1 = []
    value1 = "{0}".format(i['whoisID'])
    try:
        e = socket.gethostbyname(value1)
    except socket.gaierror:
        e = 'exclude from list'
    if e != 'exclude from list':
        list1.append(value1)
        list1.append(e)
        for word in list1:
            whoisnodes = []
            whoisnodes.append(whoisindex.get_or_create("whoisID", word, "whoisID":word}))
            rels1 = graph_db.get_or_create_relationships(
            (whoisnodes[0], "links", whoisnodes[1]))
            print "{0}".format(i['whoisID']) 
Chris Hall
  • 871
  • 6
  • 13
  • 21

2 Answers2

0

I'm a little confused as to what you are trying to do here. For each iteration of your loop for word in list you are resetting whoisnodes to a new list before adding a single item to it on the line below. This means that there can only ever be one item in the list by the time you call get_or_create_relationships, hence the IndexError when you try to access whoisnodes[1].

Did you mean for whoisnodes = [] to be outside the loop?

Incidentally, there is also a typo (missing curly bracket):

whoisindex.get_or_create("whoisID", word, "whoisID":word})

should read:

whoisindex.get_or_create("whoisID", word, {"whoisID":word})
Nigel Small
  • 4,475
  • 1
  • 17
  • 15
  • Nigel,I'm trying to create a relationship for every iteration of list1 that is created. I moved the whoisnodes[] outside the loop and its still returning the error – Chris Hall Jan 25 '13 at 16:19
0

my second try although now its returning a JSON error:

whoisindex = graph_db.get_or_create_index(neo4j.Node, "whoisID")
domains = whoisindex.query("whoisID:*com")

for i in domains:
list1 = []
value1 = "{0}".format(i['whoisID'])    
try:
    e = socket.gethostbyname(value1)
except socket.gaierror:
    e = 'exclude from list'
if e != 'exclude from list':
    list1.append(value1)
    list1.append(e)        
    list1.append(whoisindex.get_or_create("whoisID", i, {"whoisID":i}))
    rels1 = graph_db.get_or_create_relationships(
        (list1[0], "links", list1[1]))
    print "{0}".format(i['whoisID']) 
Chris Hall
  • 871
  • 6
  • 13
  • 21