Skip to content

Commit 019b4bd

Browse files
committed
nit: Format all code and remove unused libs
1 parent f5a2cd8 commit 019b4bd

File tree

8 files changed

+93
-71
lines changed

8 files changed

+93
-71
lines changed

rawsocketpy/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@
1010

1111
from __future__ import absolute_import
1212
from __future__ import print_function
13+
1314
try:
1415
from gevent import monkey
16+
1517
monkey.patch_all()
1618
from .asyncserver import RawAsyncServer, RawAsyncServerCallback
1719
except ImportError:

rawsocketpy/asyncserver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class RawAsyncServer(RawServer):
1313
1414
This will ensure you are not loosing data because the handler is too long.
1515
"""
16+
1617
pool = pool.Pool()
1718

1819
def handle_handler(self, handler):

rawsocketpy/packet.py

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from .util import to_str
66

77

8-
class RawPacket():
8+
class RawPacket:
99
"""RawPacket is the resulting data container of the RawSocket class.
1010
1111
It reads raw data and stores the MAC source, MAC destination, the Ethernet type and the data payload.
@@ -43,7 +43,17 @@ def __init__(self, data):
4343
self.success = False
4444

4545
def __repr__(self):
46-
return "".join([to_str(self.src), " == 0x", to_str(self.type, separator=""), " => ", to_str(self.dest), " - ", "OK" if self.success else "FAILED"])
46+
return "".join(
47+
[
48+
to_str(self.src),
49+
" == 0x",
50+
to_str(self.type, separator=""),
51+
" => ",
52+
to_str(self.dest),
53+
" - ",
54+
"OK" if self.success else "FAILED",
55+
]
56+
)
4757

4858
def __str__(self):
49-
return "".join([self.__repr__(), ":\n", self.data.decode('utf-8')])
59+
return "".join([self.__repr__(), ":\n", self.data.decode("utf-8")])

rawsocketpy/server.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
# -*- coding: utf-8 -*-
33

44
from __future__ import absolute_import
5-
from .packet import RawPacket
65
from .socket import RawSocket
7-
from .util import get_hw, to_bytes, protocol_to_ethertype
86

97

108
class RawServer(object):

rawsocketpy/socket.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,16 @@
33

44
from __future__ import absolute_import
55
import socket
6-
import select
7-
import struct
8-
import time
96
from .packet import RawPacket
10-
from .util import get_hw, to_str, protocol_to_ethertype, to_bytes
7+
from .util import get_hw, protocol_to_ethertype, to_bytes
118

129

1310
class RawSocket(object):
1411
"""RawSocket is using the socket library to send raw ethernet frames, using socket.RAW_SOCK
1512
1613
It has a similar API to the socket library: send/recv/close/dup.
1714
"""
15+
1816
BROADCAST = b"\xff\xff\xff\xff\xff\xff"
1917
""":description: Default MAC address: ``"\\xff\\xff\\xff\\xff\\xff\\xff"``"""
2018

@@ -47,9 +45,13 @@ def __init__(self, interface, protocol, sock=None, no_recv_protocol=False):
4745
self.close = self.sock.close
4846

4947
def dup(self):
50-
"""Duplicates the RawSocket
51-
"""
52-
return RawSocket(self.interface, self.non_processed_protocol, self.sock.dup(), self.no_recv_protocol)
48+
"""Duplicates the RawSocket"""
49+
return RawSocket(
50+
self.interface,
51+
self.non_processed_protocol,
52+
self.sock.dup(),
53+
self.no_recv_protocol,
54+
)
5355

5456
@staticmethod
5557
def sock_create(interface, protocol, sock=None):

rawsocketpy/util.py

Lines changed: 44 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import sys
99

1010
if sys.version_info >= (3, 0):
11-
import binascii
1211

1312
def get_hw(ifname):
1413
"""Returns a bytearray containing the MAC address of the interface.
@@ -19,10 +18,13 @@ def get_hw(ifname):
1918
:rtype: bytearray
2019
"""
2120
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
22-
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack(
23-
'256s', bytearray(ifname[:15], 'utf-8')))
21+
info = fcntl.ioctl(
22+
s.fileno(), 0x8927, struct.pack("256s", bytearray(ifname[:15], "utf-8"))
23+
)
2424
return info[18:24]
25+
2526
else:
27+
2628
def get_hw(ifname):
2729
"""Returns a unicode string containing the MAC address of the interface.
2830
@@ -31,8 +33,7 @@ def get_hw(ifname):
3133
:rtype: str
3234
"""
3335
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
34-
info = fcntl.ioctl(s.fileno(), 0x8927,
35-
struct.pack('256s', ifname[:15]))
36+
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack("256s", ifname[:15]))
3637
return info[18:24]
3738

3839

@@ -68,37 +69,42 @@ def protocol_to_ethertype(protocol):
6869

6970

