tf_extend.py 3.1 KB

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