project
project
class FashionStoreHandler(SimpleHTTPRequestHandler):
# Sample data for products
products = [
{"id": 1, "name": "T-Shirt", "price": 499, "description": "Comfortable
cotton T-Shirt."},
{"id": 2, "name": "Jeans", "price": 1299, "description": "Stylish blue
denim jeans."},
{"id": 3, "name": "Sneakers", "price": 1999, "description": "Sporty and
durable sneakers."},
]
cart = []
def do_GET(self):
# Routing logic
if self.path == "/":
self.send_home_page()
elif self.path == "/products":
self.send_product_list()
elif self.path.startswith("/product"):
self.send_product_details()
elif self.path == "/cart":
self.send_cart()
elif self.path == "/checkout":
self.send_checkout()
else:
self.send_error(404, "Page Not Found")
def send_home_page(self):
"""Render the homepage."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
html = """
<html>
<head>
<title>Online Fashion Store</title>
</head>
<body>
<h1>Welcome to the Online Fashion Store</h1>
<nav>
<a href="/">Home</a> |
<a href="/products">Products</a> |
<a href="/cart">Cart</a>
</nav>
<p>Find the best fashion deals here!</p>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))
def send_product_list(self):
"""Render the product listing page."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
html = """
<html>
<head>
<title>Products</title>
</head>
<body>
<h1>Product Listings</h1>
<nav>
<a href="/">Home</a> |
<a href="/cart">Cart</a>
</nav>
<ul>
"""
for product in self.products:
html += f'<li><a href="/product?
id={product["id"]}">{product["name"]}</a> - ₹{product["price"]}</li>'
html += """
</ul>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))
def send_product_details(self):
"""Render the product details page."""
query = urlparse.urlparse(self.path).query
params = urlparse.parse_qs(query)
product_id = int(params.get("id", [0])[0])
if not product:
self.send_error(404, "Product Not Found")
return
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
html = f"""
<html>
<head>
<title>{product["name"]} - Product Details</title>
</head>
<body>
<h1>{product["name"]}</h1>
<p>{product["description"]}</p>
<p>Price: ₹{product["price"]}</p>
<a href="/cart?add={product["id"]}">Add to Cart</a>
<br><br>
<a href="/products">Back to Products</a>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))
def send_cart(self):
"""Render the cart page."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
if not self.cart:
html = """
<html>
<head>
<title>Cart</title>
</head>
<body>
<h1>Your Cart is Empty</h1>
<nav>
<a href="/">Home</a> |
<a href="/products">Products</a>
</nav>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))
return
html = """
<html>
<head>
<title>Cart</title>
</head>
<body>
<h1>Your Cart</h1>
<nav>
<a href="/">Home</a> |
<a href="/products">Products</a> |
<a href="/checkout">Checkout</a>
</nav>
<ul>
"""
total = 0
for item in self.cart:
total += item["price"]
html += f'<li>{item["name"]} - ₹{item["price"]}</li>'
html += f"""
</ul>
<p>Total: ₹{total}</p>
<a href="/checkout">Proceed to Checkout</a>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))
def send_checkout(self):
"""Render the checkout page."""
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
if not self.cart:
self.wfile.write(b"Your cart is empty!")
return
if __name__ == "__main__":
run()