Commits

Brian Thorne committed 1a488d7

Fix websocket bug where long message receives were not getting all the data. Update readme.

Comments (0)

Files changed (3)

+# Python Socket Examples
+
+
+A collection of simple Python programs that show off the capabilities of
+the socket module.
+
+Because of the nature of socket programming I've used some embedded 
+devices including a RaspberryPi, an iRacer, and an eLua board.
+
+This was presented at the 2012 [Kiwi Pycon][5] in Dunedin, New Zealand.
+
+Much has been used from the Python [socket documentation][1].
+
+## Websockets
+
+
+To really get an understanding of websockets I wrote a pure Python 
+websocket server. Developer caveat - it is not a full implementation of 
+RFC6455.
+
+
+## Bluetooth Sockets
+
+
+To demonstrate Bluetooth Sockets in Python 3.3 I used the [iRacer][2] from 
+sparkfun with:
+
+* on-board Bluetooth radio
+* RFCOMM Bluetooth...
+* Protocol is dead easy - `0xXY` where X is the direction 
+  and Y is the speed. E.G `0x16`: Direction=Forwards, mid-speed.
+
+
+There was only **ONE** mention of "bluetooth" on the Python 3.3 socket
+docs... so I had to use the source.
+
+Boiled down to creating a Bluetooth controlled iRacer car with something
+like this:
+
+    import socket
+    import time
+
+    class BluetoothCar:
+        def __init__(self, mac_address="00:12:05:XX:XX:XX"):
+            self.socket = socket.socket(
+                socket.AF_BLUETOOTH, 
+                socket.SOCK_STREAM, 
+                socket.BTPROTO_RFCOMM)
+            
+            self.socket.connect((mac_address, 1))
+            
+        def forwards(self, duration=1.0):
+            self.drive(0x16, duration)
+        ...
+        
+        def drive(self, command, duration):
+            self.socket.send(bytes([command]))
+            time.sleep(duration)
+            self.socket.send(bytes([0x00]))
+
+
+## CAN Sockets
+
+
+The **C**ontroller **A**rea **N**etwork is a bus standard designed to 
+allow microcontrollers and devices to communicate with each other. It 
+has priority based bus arbitration, reliable deterministic 
+communication. Used in cars, trucks, wheelchairs... [wikipedia][3] 
+has more info
+
+
+SocketCAN is a set of open source CAN drivers and a networking stack 
+contributed by Volkswagen Research to the Linux kernel. Although out of
+date, there are some good docs at [brown hat][4] on the low level CAN 
+API.
+
+### Raw
+
+`raw_can.py` shows sending CAN messages using a raw can socket and
+receiving a filtered selection of them.
+
+
+### BCM
+
+Required creating a patch for Python. Found in `python_bcm.patch`.
+
+## IPv6 Sockets
+
+
+There are much more thorough treatments of normal network sockets out 
+there, but for completeness I've included examples 
+`basic_socket_client.py` and `basic_socket_server.py` that will work
+together.
+
+## Misc
+
+My presentation notes are in `slideshow.md` which can be turned into 
+some sort of slideshow with pandoc:
+
+    $ pandoc -s --self-contained --webtex -i -t slidy slideshow.md -o presentation.html
+
+
+[1]: http://docs.python.org/py3k/library/socket.html
+[2]: https://www.sparkfun.com/products/11162
+[3]: http://en.wikipedia.org/wiki/CAN_bus
+[4]: http://www.brownhat.org/docs/socketcan/llcf-api.html
+[5]: http://nz.pycon.org/2012/
+

readme

-Python Socket Examples
-======================
-
-A collection of simple Python programs that show off the capabilities of the socket
-module.
-
-IPv6 Sockets
-------------
-
-There are much more thorough treatments of normal network sockets out there, but
-for completeness I've included an example :file:`basic_socket_client.py` and 
-:file:`basic_socket_server.py` that will work together.
-
-Bluetooth Sockets
------------------
-
-Querying nearby devices
-~~~~~~~~~~~~~~~~~~~~~~~
-
-From the linux command line:
-	
-	$ hcitool scan
-	Scanning ...
-		38:0A:94:2C:EA:D2	briandroid
-
-	$ obexfs -b 38:0A:94:2C:EA:D2
-
-
-
-
-CAN Sockets
------------
-
-Raw
-~~~
-
-:file:`raw_can.py` shows sending CAN messages using a raw can socket and
-receiving a filtered selection of them.
-
-BCM
-~~~
-
-
-Generating some sort of slideshow:
-
-    $ pandoc -s --self-contained --webtex -i -t slidy slideshow.md -o presentation.html
     masking_key = s.recv(4)
     #print("mask: ", masking_key, bytes_to_int(masking_key))
 
-    # finally get the data:
-    masked_data_in = s.recv(payload_length)
+    # finally get the payload data:
+    bytes_received = 0
+    masked_data_in = bytearray(payload_length)
+    while bytes_received < payload_length:
+        data_in = bytearray(s.recv(payload_length))
+        #print "Received {} bytes".format(len(data_in))
+        masked_data_in[bytes_received:bytes_received+len(data_in)] = data_in
+        
+        bytes_received += len(data_in)
+    print "Packet received of {} bytes".format(len(masked_data_in))
     data = bytearray(payload_length)
 
     # The ith byte is the XOR of byte i of the data with
 
     def __del__(self):
         self.s.close()
-