From 3f65ce39540bcf51dce3d223b8551f15eda05d31 Mon Sep 17 00:00:00 2001 From: Rnhmjoj Date: Sun, 21 Sep 2014 17:00:56 +0200 Subject: [PATCH] Initial commit --- README.md | 29 +++++++++++++++++++++++--- hyp.py | 61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 87 insertions(+), 3 deletions(-) create mode 100644 hyp.py diff --git a/README.md b/README.md index a28bf5a..974d14d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,27 @@ -hyp -=== +#hyp +###hyperminimal https server +Also pyh(tts) backward. -Hyperminimal https server +Perfect for sharing files on the fly. +## Usage + + hyp + +Will load tls certificates inside `/usr/local/etc/openssl` and starts serving +the working directory on every interface, on port 443. + + hyp localhost 8080 + +Use custom address and port. + + hyp -t /etc/ssl + +Use another directory. + +Without any key/certificate hyp will fallback to http. + +## License + +Dual licensed under the MIT and GPL licenses: +http://www.opensource.org/licenses/mit-license.php +http://www.gnu.org/licenses/gpl.html diff --git a/hyp.py b/hyp.py new file mode 100644 index 0000000..5f593d3 --- /dev/null +++ b/hyp.py @@ -0,0 +1,61 @@ +#!/usr/bin/env python3 +import argparse +import sys +import os.path +import http.server +import ssl + + +def check_cert(dir): + try: + for i in 'cert', 'key': + open(os.path.join(dir, 'https-%s.pem' % i)).close() + except (FileNotFoundError, PermissionError) as e: + print('Could not use %s: %s. Using http only.' % (dir, e.args[1])) + return False + except Exception as e: + print('Error %d: %s' % e.args) + sys.exit(e.errno) + return True + + +def main(address, port, tls_dir): + use_tls = check_cert(tls_dir) + port = 80 if port == 443 and not use_tls else port + try: + server = http.server.HTTPServer((address, port), + http.server.SimpleHTTPRequestHandler) + if use_tls: + tls_socket = ssl.wrap_socket(server.socket, server_side=True, + certfile=os.path.join(tls_dir, 'https-cert.pem'), + keyfile=os.path.join(tls_dir,'https-key.pem'), + ssl_version=ssl.PROTOCOL_TLSv1_2) + server.socket = tls_socket + except Exception as e: + print('Error %d: %s' % e.args) + sys.exit(e.errno) + + print('Serving on %s:%d...' % (address, port)) + + try: + server.serve_forever() + except KeyboardInterrupt: + print(' Bye.') + server.socket.close() + + +if __name__ == '__main__': + parser = argparse.ArgumentParser(description="Hyperminimal https server") + parser.add_argument('address', nargs='?', + type=str, default='0.0.0.0', + help='bind ip address (default: 0.0.0.0)') + parser.add_argument('port', nargs='?', + type=int, default=443, + help='bind port number (default: 80 or 443)') + parser.add_argument('-t', '--tls', type=str, + default='/usr/local/etc/openssl', + help='cert/key couple directory. Must be PEM \ + formatted and named https-key.pem, https-cert.pem \ + (default: /usr/local/etc/openssl)') + args = parser.parse_args() + main(args.address, args.port, args.tls)