Skip to content

Commit f88ddc5

Browse files
committed
Python3 compat: to_string function
1 parent 95799af commit f88ddc5

File tree

5 files changed

+19
-14
lines changed

5 files changed

+19
-14
lines changed

README.md

+5-6
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,13 @@ This allows you to create a custom made Ethernet/WiFi communication system which
77
Python versions tested:
88

99
- [x] 2.7.x
10-
- [ ] 3.x
10+
- [x] 3.5.x
1111

1212
OSes:
1313

1414
- [ ] Linux 14.04
1515
- [x] Linux 16.04
1616
- [ ] Linux 18.04
17-
- [ ] Linux 18.04
1817
- [ ] Windows 10
1918
- [ ] Mac OSX
2019

@@ -54,7 +53,7 @@ On one computer:
5453
```bash
5554
sudo python -c "from rawsocketpy import RawSocket
5655
sock = RawSocket('wlp2s0', 0xEEFA)
57-
print(sock.recv())"
56+
while True: print(sock.recv())"
5857

5958
# 12:34:56:78:9A:BC == 0xEEFA => FF:FF:FF:FF:FF:FF - OK:
6059
# Boo
@@ -65,7 +64,7 @@ On the second computer:
6564
```bash
6665
sudo python -c "from rawsocketpy import RawSocket
6766
sock = RawSocket('wlp2s0', 0xEEFA)
68-
print(sock.send('Boo'))"
67+
>hile True: print(sock.send('Boo'))"
6968
```
7069

7170
## In-depth
@@ -110,9 +109,9 @@ print u_to_str(packet.type, "") # Human readable type: EEFA
110109
You are free to contribue, the following capabilities are welcome:
111110

112111
- Windows compatibility
113-
- Python 3.x compatibility
114-
- Server implementation (callbacks on new data)
112+
- Async implementation (callbacks on new data)
115113
- Readthedocs documentation
114+
- More Python versions and OS tests
116115

117116
## Credits
118117

rawsocketpy/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
# Package init
22

3-
from .util import get_hw, u_to_str, protocol_to_ethertype, to_bytes
3+
from .util import get_hw, protocol_to_ethertype, to_bytes, to_str
44
from .main import RawPacket, RawSocket

rawsocketpy/main.py

+5-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import socket, select, struct, time
2-
from .util import get_hw, u_to_str, protocol_to_ethertype, to_bytes
2+
from .util import get_hw, to_str, protocol_to_ethertype, to_bytes
33

44
class RawPacket():
55
def __init__(self, data):
@@ -12,14 +12,15 @@ def __init__(self, data):
1212
self.dest, self.src, self.type = data[0:6], data[6:12], data[12:14]
1313
self.data = data[14:]
1414
self.success = True
15-
except:
15+
except Exception as e:
16+
print("rawsocket: ", e)
1617
self.success = False
1718

1819
def __repr__(self):
19-
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"])
20+
return "".join([to_str(self.src), " == 0x", to_str(self.type, separator=""), " => ", to_str(self.dest), " - ", "OK" if self.success else "FAILED"])
2021

2122
def __str__(self):
22-
return "".join([self.__repr__(), ":\n", self.data])
23+
return "".join([self.__repr__(), ":\n", self.data.decode('utf-8')])
2324

2425
class RawSocket(object):
2526
BROADCAST = "\xff\xff\xff\xff\xff\xff"

rawsocketpy/util.py

+7-3
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,19 @@ def get_hw(ifname):
1313
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
1414
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', bytearray(ifname[:15], 'utf-8')))
1515
return info[18:24]
16-
1716
else:
1817
def get_hw(ifname):
1918
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
2019
info = fcntl.ioctl(s.fileno(), 0x8927, struct.pack('256s', ifname[:15]))
2120
return info[18:24]
2221

23-
def u_to_str(data, separator=":"):
24-
return separator.join("{:02x}".format(ord(c)) for c in data).upper()
22+
def to_str(data, separator=":"):
23+
if type(data) is str:
24+
return separator.join(["{:02x}".format(ord(c)) for c in data])
25+
if type(data) in [bytes, bytearray]:
26+
return separator.join(["{:02x}".format(c) for c in data])
27+
else:
28+
return str(data)
2529

2630
def protocol_to_ethertype(protocol):
2731
return chr((protocol & 0xFF00) >> 8) + chr(protocol & 0x00FF)

setup.py

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
license='MIT',
1515
packages=setuptools.find_packages(),
1616
classifiers=(
17+
"Programming Language :: Python :: 2",
1718
"Programming Language :: Python :: 2",
1819
"Development Status :: 4 - Beta",
1920
"License :: OSI Approved :: MIT License",

0 commit comments

Comments
 (0)