如何使用 pyshark 从远程计算机上的本地托管网站获取数据包



>我正在尝试使用 pyshark 从远程计算机上本地托管的网站(测试目的(获取数据包。

这是我的代码:

import pyshark
def print_live_dns():
capture = pyshark.LiveCapture("wlan0")
for packet in capture:
# print(packet)
with open('packets.txt', 'a') as f:
f.write(str(packet))
if "DNS" in packet and not packet.dns.flags_response.int_value:
print(packet.dns.qry_name)
if __name__ == "__main__":
print_live_dns()

使用此代码,我只能从互联网上获取数据包。 这不是我需要的。我如何实现这一点?使用pyshark, scapy, nmap

您可以使用设置交集

>>> from functools import reduce
>>>
>>> my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,7], [3,2,5,98,23,1,34]]
>>> list(reduce(lambda x, y: set(x).intersection(set(y)), my_list))
[2]
>>>
from functools import reduce
my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
print(reduce(set.intersection, map(set, my_list)))

您可以使用集合

my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
res = set(my_list[0])
for i in range(1, len(my_list)):
res = res.intersection(set(my_list[i]))
print(list(res))

输出

[2]

你可以为此使用集合

my_list = [[2,3,5,6,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
first_tuple_list = [set(lst) for lst in my_list]
print(first_tuple_list[0] & first_tuple_list[1] & first_tuple_list[2])
from functools import reduce
my_list = [[2,3,5,56,7,8,9], [2,4,78,23,13,56,7], [3,2,5,98,23,1,34]]
# use the first list converted to a set as result, use *rest for iteration
result, *rest = map(set, my_list)
for sub in rest:
result = result.intersection(sub)
print(result)

另一种方法是将&操作应用于转换后的集合:

import operator
list(reduce(operator.and_, map(set, l)))

您需要从每个子列表中生成一个set并计算它们的交集(该集合仅包括每个集合中存在的元素(:

intersect = set(my_list[0]) & set(my_list[1]) & set(my_list[2])

在打印结果之前,我们首先将其转换回list

print( list(intersect) )

我的示例是根据您的特定列表量身定制的,使用&交集运算符。我的下一步将是用循环解释一般情况(N 个子列表(,但它已经被其他答案完美覆盖,这将是多余的。

最新更新