-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_server.py
More file actions
40 lines (31 loc) · 1.3 KB
/
http_server.py
File metadata and controls
40 lines (31 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# In terminal run: python3 -m https.server
# Serving HTTP on :: port 8000
# Made serverdir and inside: test1.txt and test2.xlsx in the HTTP-Server directory
# bruker@MacBook-Pro-2 serverdir % ipconfig getifaddr en0
# 192.168.1.11
# python3 -m http.server 8000 -b 192.168.1.11
# RESULT: Serving HTTP on 192.168.1.11 port 8000 (http://192.168.1.11:8000/) ...
# bruker@MacBook-Pro-2 serverdir % curl 192.168.1.11:8000
# <html><body>Hello World!</body></html>%
from email.policy import HTTP
from http.server import HTTPServer, BaseHTTPRequestHandler
import time
HOST = "192.168.1.11"
PORT = 8000
class PhilipefHTTP(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes("<html><body>Hello World!</body></html>", "utf-8"))
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "application/json")
self.end_headers()
date = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(time.time()))
self.wfile.write(bytes('{"time":"' + date + '"}', "utf-8"))
server = HTTPServer((HOST, PORT), PhilipefHTTP)
print(f"Server now running...\nHOST={HOST}\nPORT={PORT}")
server.serve_forever()
server.server_close()
print("Server Stopped!")