网络工具,功能编程风格

时间:2012-08-30 07:42:32

标签: python functional-programming ping traceroute

我在这里说实话:在功能编程方面,我从来没有比阅读或解决简单问题更进一步。尽管喜欢简洁,易于维护的源代码,我从未找到使用范例的理由。直到今天:我想在Python中实现一些网络工具(如tracerouteping),尽可能“功能性”(在scapy的帮助下,{ {1}}和functools。首先,itertools

ping

然后,def tcp_ping(destination_ip, destination_port, n_probes=10, timeout=1): source_ports = [randint(49152, 65535) for i in range(n_probes)] probes = IP(dst=destination_ip)/TCP(sport=source_ports, dport=destination_port, flags='S') responses = map(partial(sr, timeout=timeout, verbose=0), probes) answered = filter(lambda x: len(x[0]) > 0, responses) lost = filter(lambda x: x not in answered, responses) rtts = map(lambda x: int(1000 * (x[0][0][1].time - x[0][0][0].sent_time)), answered) or [0] return dict(loss=float(len(lost))/n_probes, min_rtt=min(rtts), max_rtt=max(rtts), avg_rtt=mean(rtts), std_rtt=std(rtts))

traceroute

现在,问题:

  • 我们如何才能使这些代码更具“功能性”?
  • 如何添加更多复杂性(例如连续三次超时后停止def takeuntil(predicate, iterable): for x in iterable: yield x if predicate(x): break def traceroute_probes(destination_ip, destination_port): ttl = 1 while ttl <= 255: p = IP(dst=destination_ip, ttl=ttl)/TCP(dport=destination_port, flags='S') yield p ttl = ttl + 1 def is_finalhop(x): return (len(x[0]) > 0 and not x[0][0][1].haslayer('ICMP')) def hop_ip_latency(x): if len(x[0]) > 0: return (x[0][0][0].ttl, x[0][0][1].src, max(0, int(1000 * (x[0][0][1].time - x[0][0][0].sent_time)))) else: return (x[1][0][0].ttl, '*', 0) def tcp_traceroute(destination_ip, destination_port, timeout=1): responses = takeuntil(is_finalhop, imap(partial(sr, timeout=timeout, verbose=0), traceroute_probes(destination_ip, destination_port))) return map(hop_ip_latency, responses) )而不会引入不必要的副作用?
  • 如何更改traceroute(与世界的接口)行为(例如,根据RTT改变sr中的超时)而不会陷入命令式/面向对象的陷阱?
  • 是否有一种纯粹的(r)函数式语言足以实现网络工具,同时也迫使我“更具功能性”?
  • 谁忘记将ping添加到Python的takeuntil

0 个答案:

没有答案
相关问题