2016-01-28 138 views
1

嘿!錯誤:'str'對象沒有屬性'讀'

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

錯誤 錯誤:「海峽」對象有沒有屬性「讀」

誰能請在解決此錯誤的幫助?任何幫助將不勝感激。

回答

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)) 
相關問題