【问题标题】:Get header of a HTTP payload获取 HTTP 有效负载的标头
【发布时间】:2022-01-21 16:11:51
【问题描述】:

我得到了一段代码,它应该解码 pcap 文件以将图像写入目录中。我通过wireshark捕获数据包,浏览http网站获取图像,并将它们放在一个目录中。


def get_header(payload):
    try :
        header_raw = payload[:payload.index(b'\r\n\r\n')+2]
    except ValueError:
        sys.stdout.write('-')
        sys.stdout.flush()
        return None

    header = dict(re.findall(r'(?P<name>.*?): (?P<value>.*?)\r\n', header_raw.decode())) 
    # This line of code is supposed to split out the headers

    if 'Content-Type' not in header:
        return None
    return header

当我尝试运行它时,它给了我这个:

Traceback (most recent call last):
  File "/home/kali/Documents/Programs/Python/recapper.py", line 79, in <module>
    recapper.get_responses()
  File "/home/kali/Documents/Programs/Python/recapper.py", line 62, in get_responses
    header = get_header(payload)
  File "/home/kali/Documents/Programs/Python/recapper.py", line 24, in get_header
    header = dict(re.findall(r"(?P<name>.*?): (?P<value>.*?)\r\n", header_raw.decode()))
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc6 in position 1: invalid continuation byte

我尝试了不同的方法,但无法做到正确。比我更有经验的人能告诉我问题是什么,或者如果我做错了我该如何拆分标题?

【问题讨论】:

  • 你有一个字节集合,你假设它们是 UTF-8 编码的文本,并试图在此基础上将它们解码为文本。这些字节显然不是 UTF-8 编码中的文本。它们可能是不同编码的文本,也可能不是全部都是文本。没有足够的信息来说明它是哪一个。
  • 我怎么知道它使用的是哪种编码?

标签: python http wireshark


【解决方案1】:

更新:我发现我必须使用的编码不是 utf-8,而是 ISO-8859-1

像这样:header = dict(re.findall(r'(?P&lt;name&gt;.*?): (?P&lt;value&gt;.*?)\r\n', header_raw.decode(ISO-8859-1))),它的工作原理!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-03
    • 2021-09-14
    • 2015-06-29
    • 1970-01-01
    • 2017-04-24
    • 1970-01-01
    • 1970-01-01
    • 2015-10-21
    相关资源
    最近更新 更多