错误:'str'对象没有属性'read'

时间:2021-04-22 19:33:51

Hey there!

 # 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()

Error error: 'str' object has no attribute 'read'

错误错误:'str'对象没有属性'read'

Can anyone please help in resolving this error? Any help would be highly appreciated.

有人可以帮忙解决这个错误吗?任何帮助将受到高度赞赏。

1 个解决方案

#1


1  

You've already called read() here:

你已经在这里调用了read():

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

And then you are trying to call read() again:

然后你试图再次调用read():

html = response.read()

Also, you are using json.load() afterwards which accepts a file-object, not a string. Either do:

此外,您之后使用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))

Or:

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

#1


1  

You've already called read() here:

你已经在这里调用了read():

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

And then you are trying to call read() again:

然后你试图再次调用read():

html = response.read()

Also, you are using json.load() afterwards which accepts a file-object, not a string. Either do:

此外,您之后使用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))

Or:

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