[riot-notifications] [RIOT] dist: make ng_sniffer.py more pythonic (#3151)

Martine Lenders notifications at github.com
Fri Jun 5 16:53:29 CEST 2015


@gebart when I fix for python3 until this point I get

```
$ python3 ./dist/tools/ng_sniffer/ng_sniffer.py /dev/ttyUSB1 500000 17
ifconfig 4 set chan 17
ifconfig 4 raw
ifconfig 4 promisc
Traceback (most recent call last):
  File "./dist/tools/ng_sniffer/ng_sniffer.py", line 139, in <module>
    main(sys.argv)
  File "./dist/tools/ng_sniffer/ng_sniffer.py", line 132, in main
    generate_pcap(serport, outfile)
  File "./dist/tools/ng_sniffer/ng_sniffer.py", line 79, in generate_pcap
    NETWORK))
TypeError: must be str, not bytes
```

on the `write.out()`.

My changes so far:

```diff
diff --git a/dist/tools/ng_sniffer/ng_sniffer.py b/dist/tools/ng_sniffer/ng_sniffer.py
index 2ec82fc..b4fc2c5 100755
--- a/dist/tools/ng_sniffer/ng_sniffer.py
+++ b/dist/tools/ng_sniffer/ng_sniffer.py
@@ -1,4 +1,4 @@
-#!/usr/bin/env python2
+#!/usr/bin/env python
 '''
 (C) 2012, Mariano Alvira <mar at devl.org>
 (C) 2014, Oliver Hahm <oliver.hahm at inria.fr>
@@ -30,6 +30,7 @@ OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 SUCH DAMAGE.
 '''
 
+from __future__ import print_function
 import sys
 import re
 from time import sleep, time
@@ -49,24 +50,25 @@ NETWORK = 230       # 802.15.4 no FCS
 def configure_interface(port, channel):
     line = ""
     iface = 0
-    port.write('ifconfig\n')
+    port.write(b'ifconfig\n')
     while True:
         line = port.readline()
         if line == '':
-            print >> sys.stderr, "Application has no network interface defined"
+            print("Application has no network interface defined",
+                  file=sys.stderr)
             sys.exit(2)
-        match = re.search(r'^Iface +(\d+)', line)
+        match = re.search(r'^Iface +(\d+)', line.decode())
         if match is not None:
             iface = int(match.group(1))
             break
 
     # set channel, raw mode, and promiscuous mode
-    print >> sys.stderr, 'ifconfig %d set chan %d' % (iface, channel)
-    print >> sys.stderr, 'ifconfig %d raw' % iface
-    print >> sys.stderr, 'ifconfig %d promisc' % iface
-    port.write('ifconfig %d set chan %d\n' % (iface, channel))
-    port.write('ifconfig %d raw\n' % iface)
-    port.write('ifconfig %d promisc\n' % iface)
+    print('ifconfig %d set chan %d' % (iface, channel), file=sys.stderr)
+    print('ifconfig %d raw' % iface, file=sys.stderr)
+    print('ifconfig %d promisc' % iface, file=sys.stderr)
+    port.write(('ifconfig %d set chan %d\n' % (iface, channel)).encode())
+    port.write(('ifconfig %d raw\n' % iface).encode())
+    port.write(('ifconfig %d promisc\n' % iface).encode())
 
 
 def generate_pcap(port, out):
@@ -102,9 +104,9 @@ def generate_pcap(port, out):
 
 def main(argv):
     if len(argv) < 4:
-        print >> sys.stderr, "Usage: %s tty baudrate channel [outfile]" % \
-            (argv[0])
-        print >> sys.stderr, "       channel = 11-26"
+        print("Usage: %s tty baudrate channel [outfile]" % (argv[0]),
+              file=sys.stderr)
+        print("       channel = 11-26", file=sys.stderr)
         sys.exit(2)
 
     # open serial port
@@ -112,7 +114,7 @@ def main(argv):
         serport = Serial(argv[1], argv[2], dsrdtr=0, rtscts=0,
                          timeout=1)
     except IOError:
-        print >> sys.stderr, "error opening port"
+        print("error opening port", file=sys.stderr)
         sys.exit(2)
 
     sleep(1)
@@ -129,7 +131,7 @@ def main(argv):
     try:
         generate_pcap(serport, outfile)
     except KeyboardInterrupt:
-        print ""
+        print()
         sys.exit(2)
 
 
```

---
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/3151#issuecomment-109318420
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20150605/bf8510b6/attachment-0001.html>


More information about the notifications mailing list