-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmockServer.py
More file actions
45 lines (37 loc) · 1.15 KB
/
mockServer.py
File metadata and controls
45 lines (37 loc) · 1.15 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
41
42
43
44
45
#!/usr/bin/env python3
"""
Simple HTTP mock server using only standard library.
Runs on port 8081.
"""
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
class MockHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type', 'application/json')
self.end_headers()
response = {
"message": "Request successful",
"path": self.path,
"status": "ok"
}
self.wfile.write(json.dumps(response).encode())
def do_POST(self):
self.do_GET()
def do_PUT(self):
self.do_GET()
def do_DELETE(self):
self.do_GET()
def log_message(self, format, *args):
# Suppress default logging, or customize it
print(f"[{self.address_string()}] {args[0]}")
if __name__ == '__main__':
port = 8081
server = HTTPServer(('localhost', port), MockHandler)
print(f"Starting mock server on http://localhost:{port}")
print("Press Ctrl+C to stop")
try:
server.serve_forever()
except KeyboardInterrupt:
print("\nShutting down mock server...")
server.shutdown()