Skip to content

Commit

Permalink
clean up unused Python imports (p4lang#454)
Browse files Browse the repository at this point in the history
* Remove unused python imports

These changes have been auto-generated with:

	pip3 install isort autoflake
	isort -rc -sl .
	autoflake --remove-all-unused-imports -i -r .
	isort -rc -m 3 .

Signed-off-by: Radostin Stoyanov <[email protected]>

* python: remove redundant parenthesis

Signed-off-by: Radostin Stoyanov <[email protected]>
  • Loading branch information
rst0git authored Feb 24, 2022
1 parent 50f397b commit d84179e
Show file tree
Hide file tree
Showing 36 changed files with 175 additions and 169 deletions.
20 changes: 13 additions & 7 deletions exercises/basic/receive.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
#!/usr/bin/env python3
import sys
import struct
import os
import sys

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
from scapy.all import IP, TCP, UDP, Raw
from scapy.all import (
TCP,
FieldLenField,
FieldListField,
IntField,
IPOption,
ShortField,
get_if_list,
sniff
)
from scapy.layers.inet import _IPOption_HDR


def get_if():
ifs=get_if_list()
iface=None
Expand Down Expand Up @@ -44,7 +50,7 @@ def handle_pkt(pkt):
def main():
ifaces = [i for i in os.listdir('/sys/class/net/') if 'eth' in i]
iface = ifaces[0]
print(("sniffing on %s" % iface))
print("sniffing on %s" % iface)
sys.stdout.flush()
sniff(iface = iface,
prn = lambda x: handle_pkt(x))
Expand Down
13 changes: 5 additions & 8 deletions exercises/basic/send.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/usr/bin/env python3
import argparse
import sys
import socket
import random
import struct
import socket
import sys

from scapy.all import IP, TCP, Ether, get_if_hwaddr, get_if_list, sendp

from scapy.all import sendp, send, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP

def get_if():
ifs=get_if_list()
Expand All @@ -30,7 +27,7 @@ def main():
addr = socket.gethostbyname(sys.argv[1])
iface = get_if()

print(("sending on interface %s to %s" % (iface, str(addr))))
print("sending on interface %s to %s" % (iface, str(addr)))
pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
pkt = pkt /IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152,65535)) / sys.argv[2]
pkt.show2()
Expand Down
2 changes: 1 addition & 1 deletion exercises/basic_tunnel/myTunnel_header.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


from scapy.all import *
import sys, os

TYPE_MYTUNNEL = 0x1212
TYPE_IPV4 = 0x0800
Expand Down
12 changes: 4 additions & 8 deletions exercises/basic_tunnel/receive.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,10 @@
#!/usr/bin/env python3
import sys
import struct
import os
import sys

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
from scapy.all import IP, TCP, UDP, Raw
from scapy.layers.inet import _IPOption_HDR
from myTunnel_header import MyTunnel
from scapy.all import TCP, get_if_list, sniff


def get_if():
ifs=get_if_list()
Expand All @@ -34,7 +30,7 @@ def handle_pkt(pkt):
def main():
ifaces = [i for i in os.listdir('/sys/class/net/') if 'eth' in i]
iface = ifaces[0]
print(("sniffing on %s" % iface))
print("sniffing on %s" % iface)
sys.stdout.flush()
sniff(iface = iface,
prn = lambda x: handle_pkt(x))
Expand Down
14 changes: 5 additions & 9 deletions exercises/basic_tunnel/send.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
#!/usr/bin/env python3
import argparse
import sys
import socket
import random
import struct
import argparse
import socket

from scapy.all import sendp, send, get_if_list, get_if_hwaddr, hexdump
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP
from myTunnel_header import MyTunnel
from scapy.all import IP, TCP, Ether, get_if_hwaddr, get_if_list, sendp


def get_if():
ifs=get_if_list()
Expand All @@ -35,11 +31,11 @@ def main():
iface = get_if()

if (dst_id is not None):
print(("sending on interface {} to dst_id {}".format(iface, str(dst_id))))
print("sending on interface {} to dst_id {}".format(iface, str(dst_id)))
pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
pkt = pkt / MyTunnel(dst_id=dst_id) / IP(dst=addr) / args.message
else:
print(("sending on interface {} to IP addr {}".format(iface, str(addr))))
print("sending on interface {} to IP addr {}".format(iface, str(addr)))
pkt = Ether(src=get_if_hwaddr(iface), dst='ff:ff:ff:ff:ff:ff')
pkt = pkt / IP(dst=addr) / TCP(dport=1234, sport=random.randint(49152,65535)) / args.message

Expand Down
22 changes: 11 additions & 11 deletions exercises/calc/calc.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
#!/usr/bin/env python3

import argparse
import sys
import socket
import random
import struct
import re

from scapy.all import sendp, send, srp1
from scapy.all import Packet, hexdump
from scapy.all import Ether, StrFixedLenField, XByteField, IntField
from scapy.all import bind_layers
import readline
from scapy.all import (
Ether,
IntField,
Packet,
StrFixedLenField,
XByteField,
bind_layers,
srp1
)


class P4calc(Packet):
name = "P4calc"
Expand Down Expand Up @@ -84,7 +84,7 @@ def main():
if resp:
p4calc=resp[P4calc]
if p4calc:
print((p4calc.result))
print(p4calc.result)
else:
print("cannot find P4calc header in the packet")
else:
Expand Down
9 changes: 3 additions & 6 deletions exercises/ecn/receive.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#!/usr/bin/env python3
import sys
import struct

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import IP, UDP, Raw
from scapy.layers.inet import _IPOption_HDR
from scapy.all import get_if_list, sniff


