概念证明使用cURL进行RESTful Python服务器(使用web.py)测试

概念证明使用cURL进行RESTful Python服务器(使用web.py)测试,第1张

概述我正在使用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.') r

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

这是脚本:

#!/usr/bin/env pythonimport webimport Jsondef 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 = notfoundapp.internalerror = internalerrorclass 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        passif __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","active":true http://localhost:8080/xx/xxx/xxxxhttp/1.1 500 Internal Server ErrorContent-Length: 1382Content-Type: text/plainTraceback (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,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) 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","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
总结

以上是内存溢出为你收集整理的概念证明使用cURL进行RESTful Python服务器(使用web.py)测试全部内容,希望文章能够帮你解决概念证明使用cURL进行RESTful Python服务器(使用web.py)测试所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1205744.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-04
下一篇 2022-06-04

发表评论

登录后才能评论

评论列表(0条)

保存