1
我傳遞一個查詢ID對象的視圖,然後獲取該對象,然後調用下面FUNC:DjangoUnicodeDecodeError:「UTF-8」編解碼器不能解碼位置0x80的字節51
def portAdmin(self,status):
status = status
self.adminStateDict = {
'activate': tuple([tuple([1,3,6,1,2,1,2,2,1,7,self.snmpPortOID]),rfc1902.Integer32(1)]),
'deactivate' : tuple([tuple([1,3,6,1,2,1,2,2,1,7,self.snmpPortOID]),rfc1902.Integer32(2)]),
}
(errorIn, activateErrorStatus, errorIndex, varBinds) = cmdgen.CommandGenerator().setCmd(
cmdgen.CommunityData('my-agent', '.xxxx', 0),
cmdgen.UdpTransportTarget((self.snmpIp, 161)),
self.adminStateDict[status]
)
但是,如果沒有從函數返回,當我請求的頁面我得到這個錯誤:
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/django/core/servers/basehttp.py", line 283, in run
self.result = application(self.environ, self.start_response)
File "/usr/local/lib/python2.7/dist-packages/django/contrib/staticfiles/handlers.py", line 68, in __call__
return self.application(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/wsgi.py", line 272, in __call__
response = self.get_response(request)
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 169, in get_response
response = self.handle_uncaught_exception(request, resolver, sys.exc_info())
File "/usr/local/lib/python2.7/dist-packages/django/core/handlers/base.py", line 203, in handle_uncaught_exception
return debug.technical_500_response(request, *exc_info)
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 59, in technical_500_response
html = reporter.get_traceback_html()
File "/usr/local/lib/python2.7/dist-packages/django/views/debug.py", line 117, in get_traceback_html
frame['vars'] = [(k, force_escape(pprint(v))) for k, v in frame['vars']]
File "/usr/local/lib/python2.7/dist-packages/django/template/defaultfilters.py", line 34, in _dec
args[0] = force_unicode(args[0])
File "/usr/local/lib/python2.7/dist-packages/django/utils/encoding.py", line 93, in force_unicode
raise DjangoUnicodeDecodeError(s, *e.args)
DjangoUnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 51: invalid start byte. You passed in 'MibTableColumn((1, 3, 6, 1, 6, 3, 18, 1, 1, 1, 4), \x80\x00O\xb8\x05\xc0\xa8\x06 \x0c\r)' (<type 'str'>)
但是,當我調用同一個函數在Django殼,這工作正常。我很難過。 我想知道的是爲什麼: 1.它在shell中而不是在web服務器上運行。 2.我如何使它在使用Django/WSGI的網絡服務器上工作。
謝謝。
謝謝格雷厄姆。當我在處理代碼時,我發現了一個解決方法,將代碼邏輯重新分解到模型的實例方法中。出於某種原因,在獲取對象然後傳遞SNMP變量(使用pySnmp)之間出現問題。 所以,每當遇到這種錯誤,我發現我可以通過將views.py中的邏輯遷移到models.py中來快速修復它。如上所述,它將查找「Apache UTF-8語言環境」。 – imen 2012-04-23 19:34:17