在服务器与客户端交互过程编写中,我用的flask,socketio_client那一套编写的,发送时用的emit,想把客户端产生的公钥发送给服务器, self.clientsio.emit('WakeUp', {'message':self.seq, 'pubkey':self.pubkey})
提示 raise TypeError(f'Object of type {o.class.name} '
TypeError: Object of type PublicKey is not JSON serializable。
pubkey是Publickey类型的,序列化异常,于是根据网上的解决办法,类比
python-flask中Object of type 'Decimal' is not JSON serializable 报错_解决方法
只需重写flask.json中的JSONEncoder方法
from flask import Flask as _Flask
from flask.json import JSONEncoder as _JSONEncoder
class JSONEncoder(_JSONEncoder):
def default(self, o):
import decimal
if isinstance(o, decimal.Decimal):
return float(o)
super(JSONEncoder, self).default(o)
class Flask(_Flask):
json_encoder = JSONEncoder
于是我写了
import json
from rsa import PublicKey
class MyEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, PublicKey):
return str(obj)
super(MyEncoder, self).default(obj)
class MyEncoder01(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, str):
return rsa.key.PublicKey(obj)
super(MyEncoder01, self).default(obj)
if __name__ == '__main__':
from utils import *
pub_key, priv_key = 0, 0
pub_key, priv_key = SIG.gen(nbits=1024)
print(pub_key, type(pub_key))
print(priv_key, type(priv_key))
a = str(pub_key)
print(type(a), a)
b = json.dumps(pub_key,cls=MyEncoder)
print(type(b), b)
print(type(json.dumps(b, cls=MyEncoder01)), json.dumps(b, cls=MyEncoder01))
发现MyEncoder()可以将Publickey转为str型,但MyEncoder01()不能将str转化回Publickey型
输出:
PublicKey(146647646881171537169359107336325819666094326631142895272611391273665241129681864175202210582865711073149830259978328964445433598625123178667749752917227172604704969152912826156599596072470231489606589251316530311794733637375220518820344452893387684493864282125332025109428236062431668791798972403640731817803, 65537) <class 'rsa.key.PublicKey'>
PrivateKey(146647646881171537169359107336325819666094326631142895272611391273665241129681864175202210582865711073149830259978328964445433598625123178667749752917227172604704969152912826156599596072470231489606589251316530311794733637375220518820344452893387684493864282125332025109428236062431668791798972403640731817803, 65537, 128200613893876144453424650756368390163259567201398750306753809912600556327614371182535975875064851679417324947509626643880132248440257904319870965464075165031489831161439972747554336812249267762264709047720622188856637733456737677092968781682269609068565598034179617904522738228278400957066107547593400654913, 54636126617725945797550689259386449889024032381705807100469573652907240614613392080885496738582478818143638672274392119136737616601165804986391348214151603052470763, 2684078392072429766188919579465688336688808899145585353779755149876396199467448920973499204651540204499587689548511436841346164170181127992322081) <class 'rsa.key.PrivateKey'>
<class 'str'> PublicKey(146647646881171537169359107336325819666094326631142895272611391273665241129681864175202210582865711073149830259978328964445433598625123178667749752917227172604704969152912826156599596072470231489606589251316530311794733637375220518820344452893387684493864282125332025109428236062431668791798972403640731817803, 65537)
<class 'str'> "PublicKey(146647646881171537169359107336325819666094326631142895272611391273665241129681864175202210582865711073149830259978328964445433598625123178667749752917227172604704969152912826156599596072470231489606589251316530311794733637375220518820344452893387684493864282125332025109428236062431668791798972403640731817803, 65537)"
<class 'str'> "\"PublicKey(146647646881171537169359107336325819666094326631142895272611391273665241129681864175202210582865711073149830259978328964445433598625123178667749752917227172604704969152912826156599596072470231489606589251316530311794733637375220518820344452893387684493864282125332025109428236062431668791798972403640731817803, 65537)\""