python url解码,在python中解码URL编码的字节流数据

I'm receiving STX ETX packet data, here's a sample:

505f90c584d8ac5596966a0f839d5a95.png

The data has been URL encoded. Before it is encoded and sent it is like this:

d29ce4e1e810bbdd1f7bb317ad5b3464.png

The relationship between the URL encoded data and the byte data before it is encoded and sent is this.

0x41 -> A

0xd9 -> %D9

0x33 -> 3

0x48 -> H

0x58 -> X

0x01 -> %01

0x00 -> %00

After some research I have found that this is unicode code points being converted into hexidecimal numbers and unicode character names. With the exception of the first byte which is an ascii character.

After the first character A, the following four bytes make up a 4 byte integer which is a UTC timestamp.

question

How do i convert the URL back into hexidecimal and unicode code points using python. I've looked at the unicodedata module but can't seem to find a conversion from unicode character names to unicode code points.

Any help or suggestions would be much appreciated.

解决方案

You can use the urlparse module to decode that string.

import urlparse

data = "/type=stxetx&packet=A%d93HX%01%00&serial=1234&foo=bar"

new_data = dict(urlparse.parse_qsl(data))

assert len(new_data['packet']) == 7

assert new_data['packet'][0] == 'A'

assert ord(new_data['packet'][1]) == 0xd9

Reference:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值