简体   繁体   English

TypeError:“NoneType”类型的对象在应用程序部署中没有 len() Python

[英]TypeError: object of type 'NoneType' has no len() Python on application deployment

I'm just learning python and I'm trying to run my application but get the following:我只是在学习 python,我正在尝试运行我的应用程序,但得到以下信息:

Traceback (most recent call last):
  File "/Users/plugins/bluegreen.py", line 433, in <module>
    cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]
  File "/Users/plugins/bluegreen.py", line 66, in get_cname
    if len(data.get("cname")) == 0:
TypeError: object of type 'NoneType' has no len()

Here are the contents of line 66 and 433:以下是第 66 行和第 433 行的内容:

66: 66:

def get_cname(self, app):
    response = self.get("/apps/{}".format(app))
    data = json.loads(response.read())
    if len(data.get("cname")) == 0:
      return None
    return data.get("cname")

433: 433:

cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]

Edit 1:编辑1:

If I place a print(data), I get the following:如果我放置打印(数据),我会得到以下信息:

{u'entrypoints': [], u'routeropts': {}, u'description': u'', u'repository': u'git@10.10.1.1.nip.io:hello-green.git', u'tags': [], u'lock': {u'Owner': u'', u'Reason': u'', u'AcquireDate': u'0001-01-01T00:00:00Z', u'Locked': False}, u'routers': [{u'type': u'traefik', u'name': u'traefik', u'opts': {}, u'address': u'hello-green.10.10.1.1.nip.io'}], u'deploys': 0, u'routingsettings': None, u'teams': [u'admin'], u'platform': u'go', u'teamowner': u'admin', u'plan': {u'router': u'traefik', u'swap': 0, u'cpushare': 100, u'name': u'autogenerated', u'memory': 0}, u'ip': u'hello-green.10.10.1.1.nip.io', u'owner': u'admin@shipa.io', u'router': u'traefik', u'units': [], u'pool': u'gce', u'name': u'hello-green'}
Traceback (most recent call last):
  File "/Users/brunoandrade/.shipa/plugins/bluegreen.py", line 434, in <module>
    cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]
  File "/Users/brunoandrade/.shipa/plugins/bluegreen.py", line 67, in get_cname
    if len(data.get("cname")) == 0:
TypeError: object of type 'NoneType' has no len()

Edit 2:编辑2:

Not sure why I get no cname key, since I'm getting it here:不知道为什么我没有得到 cname 键,因为我在这里得到它:

  config = Config.load('shipa-bluegreen.ini')

  app_name = config['name']
  blue = "%s-blue" % app_name
  green = "%s-green" % app_name

  bluegreen = BlueGreen(token, target, config)

  apps = [blue, green]
  cnames = [bluegreen.get_cname(green), bluegreen.get_cname(blue)]

Which is reading from the shipa-bluegreen.ini, which I have in the file:这是从我在文件中的shipa-bluegreen.ini 中读取的:

[Application]
name: hello

Any help would be really appreciated任何帮助将非常感激

Thanks!谢谢!

You printed out data, and got你打印出数据,然后得到

{u'entrypoints': [], u'routeropts': {}, u'description': u'', u'repository': u'git@10.10.1.1.nip.io:hello-green.git', u'tags': [], u'lock': {u'Owner': u'', u'Reason': u'', u'AcquireDate': u'0001-01-01T00:00:00Z', u'Locked': False}, u'routers': [{u'type': u'traefik', u'name': u'traefik', u'opts': {}, u'address': u'hello-green.10.10.1.1.nip.io'}], u'deploys': 0, u'routingsettings': None, u'teams': [u'admin'], u'platform': u'go', u'teamowner': u'admin', u'plan': {u'router': u'traefik', u'swap': 0, u'cpushare': 100, u'name': u'autogenerated', u'memory': 0}, u'ip': u'hello-green.10.10.1.1.nip.io', u'owner': u'admin@shipa.io', u'router': u'traefik', u'units': [], u'pool': u'gce', u'name': u'hello-green'}

You can clearly see that there is no key called "cname" in here.您可以清楚地看到这里没有名为“cname”的键。

There's no cname key in data . data没有cname键。 data.get("cname") returns None if the key isn't found but you can specify an explicit default.如果未找到键,则data.get("cname")返回None但您可以指定显式默认值。 Use an empty string as the default instead.改为使用空字符串作为默认值。

def get_cname(self, app):
    response = self.get("/apps/{}".format(app))
    data = json.loads(response.read())
    if len(data.get("cname", "")) == 0:
      return None
    return data.get("cname")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM