Jestem w trakcie pisania dowód koncepcji serwera za pomocą relaksującego web.pyDowód koncepcji serwera relaksującego Pythonie (przy użyciu web.py) + testów z zwinięcie
Oto scenariusz:
#!/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()
Mogę wysłać żądanie GET ok, jednak gdy próbuję wysłać żądanie POST, otrzymuję błąd wewnętrzny. W tej chwili nie jestem pewien, czy błąd wynika z nieprawidłowego wysyłania POST (bardzo nieprawdopodobne), czy też mój serwer nie jest poprawnie zaimplementowany (co bardziej prawdopodobne).
To polecenie użyć, aby wysłać żądanie 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
Oto odpowiedź serwera:
[email protected]:~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
Co jest przyczyną błędu - i jak można to naprawić ?
Wiadomość jest całkiem jasna w sprawie. Podaje nazwę pliku i numer linii. Plik "/usr/local/lib/python2.6/dist-packages/web.py-0.36-py2.6.egg/web/application.py", wiersz 244. Jest to błąd w kodzie, którego nie napisałeś. Musisz porozmawiać z ludźmi, którzy utrzymują web.py. Nie my. –
Również. Dlaczego nie ma "zwrotu" w metodzie POST? –
Myślę, że powinieneś zwrócić 'web.notfound (json.dumps ({'ok': 0, 'errcode': 404}))' w twojej funkcji notfound. Podobna zmiana jest wymagana dla funkcji 'internalerror'. –