A simple command-line URL shortener built with Python and MySQL. Shorten long URLs, track click statistics, and manage your links efficiently.
- ✂️ Shorten long URLs into compact codes
- 📊 Track click statistics for each URL
- 🔍 Expand short codes back to original URLs
- 📝 List all shortened URLs
- 💾 Persistent storage with MySQL database
- Python 3.14.3 - Core application logic
- MySQL/MariaDB - Database for storing URLs
- mysql-connector-python - MySQL database connector
Before running this project, make sure you have:
- Python 3 or higher installed
- MySQL or MariaDB server running
- pip (Python package manager)
- Clone the repository
git clone https://github.com/SemTiOne/url-shortener.git
cd url-shortener- Install dependencies
pip install -r requirements.txt- Configure database
Open url_shortener.py and update the database credentials:
self.connection = mysql.connector.connect(
host='localhost',
user='root', # Change to your MySQL username
password='', # Change to your MySQL password
database='url_shortener'
)- Run the application
python url_shortener.pyThe application will automatically create the database and table on first run.
1. Shorten a URL - Create a short code for a long URL
2. Expand a short code - Get the original URL from a short code
3. View statistics - See clicks and details for a URL
4. List all URLs - Display all shortened URLs
5. Exit - Close the application
Shortening a URL:
Enter URL to shorten: https://www.example.com/very/long/url/path
✓ URL shortened successfully!
Original: https://www.example.com/very/long/url/path
Shortened: short.ly/aBc123
Short Code: aBc123
Expanding a short code:
Enter short code: aBc123
✓ Found URL!
Short Code: aBc123
Original URL: https://www.example.com/very/long/url/path
CREATE TABLE urls (
id INT AUTO_INCREMENT PRIMARY KEY,
original_url VARCHAR(2048) NOT NULL,
short_code VARCHAR(10) UNIQUE NOT NULL,
clicks INT DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);url-shortener/
├── 🐍 url_shortener.py # Main application file
├── 🗄️ setup.sql # Database setup script
├── 📦 requirements.txt # Python dependencies
├── 📄 LICENSE # MIT license
├── 📄 .gitignore # Git exclusions
└── 📖 README.md # This file
- Generates random 6-character alphanumeric codes
- Checks for duplicate URLs (returns existing short code)
- Ensures unique short codes
- Increments counter each time a short code is accessed
- Persistent storage in database
- View creation date
- Track total clicks
- Display original and shortened URLs
- View all shortened URLs at once
- Display click counts
- Sort by creation date
Here are some ideas to expand this project:
- Custom short codes (let users choose their own)
- Expiration dates for URLs
- QR code generation for shortened URLs
- Web interface using Flask/FastAPI
- URL validation before shortening
- Analytics dashboard with charts
- User authentication and accounts
- API endpoints for programmatic access
"Error connecting to database"
- Make sure MySQL/MariaDB is running
- Check your database credentials in
url_shortener.py - Verify MySQL is accessible on localhost:3306
"Module not found: mysql.connector"
- Install the required package:
pip install mysql-connector-python
"Access denied for user"
- Update the username and password in the code
- Grant necessary permissions in MySQL
This project is open source and available under the MIT License.
Contributions are welcome! Feel free to submit issues or pull requests.
Dane Parin - @SemTiOne
Note: This is a learning project designed to demonstrate Python and MySQL integration. For production use, consider adding security features like input validation, SQL injection prevention, and HTTPS for the shortened URLs.