__upload.py 977 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. #V2
  2. import sys
  3. import time
  4. from ubinascii import a2b_base64
  5. def _read_timeout(cnt, timeout_ms=2000):
  6. time_support = "ticks_ms" in dir(time)
  7. s_time = time.ticks_ms() if time_support else 0
  8. data = sys.stdin.read(cnt)
  9. if len(data) != cnt or (time_support and time.ticks_diff(time.ticks_ms(), s_time) > timeout_ms):
  10. return None
  11. return data
  12. def _upload():
  13. suc = False
  14. with open(file_name, "wb") as f:
  15. while True:
  16. d = _read_timeout(3)
  17. if not d or d[0] != "#":
  18. x = sys.stdout.write("#2")
  19. break
  20. cnt = int(d[1:3])
  21. if cnt == 0:
  22. suc = True
  23. break
  24. d = _read_timeout(cnt)
  25. if d:
  26. x = f.write(a2b_base64(d))
  27. x = sys.stdout.write("#1")
  28. else:
  29. x = sys.stdout.write("#3")
  30. break
  31. x = sys.stdout.write("#0" if suc else "#4")
  32. _upload()