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

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

本文介绍了在python中解码URL编码的字节流数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在接收STX ETX数据包数据,这是一个示例:

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

数据已被URL编码.在编码和发送之前,它是这样的:

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

URL编码的数据和字节数据在编码和发送之前的关系就是这样.

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

经过一些研究,我发现这是将Unicode代码点转换为十六进制数字和Unicode字符名称.除了第一个字节是ascii字符.

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.

在第一个字符A之后,接下来的四个字节组成一个4字节的整数,它是UTC时间戳.

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

如何使用python将URL转换回十六进制和unicode代码点.我看过unicodedata模块,但似乎找不到从Unicode字符名称到Unicode代码点的转换.

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.

推荐答案

您可以使用urlparse模块对该字符串进行解码.

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

参考:

  • http://blog.revathskumar.com/2011/10/python-url-encoding-and-decoding.html
  • https://docs.python.org/2/library/urlparse.html

这篇关于在python中解码URL编码的字节流数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 10:28