| 1 |
#!/usr/bin/env python |
| 2 |
# Helper to launch an ad-hoc http server, with or without SSL enabled, |
| 3 |
# and with or without required basic authentication |
| 4 |
# |
| 5 |
# usage: adhoc-httpd <path-to-serve> [<ssl|nossl> [<user> <password>]] |
| 6 |
# examples: |
| 7 |
# % adhoc-httpd . |
| 8 |
# % adhoc-httpd /tmp ssl |
| 9 |
# % adhoc-httpd /tmp nossl myuser yourpassword |
| 10 |
|
| 11 |
import sys |
| 12 |
from pathlib import Path |
| 13 |
|
| 14 |
from datalad.tests.utils_pytest import serve_path_via_http |
| 15 |
|
| 16 |
path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.cwd() |
| 17 |
if not path.exists(): |
| 18 |
raise ValueError(f'Path {path} does not exist') |
| 19 |
|
| 20 |
ssl = 'nossl' |
| 21 |
use_ssl = False |
| 22 |
if len(sys.argv) > 2: |
| 23 |
ssl = sys.argv[2] |
| 24 |
if ssl not in ('ssl', 'nossl'): |
| 25 |
raise ValueError('SSL argument must be "ssl" or "nossl"') |
| 26 |
use_ssl = ssl == 'ssl' |
| 27 |
auth = None |
| 28 |
if len(sys.argv) > 3: |
| 29 |
if len(sys.argv) != 5: |
| 30 |
raise ValueError( |
| 31 |
'Usage to enable authentication: ' |
| 32 |
'adhoc-httpd <path-to-serve> <ssl|nossl <user> <password>') |
| 33 |
auth = tuple(sys.argv[3:]) |
| 34 |
|
| 35 |
|
| 36 |
@serve_path_via_http(path, use_ssl=use_ssl, auth=auth) |
| 37 |
def runner(path, url): |
| 38 |
print(f'Serving {path} at {url} [{ssl}] (required authentication {auth})') |
| 39 |
input("Hit Return to stop serving") |
| 40 |
|
| 41 |
|
| 42 |
runner() |