tf_extend.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import os,sys,network,socket,time,machine,gc,tf
  2. # these helper classes let us use the tf.transfer() iterator,
  3. # by intercepting the .write()
  4. class wc():
  5. def __init__(self):
  6. self.words=self.lines=self.bytes_=0
  7. def write(self,text):
  8. self.bytes_ += len(text)
  9. self.lines += 1
  10. self.words += len(text.split())
  11. class lessor():
  12. def __init__(self):
  13. self.i=0
  14. def write(self,text):
  15. if self.i==-1: return
  16. self.i += 1
  17. sys.stdout.write(text)
  18. if self.i%30==0:
  19. sys.stdout.write("====> press <enter> to see more, N or Q to quit <====\n")
  20. g=sys.stdin.read(1)
  21. if g=='\n': g=sys.stdin.read(1)
  22. if g in "nNqQ":
  23. self.i=-1
  24. # the main entry point for the extension
  25. # returns True if the command was interpreted
  26. def cmd(args):
  27. cmd=args[0]
  28. if cmd in ('wc','more','less','exec') and len(args)<2:
  29. print("syntax: "+cmd+" <filename>")
  30. return True
  31. if cmd in ('wc','more','less'):
  32. if cmd=='wc':
  33. w = wc()
  34. else:
  35. w = lessor()
  36. try:
  37. tf.transfer(args[1],w)
  38. if cmd=='wc':
  39. print("lines: {}\twords: {}\tbytes: {}".format(w.lines, w.words, w.bytes_))
  40. except:
  41. print("file not found: "+args[1])
  42. elif cmd in ('ifconfig','ip'):
  43. ifc=network.WLAN().ifconfig()
  44. print("IP: {}\tmask: {}\tgateway: {}\tDNS: {}".format(*ifc))
  45. elif cmd in ('host','nslookup','dig'):
  46. if len(args)<2:
  47. print("syntax: host <domain.name>")
  48. else:
  49. print("host <{}> is at {}".format(args[1],socket.getaddrinfo(args[1],80)[0][-1][0]))
  50. elif cmd=='connect':
  51. if len(args)<3:
  52. print("syntax: connect <ssid> <password>")
  53. else:
  54. w=network.WLAN(network.STA_IF)
  55. w.connect(args[1],args[2])
  56. print("connecting...",end=' ')
  57. time.sleep(3)
  58. print(w.ifconfig() if w.isconnected() else "not yet connected; try 'ifconfig' in a few seconds")
  59. elif cmd=='scan':
  60. w=network.WLAN(network.STA_IF)
  61. print("scanning...")
  62. s=w.scan()
  63. if len(s)==0:
  64. print("no AP found")
  65. return True
  66. for i in s:
  67. print("ch: {}\tRSSI: {}\t{}\tSSID: {}".format(i[2],i[3],"open" if i[4]==0 else "",i[0]))
  68. elif cmd=='freq':
  69. if len(args)==1 or args[1] in ("160","80"):
  70. if len(args)>1:
  71. machine.freq(int(args[1])*1000000)
  72. print("master cpu frequency {}MHz".format(machine.freq()//1000000))
  73. else:
  74. print("syntax: freq [ 160 | 80 ]")
  75. elif cmd=='exec':
  76. try:
  77. exec(open(args[1]).read(),globals(),globals())
  78. except OSError:
  79. print("file not found")
  80. elif cmd=='free':
  81. print("memory used: {}\tmemory free:{}".format(gc.mem_alloc(),gc.mem_free()))
  82. elif cmd=='help':
  83. print("==Extended commands")
  84. print(" connect <essid> <password> \tscan")
  85. print(" ifconfig/ip \t\thost/dig/nslookup <domain.name>")
  86. print(" freq [ 160 | 80 ] \t\texec <python-filename>")
  87. print(" free \t\t\twc <filename>")
  88. print(" less/more <filename>")
  89. else: # command not found
  90. return False
  91. return True