我似乎無法使用json.loads時捕捉異常,即使我專門調用它。當我試圖用很多客戶端連接非常快地發送數據來強調我的服務器時,我在很大程度上看到這一點請參閱我下面的錯誤:Python的JSON解碼與Asynchat不能趕上ValueError異常
(我把它換成我的IP地址,並在錯誤代碼X的)
EX: Unterminated string starting at: line 1 column 49 (char 48) Data: '{"ap-hdop":0.55,"rtcmin":"38","ap-latdec":3.134,"a' error: uncaptured python exception, closing channel (:Unterminated string starting at: line 1 column 49 (char 48) [//faraday_server_handler.py|collect_incoming_data|34] [/usr/lib/python2.7/json/init.py|loads|338] [/usr/lib/python2.7/json/decoder.py|decode|366] [/usr/lib/python2.7/json/decoder.py|raw_decode|382])
我明白這一點,因爲我只是錯過就行了雙引號的代碼失敗:
'{ 「AP-HDOP」:0.55, 「rtcmin」: 「38」, 「AP-latdec」:3.134,「一個'
這行通常是長了不少,使」一...本來應該繼續下去,完成它的報價。
這裏是我的相關代碼:
def collect_incoming_data(self, data):
"""Read an incoming message from the client, place JSON message data into buffer"""
#self.logger.debug('collect_fing_data() -> (%d bytes)\n"""%s"""', len(data), data)
try:
loaded_data = json.loads(data)
except ValueError, ex:
self.handle_error()
type,value,traceback = sys.exc_info()
print type
#print "Collect Incoming Data: " . time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())
print "EX: ", ex
print "Data: ",repr(data)
任何想法?我搜索了互聯網,看看我能否找到這個問題,但是我似乎正在設法捕捉到其他所有人都有這個負載問題似乎表明要做的例外。
編輯2016年3月1日 - 晚間
談到了我的異常了handle_error()讓我看到了更多的錯誤:
except ValueError, ex:
self.handle_error()
type,value,traceback = sys.exc_info()
print type
#print "Collect Incoming Data: " . time.strftime("%Y-%m-%d %H:%M:%S",time.gmtime())
print "EX: ", ex
print "Data: ",repr(data)
下面是我的新的錯誤,我已經註釋掉個人數據。這是明顯的,這個問題我真的現在是,實際上未終止的字符串
EX: Unterminated string starting at: line 1 column 49 (char 48) Data: '{"ap-hdop":0.55,"rtcmin":"31","ap-latdec":XX.XXX,"a' EX: No JSON object could be decoded Data: 'p-latdeg":34,"adc6":2006,"adc7":2007,"adc4":2004,"adc5":2005,"adc2":2002,"adc3":2003,"adc0":2000,"adc1":2001,"gpio-0":30,"gpio-1":50,"gpio-2":70,"speed":5.0,"adc8":2008,"rtcday":"01","longdeg":118,"longdec":XX.XXX,"altitude":31.0,"ap-speed":0.0,"ap-pdop":0.77,"lat-dir":"N","long-dir":"W","hdop":0.01,"ap-rf":0,"alt-units":"M","rtcdow":"2","callsign":"XXXXX","ap-callsign":"XXXXX","id":1,"ap-id":1,"rtcyear":"2016","rtcmon":"03","ap-vdop":0.66,"ap-lat-dir":"N","vdop":0.02,"rtchour":"22","latdec":XX.XXX,"latdeg":34,"ap-longdeg":118,"ap-longdec":XX.XXX,"rtcsec":"15","ap-altitude":86.0,"ap-long-dir":"W","pdop":0.01,"ap-alt-units":"M","faraday-port":0}'
如果您註釋掉'self.handle_error()'它按預期工作? except塊中的打印語句絕對是通過查看輸出執行的,但我認爲'handle_error'被設計爲處理未處理的錯誤。 –
哦有趣@ TadhgMcDonald - 延森,實際上確實讓我看到更多的問題,我已經編輯與更新原來的問題!我還沒有完全測試它,看看原來的錯誤是否完全消失,但我認爲這是解決原始問題的一大步。 第二個問題是,爲什麼我的數據分裂樣,即使終止「\ r \ n」個使用... – bryce
撐起....你所得到的'EX:'消息多次!您正在接收大塊數據的json數據,並且每個部分都是無效的! –