File: adhoc-httpd

package info (click to toggle)
datalad 1.1.5-2.1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 7,140 kB
  • sloc: python: 69,392; sh: 1,521; makefile: 220
file content (42 lines) | stat: -rwxr-xr-x 1,211 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#!/usr/bin/env python
# Helper to launch an ad-hoc http server, with or without SSL enabled,
# and with or without required basic authentication
#
# usage: adhoc-httpd <path-to-serve> [<ssl|nossl> [<user> <password>]]
# examples:
#   % adhoc-httpd .
#   % adhoc-httpd /tmp ssl
#   % adhoc-httpd /tmp nossl myuser yourpassword

import sys
from pathlib import Path

from datalad.tests.utils_pytest import serve_path_via_http

path = Path(sys.argv[1]) if len(sys.argv) > 1 else Path.cwd()
if not path.exists():
    raise ValueError(f'Path {path} does not exist')

ssl = 'nossl'
use_ssl = False
if len(sys.argv) > 2:
    ssl = sys.argv[2]
    if ssl not in ('ssl', 'nossl'):
        raise ValueError('SSL argument must be "ssl" or "nossl"')
    use_ssl = ssl == 'ssl'
auth = None
if len(sys.argv) > 3:
    if len(sys.argv) != 5:
        raise ValueError(
            'Usage to enable authentication: '
            'adhoc-httpd <path-to-serve> <ssl|nossl <user> <password>')
    auth = tuple(sys.argv[3:])


@serve_path_via_http(path, use_ssl=use_ssl, auth=auth)
def runner(path, url):
    print(f'Serving {path} at {url} [{ssl}] (required authentication {auth})')
    input("Hit Return to stop serving")


runner()