From f88ddc5f1984a46ea34f5a06cda6cc9466a80ba1 Mon Sep 17 00:00:00 2001 From: Alexis Paques Date: Wed, 8 Aug 2018 12:18:54 +0200 Subject: [PATCH] Python3 compat: to_string function --- README.md | 11 +++++------ rawsocketpy/__init__.py | 2 +- rawsocketpy/main.py | 9 +++++---- rawsocketpy/util.py | 10 +++++++--- setup.py | 1 + 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 79466fa..cfabb0d 100644 --- a/README.md +++ b/README.md @@ -7,14 +7,13 @@ This allows you to create a custom made Ethernet/WiFi communication system which Python versions tested: - [x] 2.7.x -- [ ] 3.x +- [x] 3.5.x OSes: - [ ] Linux 14.04 - [x] Linux 16.04 - [ ] Linux 18.04 -- [ ] Linux 18.04 - [ ] Windows 10 - [ ] Mac OSX @@ -54,7 +53,7 @@ On one computer: ```bash sudo python -c "from rawsocketpy import RawSocket sock = RawSocket('wlp2s0', 0xEEFA) -print(sock.recv())" +while True: print(sock.recv())" # 12:34:56:78:9A:BC == 0xEEFA => FF:FF:FF:FF:FF:FF - OK: # Boo @@ -65,7 +64,7 @@ On the second computer: ```bash sudo python -c "from rawsocketpy import RawSocket sock = RawSocket('wlp2s0', 0xEEFA) -print(sock.send('Boo'))" +>hile True: print(sock.send('Boo'))" ``` ## In-depth @@ -110,9 +109,9 @@ print u_to_str(packet.type, "") # Human readable type: EEFA You are free to contribue, the following capabilities are welcome: - Windows compatibility -- Python 3.x compatibility -- Server implementation (callbacks on new data) +- Async implementation (callbacks on new data) - Readthedocs documentation +- More Python versions and OS tests ## Credits diff --git a/rawsocketpy/__init__.py b/rawsocketpy/__init__.py index 9fde2e2..9826c12 100644 --- a/rawsocketpy/__init__.py +++ b/rawsocketpy/__init__.py @@ -1,4 +1,4 @@ # Package init -from .util import get_hw, u_to_str, protocol_to_ethertype, to_bytes +from .util import get_hw, protocol_to_ethertype, to_bytes, to_str from .main import RawPacket, RawSocket diff --git a/rawsocketpy/main.py b/rawsocketpy/main.py index 8d078ae..f173232 100644 --- a/rawsocketpy/main.py +++ b/rawsocketpy/main.py @@ -1,5 +1,5 @@ import socket, select, struct, time -from .util import get_hw, u_to_str, protocol_to_ethertype, to_bytes +from .util import get_hw, to_str, protocol_to_ethertype, to_bytes class RawPacket(): def __init__(self, data): @@ -12,14 +12,15 @@ def __init__(self, data): self.dest, self.src, self.type = data[0:6], data[6:12], data[12:14] self.data = data[14:] self.success = True - except: + except Exception as e: + print("rawsocket: ", e) self.success = False def __repr__(self): - return "".join([u_to_str(self.src), " == 0x", u_to_str(self.type, separator=""), " => ", u_to_str(self.dest), " - ", "OK" if self.success else "FAILED"]) + return "".join([to_str(self.src), " == 0x", to_str(self.type, separator=""), " => ", to_str(self.dest), " - ", "OK" if self.success else "FAILED"]) def __str__(self): - return "".join([self.__repr__(), ":\n", self.data]) + return "".join([self.__repr__(), ":\n", self.data.decode('utf-8')]) class RawSocket(object): BROADCAST = "\xff\xff\xff\xff\xff\xff" diff --git a/rawsocketpy/util.py b/rawsocketpy/util.py index 55eff59..69fd846 100644 --- a/rawsocketpy/util.py +++ b/rawsocketpy/util.py @@ -13,15 +13,19 @@ def get_hw(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytearray(ifname[:15], 'utf-8'))) return info[18:24] - else: def get_hw(ifname): s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15])) return info[18:24] -def u_to_str(data, separator=":"): - return separator.join("{:02x}".format(ord(c)) for c in data).upper() +def to_str(data, separator=":"): + if type(data) is str: + return separator.join(["{:02x}".format(ord(c)) for c in data]) + if type(data) in [bytes, bytearray]: + return separator.join(["{:02x}".format(c) for c in data]) + else: + return str(data) def protocol_to_ethertype(protocol): return chr((protocol & 0xFF00) >> 8) + chr(protocol & 0x00FF) diff --git a/setup.py b/setup.py index ec784bb..373ddd1 100644 --- a/setup.py +++ b/setup.py @@ -14,6 +14,7 @@ license='MIT', packages=setuptools.find_packages(), classifiers=( + "Programming Language :: Python :: 2", "Programming Language :: Python :: 2", "Development Status :: 4 - Beta", "License :: OSI Approved :: MIT License",