Skip to content

Instantly share code, notes, and snippets.

@b3z
Last active February 1, 2021 11:36
Show Gist options
  • Save b3z/a66c80e5f829f16a48e2bdcd4a80914c to your computer and use it in GitHub Desktop.
Save b3z/a66c80e5f829f16a48e2bdcd4a80914c to your computer and use it in GitHub Desktop.
Tiny Python Webserver
#!/usr/bin/env python3
# A tiny webserver in python
# ready to run!
import os
import configparser
import socket
import sys
from http.server import HTTPServer, CGIHTTPRequestHandler
version = "1.0.0"
class Config:
file = "pyny.config"
section = "pyny Web"
port = 8080
root = "public"
@staticmethod
def load():
c = configparser.ConfigParser()
c.read(Config.file)
return c
def run():
# load config if exists
if os.path.isfile(Config.file):
configuration = {}
config = Config.load()
options = config.options(Config.section)
for option in options:
try:
configuration[option] = config.get(Config.section, option)
except:
raise Exception(f'Config File broken. Check {configuration[option]}.')
#apply
Config.root = configuration['webroot']
Config.port = int(configuration['port'])
else:
# create new config file if not
conf = configparser.ConfigParser()
conf.add_section(Config.section)
conf.set(Config.section, 'webroot', str(Config.root))
conf.set(Config.section, 'port', str(Config.port))
conffile = open(Config.file, 'w')
conf.write(conffile)
conffile.close()
# check & set web root
if not os.path.isdir(Config.root):
os.makedirs(Config.root)
os.chdir(Config.root)
# check if port is available
if socket.socket(socket.AF_INET, socket.SOCK_STREAM).connect_ex(("127.0.0.1", Config.port)) == 0:
raise ConnectionError(f'Port {Config.port} is not available.')
print(f'Webserver available at http://127.0.0.1:{Config.port}')
httpServer = HTTPServer(('', Config.port), CGIHTTPRequestHandler)
httpServer.serve_forever()
print(" __ __ _ \n _ __ _ _ _ __ _ _ / / /\ \ \___| |__ \n| '_ \| | | | '_ \| | | | \ \/ \/ / _ \ '_ \ \n| |_) | |_| | | | | |_| | \ /\ / __/ |_) |\n| .__/ \__, |_| |_|\__, | \/ \/ \___|_.__/ \n|_| |___/ |___/\n\n")
print(f'Version {version}\n')
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment