Programing/Python / / 2020. 11. 4. 19:12

python Bson 라이브러리

Bson이란?

 

Bson이란 Binary로 인코딩된 Json형태의 파일 포맷이다.

 

속도가 빠르고 인코딩, 디코딩이 쉽기 때문에 몽고db에서 사용하는 방식이며, 네트워크 상에서 데이터를 전송 할 때 많이 사용한다. 

(서버에서 프로토콜을 정의 할때도 마찬가지)

 

처음에 프로토콜을 정의하려고 아래의 형식이 정의 될때

 

commandCode | srcClientIp | dstClientIp | ~ 

 

 

1. 문자열로 입력받아서 split으로 나눠서 입력 받는다.

recv(1024).decode().split(',')이런식으로 

 

2. json 방식

 

3. bson방식

 

셋중에 bson 방식이 제일 빠르고 간편하다는 것을 알게 되었다.

 

 

 

ref. https://nahyungmin.tistory.com/100

 

 

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

github.com/py-bson/bson

 

py-bson/bson

Independent BSON codec for Python that doesn't depend on MongoDB. - py-bson/bson

github.com

en.m.wikipedia.org/wiki/BSON

 

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

 

  • 네이버 블로그 공유
  • 네이버 밴드 공유
  • 페이스북 공유
  • 카카오스토리 공유