惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
V2EX
宝玉的分享
宝玉的分享
Jina AI
Jina AI
IT之家
IT之家
博客园 - Franky
MyScale Blog
MyScale Blog
Y
Y Combinator Blog
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
I
InfoQ
雷峰网
雷峰网
WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Google DeepMind News
Google DeepMind News
美团技术团队
S
SegmentFault 最新的问题
罗磊的独立博客
博客园 - 聂微东
大猫的无限游戏
大猫的无限游戏
H
Help Net Security
D
Docker
博客园 - 司徒正美
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
阮一峰的网络日志
阮一峰的网络日志
M
MIT News - Artificial intelligence

hwchiu learning note Blog

Kubernetes 怎麼計算 imageFS | hwchiu learning note Nginx Proxy_Pass 不會重新查詢 DNS | hwchiu learning note Multus 下如何透過 network policy 設定 | hwchiu learning note Linux Bridge MTU | hwchiu learning note Kubevirt 初體驗 | hwchiu learning note [MacOS ]隨手筆記 Sed 與 Rename 的使用 | hwchiu learning note Docusaurus 使用 blog mode 後連結一直反白的問題 | hwchiu learning note gcloud 切換帳號 | hwchiu learning note k8s 內安裝 redis-cluster | hwchiu learning note Helm Chart 中如何根據條件來動態安裝 Template 內的物件 | hwchiu learning note GCS 操作上注意事項 | hwchiu learning note istio 操作記錄 | hwchiu learning note terraform | hwchiu learning note Kubernetes GKE 維運上小筆記 | hwchiu learning note Git 修改 author/committer | hwchiu learning note GCP NAT 相關筆記 | hwchiu learning note gcloud ssh 到 GCP VM | hwchiu learning note CloudSQL 收費注意事項 | hwchiu learning note Loki 安裝上的參數調整以及 Ring 的問題除錯 | hwchiu learning note kustomize + helm | hwchiu learning note 觀測 K8s 內 OOM 事件 | hwchiu learning note GKE 上的 RBAC 筆記 | hwchiu learning note 本地產生 jwt token | hwchiu learning note ArgoCD 安裝筆記 | hwchiu learning note CircleCI Context 的使用 | hwchiu learning note 透過 GCP IAP Gateway 來保護 GKE 內的網站 | hwchiu learning note 閱讀筆記: 「SRE 的工作介绍」 | hwchiu learning note 閱讀筆記: 「DevOps is a failure」 | hwchiu learning note 閱讀筆記: 「面試人生 - 設計一個簡易的分散式 Job Scheduler」 | hwchiu learning note 閱讀筆記: 「Cloudflare 06/21 災後報告」 | hwchiu learning note
Python-pack_unpack | hwchiu learning note
2013-07-01 · via hwchiu learning note Blog

pack & unpack

根據特定的格式來讀取或封裝資料。

格式部份分成兩個部份

byte-order: 這邊可以決定採用big-endian 或是 little endian, 如果沒有給的話,預設是採用系統的方式去做,那這邊比較要注意到的是 以前再寫C語言的時候,都會有所謂的htons...類的轉換,在這邊可以使用'!' 這個符號來解決這個問題,他會自己使用network的Byte order rule去解讀資料,所以有在用網路連線傳資料的話,一定要用! 避免資料解讀錯誤的問題。

format-characters:

常用的有

  • x:pad byte,就忽略他
  • h:short,2 Byte
  • s:char[], 代表字串,使用時前面要加上數字
  • i:long int, 4Byte
  • B:unsigned char, 1 Byte

詳細的格式資訊請參考官網 Python struct

這邊來個簡單範例 假設今天我們撰寫屬於自己的網路遊戲 然後我們玩家每次上線時,SERVER都會傳送一份玩家的資料給Client

這份資料包含了

  • 遊戲版本
  • 玩家ID
  • 玩家的座標(XY)
  • 玩家當前的財產
  • 玩家的職業
  • 玩家的等級
  • 玩家的經驗值

每個資料所需要的型態跟大小如下敘述

Myheader(){
uint8:version
uint8:playerID
uint16:x
uint16:y
uint32:momey
char10:profession
uint8:level
uint32:experience
}

所以傳送資料過來的時候,我們必須要謹慎的按照這個規格去放置我們的資料。

Example

假設

  • version = 1
  • playerID = 56
  • x = 123
  • y = 2341
  • momey = 5566217
  • profession = "warrior"
  • level = 128
  • experience = 2147383611
data = pack('2B2HI10sBI',version,playerID,x,y,momey,profession,level,experience)
//'\x018{\x00%\t\x00\x00\t\xefT\x00warrior\x00\x00\x00\x80\x00\xdb\xff\xff\x7f'
unpack('2B2HI10sBI',data)
(1, 56, 123, 2341, 5566217, 'warrior\x00\x00\x00', 128, 2147483611)