Skip to content

Instantly share code, notes, and snippets.

@julzhk
Last active May 15, 2019 14:15
Show Gist options
  • Save julzhk/2c0ad8898317a8d8e43857d2cc5ffa00 to your computer and use it in GitHub Desktop.
Save julzhk/2c0ad8898317a8d8e43857d2cc5ffa00 to your computer and use it in GitHub Desktop.
simple python 3 server
#!/usr/bin/env python3
import time
from datetime import datetime
from http.server import HTTPServer, BaseHTTPRequestHandler
class Server(BaseHTTPRequestHandler):
def do_HEAD(self):
return
def do_GET(self):
self.respond()
def handle_http(self):
status = 200
content_type = "text/plain"
now = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
response_content = "now :" + now
self.send_response(status)
self.send_header('Content-type', content_type)
self.end_headers()
return bytes(response_content, "UTF-8")
def respond(self):
content = self.handle_http()
self.wfile.write(content)
HOST_NAME = 'localhost'
PORT_NUMBER = 8000
if __name__ == '__main__':
httpd = HTTPServer((HOST_NAME, PORT_NUMBER), Server)
print(time.asctime(), 'Server UP - %s:%s' % (HOST_NAME, PORT_NUMBER))
try:
httpd.serve_forever()
except KeyboardInterrupt:
pass
httpd.server_close()
print(time.asctime(), 'Server DOWN - %s:%s' % (HOST_NAME, PORT_NUMBER))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment