A simple web browser implementation written in Python using Tkinter for the GUI and socket programming for HTTP requests.
PyBrowser is a minimal web browser that demonstrates the core concepts of web browsing including HTTP requests, HTML parsing, and text rendering. It provides a basic GUI interface for viewing web content with scrolling capabilities.
- HTTP/HTTPS Support: Makes HTTP requests to web servers with support for redirects
- HTML Parsing: Parses HTML content and builds a document tree structure
- Text Rendering: Renders HTML content as formatted text with basic styling
- GUI Interface: Tkinter-based graphical user interface with scrolling
- File Protocol Support: Handles
file://URLs for local file browsing - Data Protocol Support: Basic support for
data://URLs - Scrollable Content: Mouse wheel and keyboard scrolling support
src/
├── main.py # Entry point and main application logic
├── config.py # Configuration constants
├── gui/
│ └── browser.py # Tkinter GUI implementation
├── network/
│ ├── httpClient.py # HTTP client for making requests
│ ├── httpResponse.py # HTTP response parsing and handling
│ └── url.py # URL parsing and connection management
└── render/
├── htmlParser.py # HTML parsing and DOM tree building
├── layout.py # Text layout and formatting
├── render.py # Canvas rendering engine
├── tag.py # HTML tag representation
└── text.py # Text node representation
enums/
├── httpStatus.py # HTTP status code definitions
└── ports.py # Standard port definitions
httpClient.py: Handles HTTP requests with redirect support and file protocol handlinghttpResponse.py: Parses HTTP responses, handles gzip compression, and tokenizes HTML contenturl.py: URL parsing, connection establishment, and protocol handling (HTTP/HTTPS/file/data)
htmlParser.py: Parses HTML content and builds a document tree structurelayout.py: Handles text layout, font management, and basic HTML tag formattingrender.py: Manages canvas rendering, scrolling, and viewport managementtag.py&text.py: Represent HTML elements and text nodes in the DOM tree
browser.py: Tkinter-based GUI with canvas for content display and scrollbar for navigation
python src/main.py <URL># Browse a website
python src/main.py https://example.com
# View a local file
python src/main.py file:///path/to/file.html
# View data URL
python src/main.py data:text/html,<h1>Hello World</h1>from src.gui.browser import Browser
from src.network.httpClient import HttpClient
from src.render.htmlParser import HtmlParser
# Create HTTP client and fetch content
client = HttpClient(max_redirects=5)
response = client.get("https://example.com")
# Parse HTML content
parser = HtmlParser(response.html_content)
dom_tree = parser.parse()
# Display in browser GUI
browser = Browser()
browser.load(response.tokenize())The browser behavior can be customized through src/config.py:
TITLE: Window titleHORIZONTAL_STEP: Left and right marginsVERTICAL_STEP: Line height and vertical marginsSCROLL_STEP: Mouse wheel and keyboard scroll distance
The browser supports basic HTML formatting:
- Text formatting: Bold (
<b>), italic (<i>), superscript (<sup>), subscript (<sub>) - Headings: H1-H6 with different font sizes
- Text size:
<big>and<small>tags - Line breaks:
<br>and paragraph breaks<p>
- Uses raw socket programming for HTTP requests
- Supports HTTP/1.0 protocol
- Handles gzip compression
- Implements redirect following (max 5 redirects)
- SSL/TLS support for HTTPS
- Simple tokenizer that separates HTML tags from text content
- Builds a tree structure representing the DOM
- Handles nested tags and closing tags
- Uses Tkinter Canvas for text rendering
- Implements word wrapping and line breaking
- Font caching for performance
- Scrollable viewport with mouse wheel support
- Python 3.7+
- Tkinter (usually included with Python)
- Standard library modules:
socket,ssl,gzip,io,os
- No CSS support
- No JavaScript execution
- No image rendering
- Basic HTML tag support only
- No form handling
- No cookies or session management
- Limited error handling