def get_if():
ifs=get_if_list()
Expand All @@ -28,7 +25,7 @@ def handle_pkt(pkt):

def main():
iface = 'eth0'
print(("sniffing on %s" % iface))
print("sniffing on %s" % iface)
sys.stdout.flush()
sniff(filter="udp and port 4321", iface = iface,
prn = lambda x: handle_pkt(x))
Expand Down
13 changes: 3 additions & 10 deletions exercises/ecn/send.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
#!/usr/bin/env python3

import argparse
import sys
import socket
import random
import struct
import sys
from time import sleep

from scapy.all import sendp, send, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import Ether, IP, UDP
from scapy.all import IntField, FieldListField, FieldLenField, ShortField
from scapy.layers.inet import _IPOption_HDR
from scapy.all import IP, UDP, Ether, get_if_hwaddr, get_if_list, sendp

from time import sleep

def get_if():
ifs=get_if_list()
Expand Down
5 changes: 3 additions & 2 deletions exercises/link_monitor/receive.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from probe_hdrs import *


def expand(x):
yield x
while x.payload:
Expand All @@ -14,11 +15,11 @@ def handle_pkt(pkt):
print("")
for sw in data_layers:
utilization = 0 if sw.cur_time == sw.last_time else 8.0*sw.byte_cnt/(sw.cur_time - sw.last_time)
print(("Switch {} - Port {}: {} Mbps".format(sw.swid, sw.port, utilization)))
print("Switch {} - Port {}: {} Mbps".format(sw.swid, sw.port, utilization))

def main():
iface = 'eth0'
print(("sniffing on {}".format(iface)))
print("sniffing on {}".format(iface))
sniff(iface = iface,
prn = lambda x: handle_pkt(x))

Expand Down
2 changes: 2 additions & 0 deletions exercises/link_monitor/send.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
import sys
import time

from probe_hdrs import *


def main():

probe_pkt = Ether(dst='ff:ff:ff:ff:ff:ff', src=get_if_hwaddr('eth0')) / \
Expand Down
17 changes: 11 additions & 6 deletions exercises/load_balance/receive.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
#!/usr/bin/env python3
import sys
import struct
import os
import sys

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
from scapy.all import IP, UDP, Raw
from scapy.all import (
FieldLenField,
FieldListField,
IntField,
IPOption,
ShortField,
get_if_list,
sniff
)
from scapy.layers.inet import _IPOption_HDR


def get_if():
ifs=get_if_list()
iface=None
Expand Down
11 changes: 4 additions & 7 deletions exercises/load_balance/send.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
#!/usr/bin/env python3
import argparse
import sys
import socket
import random
import struct
import socket
import sys

from scapy.all import IP, TCP, Ether, get_if_hwaddr, get_if_list, sendp

from scapy.all import sendp, send, get_if_list, get_if_hwaddr
from scapy.all import Packet
from scapy.all import Ether, IP, UDP, TCP

def get_if():
ifs=get_if_list()
Expand Down
16 changes: 11 additions & 5 deletions exercises/mri/receive.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#!/usr/bin/env python3
import sys
import struct

from scapy.all import sniff, sendp, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import PacketListField, ShortField, IntField, LongField, BitField, FieldListField, FieldLenField
from scapy.all import IP, UDP, Raw
from scapy.all import (
FieldLenField,
IntField,
IPOption,
Packet,
PacketListField,
ShortField,
get_if_list,
sniff
)
from scapy.layers.inet import _IPOption_HDR


def get_if():
ifs=get_if_list()
iface=None
Expand Down
25 changes: 16 additions & 9 deletions exercises/mri/send.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
#!/usr/bin/env python3

import argparse
import sys
import socket
import random
import struct
import sys
from time import sleep

from scapy.all import sendp, send, hexdump, get_if_list, get_if_hwaddr
from scapy.all import Packet, IPOption
from scapy.all import Ether, IP, UDP
from scapy.all import IntField, FieldListField, FieldLenField, ShortField, PacketListField
from scapy.all import (
IP,
UDP,
Ether,
FieldLenField,
IntField,
IPOption,
Packet,
PacketListField,
ShortField,
get_if_hwaddr,
get_if_list,
sendp
)
from scapy.layers.inet import _IPOption_HDR

from time import sleep

def get_if():
ifs=get_if_list()
Expand Down
5 changes: 3 additions & 2 deletions exercises/p4runtime/mycontroller.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
#!/usr/bin/env python3
import argparse
import grpc
import os
import sys
from time import sleep

import grpc

# Import P4Runtime lib from parent utils dir
# Probably there's a better way of doing this.
sys.path.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../../utils/'))
import p4runtime_lib.bmv2
import p4runtime_lib.helper
from p4runtime_lib.error_utils import printGrpcError
from p4runtime_lib.switch import ShutdownAllSwitchConnections
import p4runtime_lib.helper

SWITCH_TO_HOST_PORT = 1
SWITCH_TO_SWITCH_PORT = 2
Expand Down
5 changes: 3 additions & 2 deletions exercises/p4runtime/solution/mycontroller.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
#!/usr/bin/env python3
import argparse
import grpc
import os
import sys
from time import sleep

import grpc

# Import P4Runtime lib from parent utils dir
# Probably there's a better way of doing this.
sys.path.append(
os.path.join(os.path.dirname(os.path.abspath(__file__)),
'../../utils/'))
import p4runtime_lib.bmv2
from p4runtime_lib.switch import ShutdownAllSwitchConnections
import p4runtime_lib.helper
from p4runtime_lib.switch import ShutdownAllSwitchConnections

SWITCH_TO_HOST_PORT = 1
SWITCH_TO_SWITCH_PORT = 2
Expand Down
Loading

0 comments on commit d84179e

Please sign in to comment.