TypeError:需要一个类似字节的对象,而不是python3 http Request中的'str'

时间:2019-05-25 08:52:21

标签: python python-3.x sockets http

当我运行此代码时:

#!/usr/bin/env python

import scapy.all as scapy
from scapy_http import http


def sniff(interface):
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)


def process_sniffed_packet(packet):
    if packet.haslayer(http.HTTPRequest):
        print(packet)


sniff("eth0")

我得到了:

Traceback (most recent call last):   File "packet_sniffer.py", line 16, in <module>
    sniff("eth0")   File "packet_sniffer.py", line 8, in sniff
    scapy.sniff(iface=interface, store=False, prn=process_sniffed_packet)   File "/usr/local/lib/python3.7/dist-packages/scapy/sendrecv.py", line 886, in sniff
    r = prn(p)   File "packet_sniffer.py", line 13, in process_sniffed_packet
    print(packet)   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 438, in
__str__
    return str(self.build())   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 556, in build
    p = self.do_build()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
    pay = self.do_build_payload()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
    return self.payload.do_build()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
    pay = self.do_build_payload()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
    return self.payload.do_build()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
    pay = self.do_build_payload()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
    return self.payload.do_build()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 541, in do_build
    pay = self.do_build_payload()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 528, in do_build_payload
    return self.payload.do_build()   File "/usr/local/lib/python3.7/dist-packages/scapy/packet.py", line 538, in do_build
    pkt = self.self_build()   File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 179, in self_build
    return _self_build(self, field_pos_list)   File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 101, in _self_build
    val = _get_field_value(obj, f.name)   File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 74, in _get_field_value
    headers = _parse_headers(val)   File "/usr/local/lib/python3.7/dist-packages/scapy_http/http.py", line 18, in _parse_headers
    headers = s.split("\r\n") TypeError: a bytes-like object is required, not 'str'

我该怎么办?

2 个答案:

答案 0 :(得分:1)

您可以使用python2而不是python3来解决此问题。如果您不想更改python版本,则需要对scapy-http的库代码进行一些更改。从您的追溯中,我可以看到该文件位于:/usr/local/lib/python3.7/dist-packages/scapy_http/http.py。现在,使用任何文本编辑器打开该文件。

从以下位置更改第18行

headers = s.split("\r\n")

收件人:

try:
    headers = s.split("\r\n")
except TypeError as err:
    headers = s.split(b"\r\n")

然后从以下位置更改第109行

p = f.addfield(obj, p, val + separator)

收件人:

try:
    p = f.addfield(obj, p, val + separator)
except TypeError as err:
    p = f.addfield(obj, p, str(val) + str(separator))

答案 1 :(得分:0)

scapy_http外,您还使用scapyscapy_http已有一段时间没有更新,并且与Python 3+的兼容性较差

随时查看https://github.com/secdev/scapy/pull/1925:它尚未在Scapy中合并(我想有时可能很快就会合并),但是它是scapy_http的更新和改进的端口,是为最新的{{ 1}}版本