diff options
author | neodarz <neodarz@neodarz.net> | 2019-05-26 18:02:19 +0200 |
---|---|---|
committer | neodarz <neodarz@neodarz.net> | 2019-05-26 18:02:19 +0200 |
commit | 8e170c5881d78ae9b46149a7e32bab28749dec40 (patch) | |
tree | d8364d849201ddb50632e5b9febcd435882e99ff | |
parent | a23bf5af6e7be3fcea03e4b81f4d54a2c916e087 (diff) | |
download | my_new_personal_website-8e170c5881d78ae9b46149a7e32bab28749dec40.tar.xz my_new_personal_website-8e170c5881d78ae9b46149a7e32bab28749dec40.zip |
Move HTTPServerProcess to external file
-rwxr-xr-x | pyblog | 40 | ||||
-rw-r--r-- | utils/utils.py | 42 |
2 files changed, 43 insertions, 39 deletions
@@ -53,50 +53,12 @@ from generators import generators from cli import cli -class HTTPServerProcess(multiprocessing.Process): - """This class can be used to run an HTTP server.""" - - def __init__(self, rootdir): - """Initialize the HTTPServerProcess class. - - Parameters - ---------- - rootdir : str - The root directory to serve from. - - """ - - super().__init__() - self.rootdir = rootdir - - def run(self): - """Create an HTTP server and serve forever. - - Runs on localhost. The default port is 8000; if it is not - available, a random port is used instead. - """ - - os.chdir(self.rootdir) - # pylint: disable=invalid-name - HandlerClass = http.server.SimpleHTTPRequestHandler - try: - httpd = http.server.HTTPServer(("", 8001), HandlerClass) - except OSError: - httpd = http.server.HTTPServer(("", 0), HandlerClass) - _, portnumber = httpd.socket.getsockname() - sys.stderr.write("server serving on http://localhost:%d\n" % portnumber) - try: - httpd.serve_forever() - except KeyboardInterrupt: - httpd.shutdown() - - def preview(args): """Serve the blog and auto regenerate upon changes.""" # pylint: disable=unused-argument - server_process = HTTPServerProcess(BUILDDIR) + server_process = utils.HTTPServerProcess(BUILDDIR) server_process.start() sys.stderr.write("watching for changes\n") sys.stderr.write("send SIGINT to stop\n") diff --git a/utils/utils.py b/utils/utils.py index db4bab57..1fdea5d9 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -14,6 +14,10 @@ import re import lxml.etree as ET +import http.server +import multiprocessing +import os +import sys @contextmanager def init_colorama(): @@ -215,3 +219,41 @@ def sanitize(string): string = re.sub(r"\s+", "-", string) # percent encode the result return urllib.parse.quote(string) + + +class HTTPServerProcess(multiprocessing.Process): + """This class can be used to run an HTTP server.""" + + def __init__(self, rootdir): + """Initialize the HTTPServerProcess class. + + Parameters + ---------- + rootdir : str + The root directory to serve from. + + """ + + super().__init__() + self.rootdir = rootdir + + def run(self): + """Create an HTTP server and serve forever. + + Runs on localhost. The default port is 8000; if it is not + available, a random port is used instead. + """ + + os.chdir(self.rootdir) + # pylint: disable=invalid-name + HandlerClass = http.server.SimpleHTTPRequestHandler + try: + httpd = http.server.HTTPServer(("", 8001), HandlerClass) + except OSError: + httpd = http.server.HTTPServer(("", 0), HandlerClass) + _, portnumber = httpd.socket.getsockname() + sys.stderr.write("server serving on http://localhost:%d\n" % portnumber) + try: + httpd.serve_forever() + except KeyboardInterrupt: + httpd.shutdown() |