7071
if sys.version_info >= (3, 0):
71-
def to_bytes(*data):
72-
"""Flatten the arrays and Converts data to a bytearray
73-
74-
:param data: The data to be converted
75-
:type data: [int, bytes, bytearray, str, [int], [bytes], [bytearray], [str]]
76-
:rtype: bytearray
77-
78-
>>> to_bytes("123")
79-
b'123'
80-
>>> to_bytes(1, 2, 3)
81-
b'\\x01\\x02\\x03'
82-
>>> to_bytes("\\xff", "\\x01\\x02")
83-
b'\\xff\\x01\\x02'
84-
>>> to_bytes(1, 2, 3, [4,5,6])
85-
b'\\x01\\x02\\x03\\x04\\x05\\x06'
86-
>>> to_bytes(bytes([1,3,4]), bytearray([6,7,8]), "\\xff")
87-
b'\\x01\\x03\\x04\\x06\\x07\\x08\\xff'
88-
"""
89-
result = bytearray()
90-
for d in data:
91-
if type(d) in [tuple, list]:
92-
baa = map(to_bytes, d)
93-
for ba in baa:
94-
result += ba
95-
if type(d) is int:
96-
result += bytearray([d])
97-
if type(d) is str:
98-
result += bytearray(map(ord, d))
99-
if type(d) in [bytes, bytearray]:
100-
result += d
101-
return result
72+
73+
def to_bytes(*data):
74+
"""Flatten the arrays and Converts data to a bytearray
75+
76+
:param data: The data to be converted
77+
:type data: [int, bytes, bytearray, str, [int], [bytes], [bytearray], [str]]
78+
:rtype: bytearray
79+
80+
>>> to_bytes("123")
81+
b'123'
82+
>>> to_bytes(1, 2, 3)
83+
b'\\x01\\x02\\x03'
84+
>>> to_bytes("\\xff", "\\x01\\x02")
85+
b'\\xff\\x01\\x02'
86+
>>> to_bytes(1, 2, 3, [4,5,6])
87+
b'\\x01\\x02\\x03\\x04\\x05\\x06'
88+
>>> to_bytes(bytes([1,3,4]), bytearray([6,7,8]), "\\xff")
89+
b'\\x01\\x03\\x04\\x06\\x07\\x08\\xff'
90+
"""
91+
result = bytearray()
92+
for d in data:
93+
if type(d) in [tuple, list]:
94+
baa = map(to_bytes, d)
95+
for ba in baa:
96+
result += ba
97+
if type(d) is int:
98+
result.extend(bytearray([d]))
99+
elif type(d) is str:
100+
result.extend(bytearray(map(ord, d)))
101+
elif type(d) is bytearray:
102+
result.extend(d)
103+
elif type(d) is bytes:
104+
result += d
105+
return result
106+
102107
else:
103-
def to_bytes(*data):
104-
return bytes("".join(map(str, data)))
108+
109+
def to_bytes(*data):
110+
return bytes("".join(map(str, data)))

requirements.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
gevent==25.8.2

setup.py

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,24 +3,26 @@
33
with open("README.md", "r") as fh:
44
long_description = fh.read()
55

6-
setuptools.setup(name='rawsocketpy',
7-
version='0.4.0',
8-
description='This library allows you to implement a custom layer 2 communication using raw sockets in Python 2 and Python 3, synchronous and asynchronous, with and without callbacks.',
9-
long_description=long_description,
10-
long_description_content_type="text/markdown",
11-
url='https://github.com/AlexisTM/rawsocket_python',
12-
author='AlexisTM',
13-
author_email='alexis.paques@gmail.com',
14-
license='MIT',
15-
packages=setuptools.find_packages(),
16-
classifiers=(
17-
"Programming Language :: Python :: 2",
18-
"Programming Language :: Python :: 3",
19-
"Development Status :: 4 - Beta",
20-
"License :: OSI Approved :: MIT License",
21-
"Operating System :: POSIX :: Linux",
22-
"Topic :: Internet",
23-
"Topic :: System :: Networking",
24-
"Topic :: System :: Networking :: Monitoring",
25-
),
26-
zip_safe=False)
6+
setuptools.setup(
7+
name="rawsocketpy",
8+
version="0.4.0",
9+
description="This library allows you to implement a custom layer 2 communication using raw sockets in Python 2 and Python 3, synchronous and asynchronous, with and without callbacks.",
10+
long_description=long_description,
11+
long_description_content_type="text/markdown",
12+
url="https://github.com/AlexisTM/rawsocket_python",
13+
author="AlexisTM",
14+
author_email="alexis.paques@gmail.com",
15+
license="MIT",
16+
packages=setuptools.find_packages(),
17+
classifiers=(
18+
"Programming Language :: Python :: 2",
19+
"Programming Language :: Python :: 3",
20+
"Development Status :: 4 - Beta",
21+
"License :: OSI Approved :: MIT License",
22+
"Operating System :: POSIX :: Linux",
23+
"Topic :: Internet",
24+
"Topic :: System :: Networking",
25+
"Topic :: System :: Networking :: Monitoring",
26+
),
27+
zip_safe=False,
28+
)

0 commit comments

Comments
 (0)