Storing JSON data in Aerospike in Python

时间:2022-01-11 16:56:41

I'm trying to retrieve response from ip-api.com for most IP ranges. But I want to store that data in Aerospike but I'm having some errors.

我正在尝试从ip-api.com检索大多数IP范围的响应。但我想将这些数据存储在Aerospike中,但我遇到了一些错误。

Here is the Python script

这是Python脚本

# import the module
from __future__ import print_function
import aerospike
import urllib2
config = {
  'hosts': [ ('127.0.0.1', 3000) ]
}

try:
  client = aerospike.client(config).connect()
except:
  import sys
  print("failed to connect to the cluster with", config['hosts'])
  sys.exit(1)

key = ('ip', 'hit', 'trial')

try:
  for i in range(0,255):
    for j in range(0,255):
        for k in range(0,255):
            for l in range(0,255):
                if not((i == 198 and j == 168) or (i == 172 and j > 15 and j < 32) or (i == 10)):
                    response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l))
                    html = response.read()
                    client.put(key, html)
except Exception as e:
  import sys
  print("error: {0}".format(e), file=sys.stderr)


client.close()

I'm new to Python as well as Aerospike, infact any no-SQL databases. Any help would be appreciated.

我是Python和Aerospike的新手,实际上是任何无SQL数据库。任何帮助,将不胜感激。

1 个解决方案

#1


1  

All code from aerospike perspective it right, except you would want to change

除了你想要改变之外,所有来自aerospike的代码都是正确的

html = response.read()
client.put(key, html)

to

import json

client.put(key, json.load(response))

The response is a json string which needs to be converted to json object

响应是一个json字符串,需要转换为json对象

#1


1  

All code from aerospike perspective it right, except you would want to change

除了你想要改变之外,所有来自aerospike的代码都是正确的

html = response.read()
client.put(key, html)

to

import json

client.put(key, json.load(response))

The response is a json string which needs to be converted to json object

响应是一个json字符串,需要转换为json对象