Package hype for PyPI

This commit is contained in:
Rnhmjoj 2015-03-29 12:57:46 +02:00
parent 9e69194a36
commit 2a5e169a4c
3 changed files with 39 additions and 7 deletions

5
hyp/__init__.py Normal file
View File

@ -0,0 +1,5 @@
from .hyp import UploadHandler, check_cert, serve
if __name__ == '__main__':
from .hyp import main
main()

View File

@ -1,13 +1,12 @@
#!/usr/bin/env python3
import argparse
import os
import sys
import ssl
import cgi
import os
import os.path as path
import http.server as http
upload_page = '''
<form method="POST" enctype="multipart/form-data">
File to upload: <input type="file" name="upfile"><br>
@ -15,6 +14,7 @@ File to upload: <input type="file" name="upfile"><br>
</form>
'''
class UploadHandler(http.BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
@ -48,6 +48,10 @@ class UploadHandler(http.BaseHTTPRequestHandler):
def check_cert(dir):
"""
check_dir(path) -> bool
Test whether dir constain a valid key/cert pair
"""
try:
for i in 'cert', 'key':
open(path.join(dir, 'https-%s.pem' % i)).close()
@ -60,7 +64,12 @@ def check_cert(dir):
return True
def main(address, port, tls_dir, upload):
def serve(address, port, tls_dir, upload):
"""
Starts serving on http[s]://address:port
tls_dir (filepath): directory which contains the key/cert pair
upload (bool): turn on/off the upload only server mode
"""
use_tls = check_cert(tls_dir)
port = 80 if port == 443 and not use_tls else port
bind = (address, port)
@ -71,10 +80,15 @@ def main(address, port, tls_dir, upload):
else:
server = http.HTTPServer(bind, http.SimpleHTTPRequestHandler)
if use_tls:
try:
protocol = ssl.PROTOCOL_TLSv1_2
except AttributeError:
print('hyp needs TLSv1.2. You must have openssl >= 1.0.1')
sys.exit(1)
tls_socket = ssl.wrap_socket(server.socket, server_side=True,
certfile=path.join(tls_dir, 'https-cert.pem'),
keyfile=path.join(tls_dir,'https-key.pem'),
ssl_version=ssl.PROTOCOL_TLSv1_2)
ssl_version=protocol)
server.socket = tls_socket
except Exception as e:
print('Error %d: %s' % e.args)
@ -89,7 +103,7 @@ def main(address, port, tls_dir, upload):
server.socket.close()
if __name__ == '__main__':
def main():
parser = argparse.ArgumentParser(description='Hyperminimal https server')
parser.add_argument('address', nargs='?',
type=str, default='0.0.0.0',
@ -102,8 +116,8 @@ if __name__ == '__main__':
parser.add_argument('-t', '--tls',
type=str, dest='tls_dir', metavar='DIR',
default='/usr/local/etc/openssl',
help='cert/key couple directory. Must be PEM '
help='cert/key pair directory. Must be PEM '
'formatted and named https-key.pem, https-cert.pem '
'(default: /usr/local/etc/openssl)')
args = parser.parse_args()
main(**vars(args))
serve(**vars(args))

13
setup.py Normal file
View File

@ -0,0 +1,13 @@
from setuptools import setup
setup(name='hyp',
version='1.0.0',
description='Hyperminimal https server',
url='http://github.com/rnhmjoj/hyp',
author='Michele Guerini Rocco',
license='MIT-GPL',
packages=['hyp'],
entry_points={
'console_scripts': ['hyp = hyp.hyp:main']
},
)