错误:“str”对象没有属性“read”

问题描述 投票:0回答:2

嘿那里!

 # import the module
    from __future__ import print_function
    import aerospike
    import urllib2
    import json
    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)).read()
                                        html = response.read()
                                        client.put(key, json.load(response))
     except Exception as e:
       import sys
        print("error: {0}".format(e), file=sys.stderr)


      client.close()

错误 错误:“str”对象没有属性“read”

任何人都可以帮助解决这个错误吗?任何帮助将不胜感激。

python json aerospike
2个回答
1
投票

您已经在这里致电

read()

response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l)).read()

然后您再次尝试拨打

read()

html = response.read()

此外,您之后使用

json.load()
接受文件对象,而不是字符串。要么做:

response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l)).read()
client.put(key, json.loads(response))

或者:

response = urllib2.urlopen('http://ip-api.com/json/'+str(i)+'.'+str(j)+'.'+str(k)+'.'+str(l))
client.put(key, json.load(response))

0
投票

尝试加载而不加载: client.put(key, json.loads(response))

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.