我运行了该程序来“嗅探”网络上的流量,但是遇到的第一个问题是它以十六进制等格式代码将其提供给我。所以我尝试转换代码,但是由于它是一个元组,所以它不起作用,所以我将元组转换为字符串。我收到此错误代码:
$ python sniffer.py
Traceback (most recent call last):
File "sniffer.py", line 24, in <module>
output1 = convertTuple(output)
File "sniffer.py", line 8, in convertTuple
output = functools.reduce(operator.add, (tup))
TypeError: can't concat tuple to bytes
这是我的完整代码:
import binascii
import struct
import socket
import functools
import operator
def convertTuple(tup):
output = functools.reduce(operator.add, (tup))
return output
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
s = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.IPPROTO_IP)
s.bind(("192.168.2.16",80))
s.setsockopt(socket.IPPROTO_IP,socket.IP_HDRINCL,1)
s.ioctl(socket.SIO_RCVALL,socket.RCVALL_ON)
while True:
output = s.recvfrom(800)
output1 = convertTuple(output)
bytearray.fromhex(output1).decode()
print(output1)
答案 0 :(得分:0)
recvfrom
返回一个元组(data,address)
。如果您想用十六进制数据,请执行以下操作:
data,addr = s.recvfrom(800)
print(address,data.hex())