espupload.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #!/usr/bin/python
  2. #
  3. # espupload by Theo Arends - 20170930
  4. #
  5. # Uploads binary file to OTA server
  6. #
  7. # Execute: espupload -u <Host_IP_address>:<Host_port>/<Host_path> -f <sketch.bin>
  8. #
  9. # Needs pycurl
  10. # - pip install pycurl
  11. import sys
  12. import os
  13. import optparse
  14. import logging
  15. import pycurl
  16. HOST_URL = "domus1:80/api/upload-arduino.php"
  17. def upload(hostUrl, filename):
  18. tname = os.path.normpath(os.path.dirname(filename))
  19. new_filename = tname + os.sep + os.path.basename(tname) + '.bin'
  20. if os.path.exists(new_filename):
  21. os.remove(new_filename)
  22. os.rename(filename, new_filename)
  23. url = 'http://%s' % (hostUrl)
  24. c = pycurl.Curl()
  25. c.setopt(c.URL, url)
  26. # The "Expect:" is there to suppress "Expect: 100-continue" behaviour that is
  27. # the default in libcurl when posting large bodies (and fails on lighttpd).
  28. c.setopt(c.HTTPHEADER, ["Expect:"])
  29. c.setopt(c.HTTPPOST, [('file', (c.FORM_FILE, new_filename, )), ])
  30. c.perform()
  31. c.close()
  32. def parser():
  33. parser = optparse.OptionParser(
  34. usage = "%prog [options]",
  35. description = "Upload image to over the air Host server for the esp8266 module with OTA support."
  36. )
  37. # destination ip and port
  38. group = optparse.OptionGroup(parser, "Destination")
  39. group.add_option("-u", "--host_url",
  40. dest = "host_url",
  41. action = "store",
  42. help = "Host url",
  43. default = HOST_URL
  44. )
  45. parser.add_option_group(group)
  46. # image
  47. group = optparse.OptionGroup(parser, "Image")
  48. group.add_option("-f", "--file",
  49. dest = "image",
  50. help = "Image file.",
  51. metavar = "FILE",
  52. default = None
  53. )
  54. parser.add_option_group(group)
  55. # output group
  56. group = optparse.OptionGroup(parser, "Output")
  57. group.add_option("-d", "--debug",
  58. dest = "debug",
  59. help = "Show debug output. And override loglevel with debug.",
  60. action = "store_true",
  61. default = False
  62. )
  63. parser.add_option_group(group)
  64. (options, args) = parser.parse_args()
  65. return options
  66. # end parser
  67. def main(args):
  68. # get options
  69. options = parser()
  70. # adapt log level
  71. loglevel = logging.WARNING
  72. if (options.debug):
  73. loglevel = logging.DEBUG
  74. # end if
  75. # logging
  76. logging.basicConfig(level = loglevel, format = '%(asctime)-8s [%(levelname)s]: %(message)s', datefmt = '%H:%M:%S')
  77. logging.debug("Options: %s", str(options))
  78. if (not options.host_url or not options.image):
  79. logging.critical("Not enough arguments.")
  80. return 1
  81. # end if
  82. if not os.path.exists(options.image):
  83. logging.critical('Sorry: the file %s does not exist', options.image)
  84. return 1
  85. # end if
  86. upload(options.host_url, options.image)
  87. # end main
  88. if __name__ == '__main__':
  89. sys.exit(main(sys.argv))
  90. # end if