Last active
September 26, 2023 00:09
-
-
Save DevBramz/3e6f5cb26ed5fa3cd84ff6bd79635757 to your computer and use it in GitHub Desktop.
simple db server
This file contains 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
import json | |
from urllib.parse import urlparse | |
from urllib import parse | |
import logging | |
import re | |
from http.server import BaseHTTPRequestHandler, HTTPServer | |
hostName = "localhost" | |
serverPort = 4000 | |
db=[] | |
class MyServer(BaseHTTPRequestHandler): | |
def do_POST(self): | |
pass | |
def do_GET(self): | |
parsed = urlparse(self.path) | |
# get the query string | |
query_string = parsed.query | |
params=parsed.params | |
print(query_string) | |
print(params) | |
url=self.path | |
path = parsed.path | |
# check the path if it is a et | |
if path=="/set": | |
data=dict(parse.parse_qsl(parse.urlsplit(url).query)) | |
db.append(data) | |
self.send_response(200, "Adding element") | |
self.send_response(200) | |
self.send_header("Content-type", "text/html") | |
self.end_headers() | |
self.wfile.write(bytes("<p>Adding Data: %s</p>" % data, "utf-8")) | |
# check the path if it is a get | |
elif path =="/get": | |
data=dict(parse.parse_qsl(parse.urlsplit(url).query)) | |
if data in db: | |
self.send_response(200, "ELEMET EXists") | |
self.send_response(200) | |
self.send_header("Content-type", "text/html") | |
self.end_headers() | |
self.wfile.write(bytes("<p>Data found: %s</p>" % data, "utf-8")) | |
else: | |
self.send_response(200, "ELEMET EXists") | |
self.send_header("Content-type", "text/html") | |
self.end_headers() | |
self.wfile.write(bytes("<p>Sorry Data Not found: %s</p>" % data, "utf-8")) | |
if __name__ == "__main__": | |
webServer = HTTPServer((hostName, serverPort), MyServer) | |
print("Server started http://%s:%s" % (hostName, serverPort)) | |
try: | |
webServer.serve_forever() | |
except KeyboardInterrupt: | |
pass | |
webServer.server_close() | |
print("Server stopped.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment