【问题标题】:Webpy AttributeError ' header key'Webpy AttributeError '标题键'
【发布时间】:2017-04-25 06:28:37
【问题描述】:

我最近在我的 ubuntu 机器上下载了 webpy,但我目前在使用它的 POST 功能时遇到了问题。 这是我的代码:

#! /usr/bin/env python

import web,interface

urls = (
    '/', 'index'
)

class index(object):
    def POST(self):
        data = web.input()
        interface.interfaceModule(data.decider)
    return "SENT TO INTERFACE"


if __name__ == "__main__":
    app = web.application(urls, globals())
    app.run()

interface 只是另一个接收 POST 值并通过 Serial 将其发送到 Arduino 的类。

这是错误输出:

 Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/web/application.py", line     239, in process
return self.handle()
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 230, in handle
    return self._delegate(fn, self.fvars, args)
File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 462,    in _delegate
   return handle_class(cls)
  File "/usr/local/lib/python2.7/dist-packages/web/application.py", line 438, in handle_class
    return tocall(*args)
  File "/var/www/cgi-bin/index.py", line 12, in POST
    interface.interfaceModule(data.decider)
  File "/usr/local/lib/python2.7/dist-packages/web/utils.py", line 76, in _  _getattr__
    raise AttributeError, k
AttributeError: 'decider'

192.168.10.1:52225 - - [09/Dec/2016 19:41:09] "HTTP/1.1 POST /" - 500   Internal Server Error

我使用 Chromium 应用程序发送了 post 请求:POST-MAN,带有指定的键和任意值。

【问题讨论】:

  • 您使用data.decider,但似乎data 没有属性decider。使用print(data, type(data), dir(data)) 查看data 中的内容
  • @furus 我之前尝试打印出对象值,是的,它是空的:“”,至于类型:,然后dir() 不显示决策者属性。我该如何解决这个问题?
  • 这意味着您发布了错误的数据 - 您没有发送任何值。

标签: python ubuntu web.py


【解决方案1】:

此错误表示您发送了错误的请求 - 您没有在 POST 中发送 decider

试试

import requests

r = requests.post('http://localhost:8080', data={'decider':'Hello World'})

print(r.text)

【讨论】:

  • 谢谢它的工作!我看到我一直在使用 POST MAN 和 HttpRequest 浏览器插件做错了什么
猜你喜欢
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 2011-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
  • 2019-07-19
相关资源
最近更新 更多