Created
January 10, 2016 23:31
-
-
Save grandadmiral-thrawn/16149c08e9593e281745 to your computer and use it in GitHub Desktop.
Creates a localhost server, index.html, and objects for writing to it. Lightweight, minimal.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.server | |
import os | |
import webbrowser | |
class Server(object): | |
""" serves http on port 5200""" | |
def __init__(self): | |
self.page = "" | |
pass | |
def start_server(self, port=5200, bind="", cgi=False): | |
self.page = "http://localhost:" + str(port) + "/" | |
http.server.test(HandlerClass=http.server.SimpleHTTPRequestHandler,port=port,bind=bind) | |
def launch_page(self): | |
""" launch webpage """ | |
if self.page == "": | |
self.page="http://localhost:5200/" | |
else: | |
pass | |
if "index.html" not in os.listdir(): | |
print("opening default directory, no index.html") | |
webbrowser.open_new(self.page) | |
def go(self): | |
""" launches index.html or directory """ | |
self.launch_page() | |
self.start_server() | |
def go_port(self, port): | |
""" start server on a different port""" | |
self.page = "http://localhost:" + str(port) + "/" | |
self.launch_page() | |
self.start_server(self, port=port, bind="", cgi=False) | |
class makeIndex(object): | |
def __init__(self, file="index.html"): | |
self.file = file | |
self.writer = None | |
def __enter__(self): | |
return self | |
def __exit__(self): | |
os.unlink(self.file) | |
def openWriter(self): | |
""" create an interactive writer""" | |
self.writer = open(self.file, 'w') | |
def closeWriter(self): | |
""" close an interactive writer""" | |
if self.writer.__class__.__name__ != "NoneType": | |
self.writer.close() | |
else: | |
pass | |
def writeWriter(self, content): | |
""" write with the writer""" | |
if self.writer.__class__.__name__ != "NoneType": | |
self.writer.write(content) | |
else: | |
pass | |
def writeIndex(self): | |
""" writes to index.html """ | |
f = open(self.file,'w') | |
f.write("hello") | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment