我在python(sniffer.py)中有以下代码,它会嗅探数据包,
import socket
def get_packet():
#create an INET, raw socket
s = socket.socket(socket.AF_INET, socket.SOCK_RAW, socket.IPPROTO_TCP)
#current_interval and conf_interval are read here. where current_interval is a difference of time.time and the time at which program began. and conf_interval is pre-configured interval
# receive a packet
while current_interval < conf_interval:
pkt = s.recvfrom(65565)
process(pkt)
if __name__ == '__main__':
get_packet()
我正在为这段代码编写单元测试,如何模拟recvfrom函数返回虚拟数据包? 这似乎不像我预期的那样有效,
with patch('sniffer.socket.socket.recvfrom') as m_recvfrom:
m_recvfrom.return_value = dummy_pkt
这只是给了我一个模拟对象,我无法访问dummy_pkt。 我如何使这项工作?
谢谢!