Last active
May 15, 2019 14:15
-
-
Save julzhk/2c0ad8898317a8d8e43857d2cc5ffa00 to your computer and use it in GitHub Desktop.
simple python 3 server
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
#!/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