0% found this document useful (0 votes)
13 views

project

Uploaded by

dk034081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

project

Uploaded by

dk034081
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

from http.

server import SimpleHTTPRequestHandler, HTTPServer


import urllib.parse as urlparse
import json

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])

product = next((p for p in self.products if p["id"] == product_id), None)

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

self.cart.clear() # Clear the cart after checkout


html = """
<html>
<head>
<title>Checkout</title>
</head>
<body>
<h1>Thank you for your purchase!</h1>
<nav>
<a href="/">Home</a> |
<a href="/products">Products</a>
</nav>
<p>Your order has been placed successfully.</p>
</body>
</html>
"""
self.wfile.write(html.encode("utf-8"))

def do_GET_cart_management(self, query):


"""Handle cart-related queries (add items to the cart)."""
params = urlparse.parse_qs(query)
if "add" in params:
product_id = int(params["add"][0])
product = next((p for p in self.products if p["id"] == product_id),
None)
if product:
self.cart.append(product)
self.send_response(302)
self.send_header("Location", "/cart")
self.end_headers()
else:
self.send_error(404, "Product Not Found")

# Main server setup


def run(server_class=HTTPServer, handler_class=FashionStoreHandler, port=8080):
server_address = ("", port)
httpd = server_class(server_address, handler_class)
print(f"Starting server on port {port}...")
httpd.serve_forever()

if __name__ == "__main__":
run()

You might also like