概念验证 RESTful Python 服务器(使用 web.py)+ 用 cURL 测试
我正在用 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
这个错误的原因是什么?我该如何修复它呢?
1 个回答
4
这里有几个问题。
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