#!/usr/bin/python2

import mimetypes
import os
import sys
import time
import BaseHTTPServer

HOST = "localhost"
PORT = 8000

class MyHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
	if self.path=="/":
            self.send_response(200)
	    self.send_header('Content-type', 'text/html')
            self.end_headers()
	    self.wfile.write('<html><body>')
	    self.wfile.write('<a href="file:///path/to/file/foo b\xE1r test.txt" download>Download local file</a><br>')
	    self.wfile.write('<a href="foo b\xE1r test.txt" download>Download from server</a><br>')
	    self.wfile.write('<a href="foo b\xE1r test.txt" download="foo b\xE1r test.txt">Download from server with filename</a>')
            self.wfile.write('<form method="get" action="download">')
	    self.wfile.write('<button type="submit">Download attachment</button>')
            self.wfile.write('</form>')
	    self.wfile.write('</body></html>')
	else:
	    self.send_response(200)
	    self.send_header('Content-type', 'text/html')
            self.send_header('Content-Disposition', 'attachment; filename="foo b\xE1r test.txt"')
            self.end_headers()


if __name__ == '__main__':
    server_class = BaseHTTPServer.HTTPServer
    httpd = server_class((HOST, PORT), MyHandler)
    print("%s Server Starts = %s %s" % (time.asctime(), HOST, PORT))
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        pass
    httpd.server_close()
    print("%s Server Stops = %s %s " % (time.asctime(), HOST, PORT))

