Skip to content

Instantly share code, notes, and snippets.

@peterkaminski
Forked from bxt/webmsg.py
Last active July 9, 2019 18:06
Show Gist options
  • Save peterkaminski/d0a4857fef9f1126e1d59e4e18b80c0e to your computer and use it in GitHub Desktop.
Save peterkaminski/d0a4857fef9f1126e1d59e4e18b80c0e to your computer and use it in GitHub Desktop.
Really simple python web server to serve a maintenance message to the web
# forked from https://gist.github.com/bxt/3491401
import string,cgi,time, datetime
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
class MyHandler(BaseHTTPRequestHandler):
def do_GET(self):
try:
self.send_response(503) # let bots know whats up
self.send_header('Content-type','text/html')
self.end_headers()
self.wfile.write('<!DOCTYPE html>\n<meta charset=utf-8 />\n<title>Site Maintenance</title>\n')
# with thanks to https://gist.github.com/pitch-gist/2999707
self.wfile.write('<style> body { background:#fff8ea; text-align: center; padding: 150px; } h1 { font-size: 50px; } body { font: 20px Helvetica, sans-serif; color: #333; } article { display: block; text-align: left; width: 650px; margin: 0 auto; } a { color: #dc8100; text-decoration: none; } a:hover { color: #333; text-decoration: none; } </style>\n')
self.wfile.write('<h1>Site Maintenance</h1><p>Sorry for the inconvenience!</p></p>We are performing some maintenance at the moment.</p><p>Please check back later.</p>\n\n')
self.wfile.write('<!-- '+datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')+' // '+self.path+'-->\n')
return
except IOError:
self.send_error(500,'Internal error')
class TimeoutingHTTPServer(HTTPServer):
def finish_request(self, request, client_address):
request.settimeout(5) # Really short timeout as there is only 1 thread
HTTPServer.finish_request(self, request, client_address)
def main():
try:
server = TimeoutingHTTPServer(('',80), MyHandler)
print 'Listening...'
server.serve_forever()
except KeyboardInterrupt:
print 'quit...'
server.socket.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment