Skip to content

Commit

Permalink
migrate to poetry
Browse files Browse the repository at this point in the history
  • Loading branch information
Granitosaurus committed Dec 22, 2021
1 parent f2ec508 commit b026fac
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 64 deletions.
12 changes: 9 additions & 3 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ A simple server for serving directories via http or https and BASIC authorizatio
Start http server with basic authentication current directory.

Options:
--https use https
--dir TEXT use different directory
--help Show this message and exit.
-d, --dir TEXT use different directory
-s, --https use https
-t, --thread serve each request in a different thread
--help Show this message and exit.

* Free software: GNU General Public License v3

Expand All @@ -45,3 +46,8 @@ You can specify port and ip to serve on with 3rd and 4th arguments::

$ sauth someuser somepass 127.0.0.1 1234
Serving "/home/user/somedir" directory on http://127.0.0.1:1234

Threading is also supported through `-t` or `--thread` flags:

$ sauth someuser somepass 127.0.0.1 1234 --thread
Serving "/home/user/somedir" directory on http://127.0.0.1:1234 using threading
83 changes: 83 additions & 0 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
[tool.poetry]
name = "sauth"
version = "1.1.0"
description = "simple server for serving directories via http or https and BASIC authorization"
authors = ["granitosaurus <bernardas.alisauskas@pm.me>"]
readme = "README.rst"
homepage = "https://github.com/Granitosaurus/sauth/"
repository = "https://github.com/Granitosaurus/sauth/"
license = "GPL-3.0-or-later"
keywords = [
"http","server","authentication",
]
classifiers = [
"Intended Audience :: Developers",
"Natural Language :: English",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.8",
"Programming Language :: Python :: 3.7",
"Programming Language :: Python :: 3.6",
]

[tool.poetry.dependencies]
python = "^3.6"
click = "^8.0.3"

[tool.poetry.dev-dependencies]

[tool.poetry.scripts]
sauth = 'sauth:main'

[tool.black]
line-length = 120
target-version = ['py36', 'py37', 'py38', 'py39']

[build-system]
requires = ["poetry-core>=1.0.0"]
build-backend = "poetry.core.masonry.api"
73 changes: 47 additions & 26 deletions sauth.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
license="GNU General Public License v3"
"""

__version__ = '1.0.1'
__prog__ = 'sauth'
__version__ = "1.1.0"
__prog__ = "sauth"

from http.server import SimpleHTTPRequestHandler, HTTPServer
import os
Expand All @@ -18,19 +18,21 @@

CERT_FILE = os.path.expanduser("~/.ssh/cert.pem")
KEY_FILE = os.path.expanduser("~/.ssh/key.pem")
SSL_CMD = "openssl req -newkey rsa:2048 -new -nodes -x509 " \
"-days 3650 -keyout {0} -out {1}".format(KEY_FILE, CERT_FILE)
SSL_CMD = "openssl req -newkey rsa:2048 -new -nodes -x509 " "-days 3650 -keyout {0} -out {1}".format(
KEY_FILE, CERT_FILE
)


class SimpleHTTPAuthHandler(SimpleHTTPRequestHandler):
"""Main class to present webpages and authentication."""
username = ''
password = ''

username = ""
password = ""

def __init__(self, request, client_address, server):
key = '{}:{}'.format(self.username, self.password).encode('ascii')
key = "{}:{}".format(self.username, self.password).encode("ascii")
self.key = base64.b64encode(key)
self.valid_header = b'Basic ' + self.key
self.valid_header = b"Basic " + self.key
super().__init__(request, client_address, server)

def do_HEAD(self):
Expand All @@ -44,13 +46,13 @@ def do_authhead(self):
"""do authentication"""
print("send header")
self.send_response(401)
self.send_header("WWW-Authenticate", "Basic realm=\"Test\"")
self.send_header("WWW-Authenticate", 'Basic realm="Test"')
self.send_header("Content-type", "text/html")
self.end_headers()

def do_GET(self):
"""Present frontpage with user authentication."""
auth_header = self.headers.get('Authorization', '').encode('ascii')
auth_header = self.headers.get("Authorization", "").encode("ascii")
if auth_header is None:
self.do_authhead()
self.wfile.write(b"no auth header received")
Expand All @@ -66,23 +68,36 @@ class ThreadingSimpleServer(socketserver.ThreadingMixIn, HTTPServer):
"""
Not to be confused with http.server.ThreadingHTTPServer that appears in 3.7
"""
pass


def serve_http(ip="", port=80, https=True, start_dir=None, handler_class=SimpleHTTPAuthHandler):
def serve_http(
ip="",
port=80,
https=True,
start_dir=None,
use_threads=False,
):
"""setting up server"""

if https:
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=KEY_FILE,
certfile=CERT_FILE, server_side=True)
httpd.socket = ssl.wrap_socket(httpd.socket, keyfile=KEY_FILE, certfile=CERT_FILE, server_side=True)

if start_dir:
print("Changing dir to {cd}".format(cd=start_dir))
os.chdir(start_dir)

server = ThreadingSimpleServer(('0.0.0.0', port), SimpleHTTPAuthHandler)
print('Serving "{}" directory on {}://{}:{}'.format(
os.getcwd(), 'https' if https else 'http', ip, port)
if use_threads:
server = ThreadingSimpleServer((ip, port), SimpleHTTPAuthHandler)
else:
server = HTTPServer((ip, port), SimpleHTTPAuthHandler)
print(
'Serving "{}" directory on {}://{}:{} {}'.format(
os.getcwd(),
"https" if https else "http",
ip,
port,
"using threading" if use_threads else "",
).strip()
)
try:
while 1:
Expand All @@ -93,13 +108,20 @@ def serve_http(ip="", port=80, https=True, start_dir=None, handler_class=SimpleH


@click.command()
@click.argument('username')
@click.argument('password')
@click.argument('ip', default='0.0.0.0')
@click.argument('port', default=8333)
@click.option('--https', help='use https', is_flag=True)
@click.option('--dir', help='use different directory')
def main(dir, ip, port, username, password, https):
@click.argument("username")
@click.argument("password")
@click.argument("ip", default="0.0.0.0")
@click.argument("port", default=8333)
@click.option("-d", "--dir", help="use different directory")
@click.option("-s", "--https", help="use https", is_flag=True)
@click.option(
"-t",
"--thread",
"use_threads",
is_flag=True,
help="serve each request in a different thread",
)
def main(dir, ip, port, username, password, https, use_threads):
"""
Start http server with basic authentication current directory.
"""
Expand All @@ -112,8 +134,7 @@ def main(dir, ip, port, username, password, https):

SimpleHTTPAuthHandler.username = username
SimpleHTTPAuthHandler.password = password
serve_http(ip=ip, port=port, https=https,
start_dir=dir, handler_class=SimpleHTTPAuthHandler)
serve_http(ip=ip, port=port, https=https, start_dir=dir, use_threads=use_threads)


if __name__ == "__main__":
Expand Down
35 changes: 0 additions & 35 deletions setup.py

This file was deleted.

0 comments on commit b026fac

Please sign in to comment.