在Django模板中解析/ proc / net / route输出

时间:2019-01-26 12:59:38

标签: django python-3.x django-templates

我有以下python脚本来解析/ proc / net / route文件的输出,当我在shell中运行它时,它可以正常工作。该代码为表中的每个路由条目返回一个单独的列表

我需要在Django模板的表中显示此脚本的输出。我尝试使用一个for循环,但它不显示任何内容。

def routes(request):
    with open("/proc/net/route") as fh:
        next(fh)
        for line in fh:
            routes = line.strip().split()
            destination = socket.inet_ntoa(struct.pack("<L", int(routes[1], 16)))
            gateway = socket.inet_ntoa(struct.pack("<L", int(routes[2], 16)))
            mask = socket.inet_ntoa(struct.pack("<L", int(routes[7], 16)))
            metric = routes[6]
            interface = routes[0]
    context_routes = {'routes': routes }
    return render(request, 'lwp_admin/routes.html', context_routes )

脚本的CLI输出:

0.0.0.0 192.168.1.1 0.0.0.0 100 enp1s0
172.17.0.0 0.0.0.0 255.255.0.0 0 docker0
192.168.1.0 0.0.0.0 255.255.255.0 100 enp1s0
192.168.34.0 0.0.0.0 255.255.255.0 0 vmnet1
192.168.64.0 0.0.0.0 255.255.255.0 0 vmnet8
192.168.122.0 0.0.0.0 255.255.255.0 0 virbr0

我希望此输出显示在表的Django模板中。

Django模板代码:

<table class="table table-bordered table-responsive table-striped table-condensed">
                  <thead class="bg-maroon-gradient">
                    <tr>
                        <th scope="col" class="col-xs-1 text-center">Destination</th>
                        <th scope="col" class="col-xs-1 text-center">Subnet mask</th>
                        <th scope="col" class="col-xs-1 text-center">Gateway</th>
                        <th scope="col" class="col-xs-1 text-center">Metric</th>
                        <th scope="col" class="col-xs-1 text-center">Interface</th>
                    </tr>
                  </thead>
                  <tbody>
                      {% for route in routes %}
                      <tr class="text-center">
                              <td> {{ route }}</td>
                      </tr>
                      {% endfor %}
                  </tbody>
              </table>

1 个答案:

答案 0 :(得分:0)

我自己解决了这个问题。 这是Django视图代码:

    with open("/proc/net/route") as fh:
    next(fh)
    route_list = []
    for line in fh:
        routes = line.strip().split()
        destination = socket.inet_ntoa(struct.pack("<L", int(routes[1], 16)))
        gateway = socket.inet_ntoa(struct.pack("<L", int(routes[2], 16)))
        mask = socket.inet_ntoa(struct.pack("<L", int(routes[7], 16)))
        metric = routes[6]
        interface = routes[0]
        route_table = (destination + ' ' + gateway + ' ' + mask + ' ' + metric + ' ' + interface).split()
        route_list.append(route_table)
context_routes = {'route_list': route_list, 'arp_list': arp_list }
return render(request, 'lwp_admin/routes.html', context_routes )

这是Django模板部分:

<table class="table table-bordered table-responsive table-striped table-condensed">
              <thead class="bg-maroon-gradient">
                <tr>
                    <th scope="col" class="col-xs-1 text-center">Destination</th>
                    <th scope="col" class="col-xs-1 text-center">Gateway</th>
                    <th scope="col" class="col-xs-1 text-center">Subnet mask</th>
                    <th scope="col" class="col-xs-1 text-center">Metric</th>
                    <th scope="col" class="col-xs-1 text-center">Interface</th>
                </tr>
              </thead>
              <tbody>
              {% for routes in route_list %}
                  <tr class="text-center">
                    {% for items in routes %}
                        <td> {{ items }}</td>
                    {% endfor %}
                  </tr>
                {% endfor %}
              </tbody>
          </table>