Package hype for PyPI
This commit is contained in:
parent
9e69194a36
commit
2a5e169a4c
5
hyp/__init__.py
Normal file
5
hyp/__init__.py
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
from .hyp import UploadHandler, check_cert, serve
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
from .hyp import main
|
||||||
|
main()
|
@ -1,13 +1,12 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import argparse
|
import argparse
|
||||||
|
import os
|
||||||
import sys
|
import sys
|
||||||
import ssl
|
import ssl
|
||||||
import cgi
|
import cgi
|
||||||
import os
|
|
||||||
import os.path as path
|
import os.path as path
|
||||||
import http.server as http
|
import http.server as http
|
||||||
|
|
||||||
|
|
||||||
upload_page = '''
|
upload_page = '''
|
||||||
<form method="POST" enctype="multipart/form-data">
|
<form method="POST" enctype="multipart/form-data">
|
||||||
File to upload: <input type="file" name="upfile"><br>
|
File to upload: <input type="file" name="upfile"><br>
|
||||||
@ -15,6 +14,7 @@ File to upload: <input type="file" name="upfile"><br>
|
|||||||
</form>
|
</form>
|
||||||
'''
|
'''
|
||||||
|
|
||||||
|
|
||||||
class UploadHandler(http.BaseHTTPRequestHandler):
|
class UploadHandler(http.BaseHTTPRequestHandler):
|
||||||
def do_GET(self):
|
def do_GET(self):
|
||||||
self.send_response(200)
|
self.send_response(200)
|
||||||
@ -48,6 +48,10 @@ class UploadHandler(http.BaseHTTPRequestHandler):
|
|||||||
|
|
||||||
|
|
||||||
def check_cert(dir):
|
def check_cert(dir):
|
||||||
|
"""
|
||||||
|
check_dir(path) -> bool
|
||||||
|
Test whether dir constain a valid key/cert pair
|
||||||
|
"""
|
||||||
try:
|
try:
|
||||||
for i in 'cert', 'key':
|
for i in 'cert', 'key':
|
||||||
open(path.join(dir, 'https-%s.pem' % i)).close()
|
open(path.join(dir, 'https-%s.pem' % i)).close()
|
||||||
@ -60,7 +64,12 @@ def check_cert(dir):
|
|||||||
return True
|
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)
|
use_tls = check_cert(tls_dir)
|
||||||
port = 80 if port == 443 and not use_tls else port
|
port = 80 if port == 443 and not use_tls else port
|
||||||
bind = (address, port)
|
bind = (address, port)
|
||||||
@ -71,10 +80,15 @@ def main(address, port, tls_dir, upload):
|
|||||||
else:
|
else:
|
||||||
server = http.HTTPServer(bind, http.SimpleHTTPRequestHandler)
|
server = http.HTTPServer(bind, http.SimpleHTTPRequestHandler)
|
||||||
if use_tls:
|
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,
|
tls_socket = ssl.wrap_socket(server.socket, server_side=True,
|
||||||
certfile=path.join(tls_dir, 'https-cert.pem'),
|
certfile=path.join(tls_dir, 'https-cert.pem'),
|
||||||
keyfile=path.join(tls_dir,'https-key.pem'),
|
keyfile=path.join(tls_dir,'https-key.pem'),
|
||||||
ssl_version=ssl.PROTOCOL_TLSv1_2)
|
ssl_version=protocol)
|
||||||
server.socket = tls_socket
|
server.socket = tls_socket
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print('Error %d: %s' % e.args)
|
print('Error %d: %s' % e.args)
|
||||||
@ -89,7 +103,7 @@ def main(address, port, tls_dir, upload):
|
|||||||
server.socket.close()
|
server.socket.close()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
def main():
|
||||||
parser = argparse.ArgumentParser(description='Hyperminimal https server')
|
parser = argparse.ArgumentParser(description='Hyperminimal https server')
|
||||||
parser.add_argument('address', nargs='?',
|
parser.add_argument('address', nargs='?',
|
||||||
type=str, default='0.0.0.0',
|
type=str, default='0.0.0.0',
|
||||||
@ -102,8 +116,8 @@ if __name__ == '__main__':
|
|||||||
parser.add_argument('-t', '--tls',
|
parser.add_argument('-t', '--tls',
|
||||||
type=str, dest='tls_dir', metavar='DIR',
|
type=str, dest='tls_dir', metavar='DIR',
|
||||||
default='/usr/local/etc/openssl',
|
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 '
|
'formatted and named https-key.pem, https-cert.pem '
|
||||||
'(default: /usr/local/etc/openssl)')
|
'(default: /usr/local/etc/openssl)')
|
||||||
args = parser.parse_args()
|
args = parser.parse_args()
|
||||||
main(**vars(args))
|
serve(**vars(args))
|
13
setup.py
Normal file
13
setup.py
Normal 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']
|
||||||
|
},
|
||||||
|
)
|
Loading…
Reference in New Issue
Block a user