dev_server.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import web
  2. from subprocess import *
  3. urls = ('/upload', 'Upload')
  4. class Upload:
  5. def GET(self):
  6. return """<html><head></head><body>
  7. <form method="POST" enctype="multipart/form-data" action="">
  8. <input type="file" name="myfile" />
  9. <br/>
  10. <input type="submit" />
  11. </form>
  12. </body></html>"""
  13. def POST(self):
  14. obj = web.input(myfile={})
  15. filedir = '/Users/david/Devel/arch/avr/code/quickdev16/roms' # change this to the directory you want to store the file in.
  16. if 'myfile' in obj:
  17. web.debug("Upload file %s" % obj['myfile'].filename)
  18. filepath = obj.myfile.filename.replace('\\','/')
  19. filename = filepath.split('/')[-1]
  20. foutname = filedir +'/'+ filename
  21. web.debug("Write to %s" % foutname)
  22. fout = open(foutname,'w')
  23. fout.write( obj.myfile.file.read())
  24. fout.close()
  25. cmd = "ucon64 --port=usb --xsnesram %s " % foutname
  26. web.debug("Execute: %s" % cmd)
  27. p = Popen(cmd, shell=True, bufsize=128,
  28. stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True)
  29. stdout,stderr = p.communicate()
  30. return '''<html><head></head><body>Out: %s <br/>Err: %s</body></html>''' % (
  31. stdout.replace("\n","<br/>").replace("\r","<br/>"),
  32. stderr.replace("\n","<br/>"))
  33. raise web.seeother('/upload')
  34. if __name__ == "__main__":
  35. app = web.application(urls, globals())
  36. app.run()