【问题标题】:Proof of concept RESTful Python server (using web.py) + testing with cURL概念证明 RESTful Python 服务器(使用 web.py)+ 使用 cURL 进行测试
【发布时间】:2012-01-11 20:24:38
【问题描述】:

我正在使用 web.py 编写概念验证 RESTful 服务器

这是脚本:

#!/usr/bin/env python
import web
import json


def notfound():
    #return web.notfound("Sorry, the page you were looking for was not found.")
    return json.dumps({'ok':0, 'errcode': 404})

def internalerror():
    #return web.internalerror("Bad, bad server. No donut for you.")
    return json.dumps({'ok':0, 'errcode': 500})


urls = (
    '/(.*)', 'handleRequest',
)


app = web.application(urls, globals())
app.notfound = notfound
app.internalerror = internalerror


class handleRequest:
    def GET(self, method_id):
        if not method_id: 
            return web.notfound()
        else:
            return json.dumps({'ok': method_id})

    def POST(self):
        i = web.input()
        data = web.data() # you can get data use this method
        print data
        pass

if __name__ == "__main__":
    app.run()

我可以发送 GET 请求,但是当我尝试发送 POST 请求时,我收到一个内部错误。目前,我不确定错误是由于 cURL 未正确发送 POST(极不可能),还是我的服务器未正确实现(更有可能)。

这是我用来发送 POST 请求的命令:

curl -i -H "Accept: application/json" -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","active":true http://localhost:8080/xx/xxx/xxxx

这是服务器响应:

me@localhost:~curl -i -H "Accept: application/json" -X POST -d "value":"30","type":"Tip 3","targetModule":"Target 3","active":true http://localhost:8080/xx/xxx/xxxx
HTTP/1.1 500 Internal Server Error
Content-Length: 1382
Content-Type: text/plain

Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 1245, in communicate
    req.respond()
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 775, in respond
    self.server.gateway(self).respond()
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/wsgiserver/__init__.py", line 2018, in respond
    response = self.req.server.wsgi_app(self.env, self.start_response)
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/httpserver.py", line 270, in __call__
    return self.app(environ, xstart_response)
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/httpserver.py", line 238, in __call__
    return self.app(environ, start_response)
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 277, in wsgi
    result = self.handle_with_processors()
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 247, in handle_with_processors
    return process(self.processors)
  File "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", line 244, in process
    raise self.internalerror()
TypeError: exceptions must be old-style classes or derived from BaseException, not str

错误的原因是什么 - 我该如何解决?

【问题讨论】:

  • 该消息的原因很清楚。它命名文件和行号。文件“/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py”,第 244 行。这是您未编写的代码中的错误。你需要和维护 web.py 的人谈谈。不是我们。
  • 还有。为什么POST方法中没有return
  • 我认为你应该在你的 notfound 函数中返回web.notfound(json.dumps({'ok':0, 'errcode': 404}))internalerror 函数也需要类似的更改。
  • @AnandChitipothu:谢谢!。你是明星 :) - 你的建议解决了我遇到的错误。

标签: python web.py


【解决方案1】:

这里有几个问题。

1) POST takes 2 arguments (like GET), self and the resource (method_id is fine)
2) When you're making a POST request you're setting "Content-Type" and not "Accept"
3) Your JSON isn't in quotes as a string

如果您将 POST 更改为 (self, method_id),则以下内容应该有效:

curl -i -H "Content-Type: application/json" -X POST -d '{"value":"30","type":"Tip 3","targetModule":"Target 3","active":true}' http://127.0.0.1:8080

您还应该将块包装在 try/except 中以捕获错误并对它们做一些有用的事情:

def POST(self,method_id):
    try:
        i = web.input()
        data = web.data() # you can get data use this method
        return
    except Error(e):
        print e

【讨论】:

  • 谢谢克尔斯滕!顺便说一句,我还必须更正我的 POST 方法以返回一个值 - 正如 S. Lott 所建议的那样。顺便说一句,您能否指出有关实现 POST 方法的正确方法的文档? web.py 上的文档有一个示例,其中 POST 方法的唯一参数是 self - 这就是我从那里得到这种用法的地方。
猜你喜欢
  • 2015-10-22
  • 2014-03-29
  • 2018-08-03
  • 2014-10-06
  • 2015-12-01
  • 1970-01-01
  • 1970-01-01
  • 2011-02-16
  • 2018-06-11
相关资源
最近更新 更多