用Python在Aerospike中存储JSON数据
我正在尝试从ip-api.com检索大多数ip范围的响应。但我想在Aerospike中存储数据,但我有一些错误 下面是Python脚本用Python在Aerospike中存储JSON数据,python,json,aerospike,nosql,Python,Json,Aerospike,Nosql,我正在尝试从ip-api.com检索大多数ip范围的响应。但我想在Aerospike中存储数据,但我有一些错误 下面是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).co
# 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()
#导入模块
来自未来导入打印功能
进口aerospike
导入urllib2
配置={
'主机':[('127.0.0.1',3000)]
}
尝试:
client=aerospike.client(config.connect())
除:
导入系统
打印(“无法使用”,config['hosts']连接到群集)
系统出口(1)
键=('ip','hit','trial')
尝试:
对于范围(0255)内的i:
对于范围(0255)内的j:
对于范围(0255)内的k:
对于范围(0255)内的l:
如果不是((i==198和j==168)或(i==172和j>15和j<32)或(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)
例外情况除外,如e:
导入系统
打印(“错误:{0}”。格式(e),文件=sys.stderr)
client.close()
我对Python和Aerospike都是新手,事实上没有SQL数据库。任何帮助都将不胜感激。从aerospike的角度来看,所有代码都是正确的,除非您想更改
html = response.read()
client.put(key, html)
到
响应是一个json字符串,需要将其转换为json对象。了解html中的对象是什么样子以及您看到的错误会很有帮助。该方法非常简单-它需要键值对。请使用适当的大小写!这是使用解决方案后出现的错误:无法解码JSON对象,这是因为该特定查询的响应不是有效的JSON。在该url上或通过浏览器尝试curl。
import json
client.put(key, json.load(response))