奇怪的'str'对象是不可调用的python [英] weird 'str' object is not callable python

查看:572
本文介绍了奇怪的'str'对象是不可调用的python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好我的代码我得到这个堆栈:

Hello guys in my code i'm getting this stack:

Traceback (most recent call last):
  File "main.py", line 30, in <module>
    print nova.put_server_metadata("48f366bd-e9c9-47b5-a41f-ca9bcac2d945","labeltest","2.0")
TypeError: 'str' object is not callable

但是我不知道,关于这个错误,我试图在我的方法put_server_metadata中放置一个打印件,但它不是在屏幕上打印,请按照以下代码:

but i've no idea, about this error, i've tried to put a print inside my method "put_server_metadata", but it's not printing on screen, follow the code:

主要类别:

import novaapiclient
import novaauth

if __name__ == "__main__":

    nova = novaapiclient.NovaAPIClient("http://192.168.100.142:35357/v2.0/tokens")

    nova.makeAuth("adminUser", "secretword", "2ad1fc162c254e59bea043560b7f73cb")

    print nova.put_server_metadata("48f366bd-e9c9-47b5-a41f-ca9bcac2d945","labeltest","2.0")

我的put_server_metadata代码:

And my put_server_metadata code:

def put_server_metadata(self, serv_id, label, version):
        if self.auth.isAuthed():

            c = pycurl.Curl()
            printer = cStringIO.StringIO()

            url = self.auth.getComputeURL() + "/servers/%s/metadata" % serv_id
            json_put = file('json_server_put_metadata.json','r+w')
            new_js = str(self.put_server_metadata) % (label, version)

            json_put.seek(0)
            json_put.write(new_js)
            json_put.truncate()
            json_put.close()

            json_to_send = file('json_server_put_metadata.json','r+w')
            siz = os.path.getsize("json_server_put_metadata.json")

            tok = str(self.auth.getAuthToken())

            final_tok = "X-Auth-Token: %s" % tok
            content_len = "Content-length: %d" % siz
            cont_type = "Content-Type: application/json"
            accept = "Accept: application/json"

            c.setopt(pycurl.URL, url)
            c.setopt(pycurl.PUT, 1)
            c.setopt(pycurl.HTTPHEADER, [final_tok, cont_type, accept, content_len])
            c.setopt(pycurl.INFILE, json_to_send)
            c.setopt(pycurl.INFILESIZE, siz)
            c.setopt(pycurl.WRITEFUNCTION, printer.write)
            c.setopt(pycurl.VERBOSE, 1)
            c.perform()
            c.close()

            return printer.getvalue()
        else:
            return "Not authorized"


推荐答案

从错误消息中可以看出, put_server_metadata 既可以用作方法,也可以用作字符串。以下提示相同:

From the error message, it would appear that put_server_metadata is used both as a method and as a string. The following hints at the same:

def put_server_metadata(self, serv_id, label, version):  # a method
  ...
  new_js = str(self.put_server_metadata) % ... # not consistent with
                                               # put_server_metadata being a method

如果你是,我会在代码中搜索所有出现的 put_server_metadata 并从那里获取它。

If were you, I'd search through the code for all occurrences of put_server_metadata and would take it from there.

这篇关于奇怪的'str'对象是不可调用的python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