python错误:'module'对象不可调用“math.ceil”

时间:2014-05-21 07:38:10

标签: python python-2.7

我有以下功能,它是对提供商做POST请求,我需要添加新的参数来发布请求以限制超时(默认情况下是5分钟我想将其压缩到1小时,我做了更改但是我不断收到错误

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib64/python2.6/threading.py", line 532, in __bootstrap_inner
    self.run()
  File "/opt/lvptest/lvp_upload.py", line 226, in run
    op = uploadMedia(mediaName, "PyUploader", env)
  File "/opt/lvptest/lvp_upload.py", line 121, in uploadMedia
    expires = math.ceil(time() + 3000) ["expires"]
TypeError: 'module' object is not callable

这是我的功能

def uploadMedia(filepath, description, env):

    global verbose
    global config

    orgId = config[env]["org_id"]
    accessKey = config[env]["access_key"]
    secret = config[env]["secret"]
    expires = math.ceil(time() + 3000) ["expires"]
    filename = os.path.basename(filepath)
    baseUrl = "http://api.videoplatform.limelight.com/rest/organizations/%s/media" %(orgId)

    signedUrl =  lvp_auth_util.authenticate_request("POST", baseUrl, accessKey, secret, expires)
    c = pycurl.Curl()
    c.setopt(c.POST, 1)
    c.setopt(c.HEADER, 0)
    c.setopt(c.HTTPPOST, [('title', filename), ("description", description), (("media_file", (c.FORM_FILE, filepath)))])
    if verbose:
        c.setopt(c.VERBOSE, 1)
    bodyOutput = StringIO()
    headersOutput = StringIO()
    c.setopt(c.WRITEFUNCTION, bodyOutput.write)
    c.setopt(c.URL, signedUrl)
    c.setopt(c.HEADERFUNCTION, headersOutput.write)
    try:
        c.perform()
        c.close()

任何提示,如果我错误添加参数"到期" ?

这是一个示例,我的POST请求是什么样的

POST /rest/organizations/9fafklsdf/media?access_key=sfdfsdfsdfsdfsdf89234 &expires=1400406364&signature=Mc9Qsd4sdgdfg0iEOFUaRC4iiAJBtP%2BMCot0sFKM8A$

2 个答案:

答案 0 :(得分:1)

两个错误:

  1. 您应该from time import time而不仅仅是time。因为里面有time module has a time function

  2. math.ceil returns a float您正试图在以下情况下将其用作词典:

    expires = math.ceil(time() + 3000) ["expires"]
    
  3. 这没有意义。 math.ceil(time() + 3000)将等同于1400406364,您无法从中检索数据 删除["expires"]可以解决问题。

答案 1 :(得分:1)

time 模块不可调用,您需要从中调用time方法:

>>> import time
>>> import math
>>> math.ceil(time())
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'module' object is not callable
>>> math.ceil(time.time())
1400657920.0

然后你需要摆脱["expires"]之后,因为它会返回一个浮点数而不是字典。

我不知道你为什么在这里使用cURL,requests你的代码更简单:

import time
import math
import urllib

import requests

url = 'http://api.videoplatform.limelight.com/rest/organizations/{}/media'
filename = 'foo/bar/zoo.txt'

params = {}
params['access_key'] = 'dfdfdeef'
params['expires'] = math.ceil(time.time()+3000)

url = '{}?{}'.format(url.format(org_id), urllib.urlquote(params))

payload = {}
payload['title'] = os.path.basename(filename)
payload['description'] = 'description'

file_data = {'media_file': open(filename, 'rb')}

result = requests.post(url, data=payload, files=file_data)

result.raise_for_status() # This will raise an exception if
                          # there is a problem with the request