Bson이란?
Bson이란 Binary로 인코딩된 Json형태의 파일 포맷이다.
속도가 빠르고 인코딩, 디코딩이 쉽기 때문에 몽고db에서 사용하는 방식이며, 네트워크 상에서 데이터를 전송 할 때 많이 사용한다.
(서버에서 프로토콜을 정의 할때도 마찬가지)
처음에 프로토콜을 정의하려고 아래의 형식이 정의 될때
commandCode | srcClientIp | dstClientIp | ~
1. 문자열로 입력받아서 split으로 나눠서 입력 받는다.
recv(1024).decode().split(',')이런식으로
2. json 방식
3. bson방식
셋중에 bson 방식이 제일 빠르고 간편하다는 것을 알게 되었다.

Python Bson
python Bson으로 검색을 해봤는데 한글 자료가 별로 없었다.
python에서 Bson을 사용하려면
pip install bson
pip로 bson을 설치해준다.
자주 사용하는 명령어는 dumps와 loads가 있는데, 이런식으로 사용이 가능하다
examDic = {
"commandCode" : 1,
"name" : "James",
"age" : "30",
"major" : "ComputerEngineering"
}
examDicBson = bson.dumps(examDic)
print(examDic)
print(examDicBson)
examDicBsonLoads = bson.loads(examDicBson)
print(examDicBsonLoads)
Result:
{'commandCode': 1, 'name': 'James', 'age': '30', 'major': 'ComputerEngineering'}
b'R\x00\x00\x00\x10commandCode\x00\x01\x00\x00\x00\x02name\x00\x07\x00\x00\x00r00t0k\x00\x02age\x00\x03\x00\x00\x0024\x00\x02major\x00\x14\x00\x00\x00ComputerEngineering\x00\x00'
{'commandCode': 1, 'name': 'James', 'age': '30', 'major': 'ComputerEngineering'}
api.mongodb.com/python/current/api/bson/index.html
bson – BSON (Binary JSON) Encoding and Decoding — PyMongo 3.9.0 documentation
bson – BSON (Binary JSON) Encoding and Decoding BSON (Binary JSON) encoding and decoding. The mapping from Python types to BSON types is as follows: Python Type BSON Type Supported Direction None null both bool boolean both int int32 / int64 py -> bson l
api.mongodb.com
py-bson/bson
Independent BSON codec for Python that doesn't depend on MongoDB. - py-bson/bson
github.com
BSON - Wikipedia
BSON () is a computer data interchange format. The name "BSON" is based on the term JSON and stands for "Binary JSON".[2] It is a binary form for representing simple or complex data structures including associative arrays (also known as name-value pairs),
en.m.wikipedia.org
'Programing > Python' 카테고리의 다른 글
python -defaultdict(), 딕셔너리 기본 값지정- (0) | 2022.03.10 |
---|---|
재귀 깊이 수정 (python) (0) | 2022.03.10 |
pip install SSL: CERTIFICATE_VERIFY_FAILED 해결방법 (0) | 2021.07.07 |
num을 0xnum으로 바꾸는 방법 (0) | 2020.02.03 |
Python Portscaner (0) | 2015.10.20 |