Easy Guide to Building a Fake Website Cloner Tool


In this tutorial, we'll walk through the steps to create a simple website cloner tool using Python and Streamlit. This tool will allow you to clone any website to your localhost for testing and educational purposes.
Prerequisites
Basic knowledge of Python
Streamlit installed (
pip install streamlit
)Requests library installed (
pip install requests
)
Step-by-Step Guide
1. Set Up Streamlit
First, import the necessary libraries and configure the Streamlit app:
import streamlit as st
import threading
import random
import socketserver
import http.server
import requests
import time
from urllib.parse import urlparse
st.set_page_config(page_title="Clone Any Website to Localhost", page_icon="๐")
2. Style the App
Add some custom styling to enhance the appearance of the app:
st.markdown(
"""
<style>
#MainMenu, header, footer {visibility: hidden;}
.stButton>button {
color: #FFFFFF !important;
}
.stButton>button:hover {
background-color: #262730 !important;
color: #FFFFFF !important;
}
.stButton>button:disabled {
background: linear-gradient(to right, #e52d27, #b31217) !important;
color: #FFFFFF !important;
}
div[data-testid="stProgress"] > div > div > div > div {
background-color: #e52d27 !important;
}
</style>
""",
unsafe_allow_html=True,
)
3. Create the Main Interface
Set up the main interface where users can input the URL of the website they want to clone:
st.title("๐ Clone Any Website to Localhost")
url = st.text_input("Enter the website URL to clone", "")
4. Implement the Proxy Server
Create a proxy server to handle requests and responses between the cloned site and the user:
class Proxy(http.server.SimpleHTTPRequestHandler):
target_url = ""
def do_GET(self):
try:
full_url = Proxy.target_url + self.path
resp = requests.get(full_url)
self.send_response(resp.status_code)
for k, v in resp.headers.items():
if k.lower() not in ("content-encoding", "content-length", "transfer-encoding", "connection"):
self.send_header(k, v)
self.end_headers()
self.wfile.write(resp.content)
except Exception as e:
self.send_response(500)
self.end_headers()
self.wfile.write(f"Error: {e}".encode())
5. Run the Proxy Server
Define a function to run the proxy server on a random port:
def run_proxy_server(port, target):
Proxy.target_url = target
with socketserver.TCPServer(("", port), Proxy) as httpd:
httpd.serve_forever()
6. Start Cloning Process
Add functionality to start the cloning process when the user clicks the button:
if st.button("Start Cloning"):
parsed = urlparse(url)
if not parsed.scheme.startswith("http"):
st.error("Please enter a valid URL with http or https scheme.")
else:
port = random.randint(9000, 9999)
progress = st.progress(0)
status = st.empty()
# Start proxy server
threading.Thread(target=run_proxy_server, args=(port, url), daemon=True).start()
# Simulate steps with progress
for text, pct in [
("Fetching website content...", 25),
("Building structure...", 50),
("Making pixel perfect...", 75),
("Rendering on new port...", 100),
]:
status.text(text)
progress.progress(pct)
time.sleep(1)
# Show card with preview link
st.markdown("---")
st.subheader("โ
Website Cloned Successfully")
st.markdown(
f'<div style="border:1px solid #444;padding:1rem;border-radius:12px;background-color:#111;">'
f'<h4 style="color:white;">Preview your cloned site:</h4>'
f'<a href="http://localhost:{port}" target="_blank" style="color:#1E90FF;">http://localhost:{port}</a>'
f'</div>',
unsafe_allow_html=True,
)
Conclusion
This simple tool demonstrates how you can clone a website to your local environment using Python and Streamlit. Remember to use this tool responsibly and only for educational purposes. Happy coding!
Subscribe to my newsletter
Read articles from Manish Tiwari directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
