我正在使用python 2.7和cherrypy 3.7,并在Ubuntu 14.04上运行它们。我正在使用大学桌面时,无法为此项目安装新模块。
将此行添加到函数后:
in_dict = json.loads(parameters)
按我的应用程序中导致调用此功能的按钮导致“500内部服务器错误”。 参数是一个字典翻转的json对象。 这肯定是导致错误的这一行。
以下是完整的错误消息:
500内部服务器错误
服务器遇到意外情况,导致无法完成请求。
Traceback (most recent call last):
File "/afs/ec.auckland.ac.nz/users/h/r/hril230/unixhome/Documents/hril230/Project/cherrypy/_cprequest.py", line 670, in respond
response.body = self.handler()
File "/afs/ec.auckland.ac.nz/users/h/r/hril230/unixhome/Documents/hril230/Project/cherrypy/lib/encoding.py", line 217, in __call__
self.body = self.oldhandler(*args, **kwargs)
File "/afs/ec.auckland.ac.nz/users/h/r/hril230/unixhome/Documents/hril230/Project/cherrypy/_cpdispatch.py", line 61, in __call__
return self.callable(*self.args, **self.kwargs)
File "mainFile.py", line 146, in sendMessage
msgResponse = urllib2.urlopen(req).read()
File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
return _opener.open(url, data, timeout)
File "/usr/lib/python2.7/urllib2.py", line 410, in open
response = meth(req, response)
File "/usr/lib/python2.7/urllib2.py", line 523, in http_response
'http', request, response, code, msg, hdrs)
File "/usr/lib/python2.7/urllib2.py", line 448, in error
return self._call_chain(*args)
File "/usr/lib/python2.7/urllib2.py", line 382, in _call_chain
result = func(*args)
File "/usr/lib/python2.7/urllib2.py", line 531, in http_error_default
raise HTTPError(req.get_full_url(), code, msg, hdrs, fp)
HTTPError: HTTP Error 500: Internal Server Error
以下是我的导入:
# -*- coding: utf-8 -*-
import cherrypy
import urllib2
import hashlib
import urllib
#import ipgetter TODO:Uncomment before handing in
import socket
import json
以下是发送的功能:
#This function sends a message TODO: Finish this, delete temp link
@cherrypy.expose
def sendMessage(self, sender, info, message):
destination = info[:7] #destination obtained
relevant = info[10:]
i = 0
while (relevant[i] != ","):
i += 1
ip = relevant[:i] #IP obtained
portUnfinished = relevant[(i+1):]
i = 0
while (portUnfinished[i] != ","):
i += 1
port = portUnfinished[:i] #port obtained
out_dict = {"sender":sender, "destination":destination, "message":message}
parameters = json.dumps(out_dict) #all parameters are now combined into a json object
req = urllib2.Request("http://127.0.0.1:8080/receiveMessage", parameters, {'Content-Type':'application/json'})
msgResponse = urllib2.urlopen(req).read()
return msgResponse
以下是接收的功能:
#API IMPLEMENTED: /receiveMessage TODO: Complete this function
@cherrypy.expose
@cherrypy.tools.json_in()
def receiveMessage(self):
in_dict = json.loads(parameters)
return "0"
为什么行“in_dict = json.loads(parameters)”会导致此错误?
更新:非常有用的Eli Rose引起了我的注意,参数不应该是json.loads()的参数。为了找出应该是什么,我回到了使用json的指令,并意识到我在错误的地方使用了错误的指令。我应该使用:
var = cherrypy.request.json
感谢所有评论过的人!