Python 2.7 python中的搜索不起作用

Python 2.7 python中的搜索不起作用,python-2.7,Python 2.7,以下是上述功能的输入: 如果源地址和多播组地址匹配,那么我必须返回该特定IP地址的完整输出。示例:在上述情况下,我的返回值低于输出值 vrId 2 G=224.0.1.24 S=10.89.204.46 Vid 103 : (HW IPMC 0 l3hash 0 hit 0) -> Vid 103 -> 5 9 10 49 vrId 2 G=224.0.1.25 S=10.89.204.50 Vid 103 : (HW IPMC 0 l3hash 0 hit 0) ->

以下是上述功能的输入:

如果源地址和多播组地址匹配,那么我必须返回该特定IP地址的完整输出。示例:在上述情况下,我的返回值低于输出值

vrId 2 G=224.0.1.24 S=10.89.204.46 Vid 103 : (HW IPMC 0 l3hash 0 hit 0)
-> Vid 103
    -> 5 9 10 49
vrId 2 G=224.0.1.25 S=10.89.204.50 Vid 103 : (HW IPMC 0 l3hash 0 hit 0)
-> Vid 103
    -> 5 9 10 49
vrId 2 G=224.0.1.25 S=10.89.204.49 Vid 103 : (HW IPMC 0 l3hash 0 hit 1)
-> Vid 103
    -> 5 9 10 49
vrId 2 G=224.0.1.25 S=10.89.204.48 Vid 103 : (HW IPMC 0 l3hash 0 hit 0)
-> Vid 103
    -> 5 9 10 49
上面的操作非常好。现在,我希望用户输入源和多播IP地址,并将其传递给函数在表中搜索,因此我编写了以下代码

     2 G=224.0.1.25 S=10.89.204.50 Vid 103 : (HW IPMC 0 l3hash 0 hit 0)
-> Vid 103
    -> 5 9 10 49
它不会返回任何输出。 有人能建议如何解决这个问题吗?有没有其他方法可以更有效地搜索输入


谢谢。

您正在搜索字符串grp\u id和src\u ip,而不是传递给函数的变量

src_address = raw_input("\nEnter SOURCE IP address of multicast sender: ")
grp_address = raw_input("\nEnter Multicast group IP address: ")

hal_dump = cache(xos.cmd("debug hal show ipv4mc"),src_address,grp_address)

def cache(dump,src_ip,grp_ip):
 for mcast_cache in dump.split('vrId') :
    if "grp_ip" in mcast_cache:
        if "src_ip" in mcast_cache:
            return mcast_cache

非常感谢你,科林。我错过了那部分。
src_address = raw_input("\nEnter SOURCE IP address of multicast sender: ")
grp_address = raw_input("\nEnter Multicast group IP address: ")

hal_dump = cache(xos.cmd("debug hal show ipv4mc"),src_address,grp_address)

def cache(dump,src_ip,grp_ip):
 for mcast_cache in dump.split('vrId') :
    if "grp_ip" in mcast_cache:
        if "src_ip" in mcast_cache:
            return mcast_cache
src_address = raw_input("\nEnter SOURCE IP address of multicast sender: ")
grp_address = raw_input("\nEnter Multicast group IP address: ")

hal_dump = cache(xos.cmd("debug hal show ipv4mc"),src_address,grp_address)

def cache(dump,src_ip,grp_ip):
 for mcast_cache in dump.split('vrId') :
    if grp_ip in mcast_cache:
        if src_ip in mcast_cache:
            return mcast_cache