|
1 | 1 | # RawSocketPy |
2 | 2 |
|
| 3 | +[API Documentation](https://rawsocket-python.readthedocs.io/en/latest/api.html) |
| 4 | + |
3 | 5 | Raw socket is a layer 2 python library for communication using the MAC addresses only. |
4 | 6 |
|
5 | 7 | This allows you to create a custom made Ethernet/WiFi communication system which is **not** using IP nor TCP/UDP or to debug custom frames such as SERCOS III, Profibus, ARP, PTP, ... |
@@ -91,29 +93,87 @@ sock.send("other data", ethertype="\xEE\xFF") # Broadcast "other data" with an e |
91 | 93 | On another machine, you can run the following: |
92 | 94 |
|
93 | 95 | ```python |
94 | | -from rawsocketpy import RawSocket, u_to_str |
| 96 | +from rawsocketpy import RawSocket, to_str |
95 | 97 |
|
96 | 98 | sock = RawSocket("wlp2s0", 0xEEFA) |
97 | 99 | packet = sock.recv() |
98 | 100 | # The type of packet is RawPacket() which allows pretty printing and unmarshal the raw data. |
99 | 101 |
|
| 102 | +# If you are using Python2, all data is encoded as unicode strings "\x01.." while Python3 uses bytearray. |
| 103 | + |
100 | 104 | print(packet) # Pretty print |
101 | | -packet.dest # unicode string "\xFF\xFF\xFF\xFF\xFF\xFF" |
102 | | -packet.src # unicode string "\x12\x12\x12\x12\x12\x13" |
103 | | -packet.type # unicode string "\xEE\xFA" |
104 | | -packegt.data # unicode string "some data" |
| 105 | +packet.dest # string "\xFF\xFF\xFF\xFF\xFF\xFF" or bytearray(b"\xFF\xFF\xFF\xFF\xFF\xFF") |
| 106 | +packet.src # string "\x12\x12\x12\x12\x12\x13" or bytearray(b"\x12\x12\x12\x12\x12\x13") |
| 107 | +packet.type # string "\xEE\xFA" or bytearray([b"\xEE\xFA"] |
| 108 | +packegt.data # string "some data" or bytearray(b"some data"] |
| 109 | + |
| 110 | +print(to_str(packet.dest)) # Human readable MAC: FF:FF:FF:FF:FF:FF |
| 111 | +print(to_str(packet.type, "")) # Human readable type: EEFA |
| 112 | +``` |
| 113 | + |
| 114 | +## Stateless blocking Server |
| 115 | + |
| 116 | +```python |
| 117 | +from rawsocketpy import RawServer |
| 118 | + |
| 119 | +class LongTaskTest(RawRequestHandler): |
| 120 | + def handle(self): |
| 121 | + time.sleep(1) |
| 122 | + print(self.packet) |
| 123 | + |
| 124 | + def finish(self): |
| 125 | + print("End") |
| 126 | + |
| 127 | + def setup(self): |
| 128 | + print("Begin") |
| 129 | + |
| 130 | +def main(): |
| 131 | + rs = RawServer("wlp2s0", 0xEEFA, LongTaskTest) |
| 132 | + rs.spin() |
| 133 | + |
| 134 | +if __name__ == '__main__': |
| 135 | + main() |
| 136 | +``` |
| 137 | + |
| 138 | +## Stateful Asynchronous server |
| 139 | + |
| 140 | +Available **only** if **gevent** is installed. |
| 141 | + |
| 142 | +```python |
| 143 | +from rawsocketpy import RawRequestHandler, RawAsyncServerCallback |
| 144 | +import time |
| 145 | + |
| 146 | +def callback(handler, server): |
| 147 | + print("callback") |
| 148 | + handler.setup() |
| 149 | + handler.handle() |
| 150 | + handler.finish() |
| 151 | + |
| 152 | + |
| 153 | +class LongTaskTest(RawRequestHandler): |
| 154 | + def handle(self): |
| 155 | + time.sleep(1) |
| 156 | + print(self.packet) |
| 157 | + |
| 158 | + def finish(self): |
| 159 | + print("End") |
| 160 | + |
| 161 | + def setup(self): |
| 162 | + print("Begin") |
| 163 | + |
| 164 | +def main(): |
| 165 | + rs = RawAsyncServerCallback("wlp2s0", 0xEEFA, LongTaskTest, callback) |
| 166 | + rs.spin() |
105 | 167 |
|
106 | | -print u_to_str(packet.dest) # Human readable MAC: FF:FF:FF:FF:FF:FF |
107 | | -print u_to_str(packet.type, "") # Human readable type: EEFA |
| 168 | +if __name__ == '__main__': |
| 169 | + main() |
108 | 170 | ``` |
109 | 171 |
|
110 | 172 | ## I want to contribue!! |
111 | 173 |
|
112 | 174 | You are free to contribue, the following capabilities are welcome: |
113 | 175 |
|
114 | 176 | - Windows compatibility |
115 | | -- Async implementation (callbacks on new data) |
116 | | -- Readthedocs documentation |
117 | 177 | - More Python versions and OS tests |
118 | 178 |
|
119 | 179 | ## Credits |
|
0 commit comments