Skip to content

Commit

Permalink
Python3 compat: to_string function
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexisTM committed Aug 8, 2018
1 parent 95799af commit f88ddc5
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down
2 changes: 1 addition & 1 deletion rawsocketpy/__init__.py
Original file line number Diff line number Diff line change
@@ -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
9 changes: 5 additions & 4 deletions rawsocketpy/main.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"
Expand Down
10 changes: 7 additions & 3 deletions rawsocketpy/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f88ddc5

Please sign in to comment.