Created
June 18, 2025 18:47
-
-
Save greyhoundforty/ee79cdc7e4327ee08a86b78bc4b65b03 to your computer and use it in GitHub Desktop.
WiFi QR Code Generator with Click prompts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
""" | |
Generates QR codes for WiFi network connection | |
""" | |
import click | |
import qrcode | |
from pathlib import Path | |
def generate_wifi_qr(ssid, password, security="WPA", hidden=False, output_file="wifi_qr.png"): | |
"""Generate a WiFi QR code and save to file""" | |
# WiFi QR code format: WIFI:T:security;S:ssid;P:password;H:hidden;; | |
wifi_string = f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;" | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=10, | |
border=4, | |
) | |
qr.add_data(wifi_string) | |
qr.make(fit=True) | |
img = qr.make_image(fill_color="black", back_color="white") | |
img.save(output_file) | |
return img | |
@click.command() | |
@click.option('--ssid', '-s', | |
prompt='WiFi Network Name (SSID)', | |
help='The WiFi network name') | |
@click.option('--password', '-p', | |
prompt='WiFi Password', | |
hide_input=True, | |
confirmation_prompt=True, | |
help='The WiFi network password') | |
@click.option('--security', '-sec', | |
type=click.Choice(['WPA', 'WEP', 'nopass'], case_sensitive=False), | |
default='WPA', | |
prompt='Security Type', | |
help='WiFi security type (WPA, WEP, or nopass)') | |
@click.option('--hidden', '-h', | |
is_flag=True, | |
prompt='Is this a hidden network?', | |
help='Whether the network is hidden') | |
@click.option('--output', '-o', | |
default='wifi_qr.png', | |
prompt='Output filename', | |
help='Output filename for the QR code image') | |
@click.option('--display-info', '-d', | |
is_flag=True, | |
help='Display the WiFi string that will be encoded') | |
def main(ssid, password, security, hidden, output, display_info): | |
"""Generate a WiFi QR code for easy network sharing""" | |
# Show a nice header | |
click.echo() | |
click.echo("π " + click.style("WiFi QR Code Generator", fg='blue', bold=True)) | |
click.echo("=" * 40) | |
# Display the configuration | |
click.echo(f"πΆ Network Name: {click.style(ssid, fg='green', bold=True)}") | |
click.echo(f"π Security: {click.style(security.upper(), fg='yellow')}") | |
click.echo(f"ποΈ Hidden Network: {click.style('Yes' if hidden else 'No', fg='cyan')}") | |
click.echo(f"πΎ Output File: {click.style(output, fg='magenta')}") | |
if display_info: | |
wifi_string = f"WIFI:T:{security};S:{ssid};P:{password};H:{'true' if hidden else 'false'};;" | |
click.echo(f"π WiFi String: {click.style(wifi_string, fg='white', dim=True)}") | |
click.echo() | |
# Confirm before generating | |
if click.confirm('Generate QR code?', default=True): | |
try: | |
with click.progressbar(length=100, label='Generating QR code') as bar: | |
# Simulate progress for visual feedback | |
import time | |
for i in range(100): | |
time.sleep(0.01) | |
bar.update(1) | |
# Generate the QR code | |
generate_wifi_qr(ssid, password, security, hidden, output) | |
# Success message | |
click.echo() | |
click.echo("β " + click.style("QR code generated successfully!", fg='green', bold=True)) | |
click.echo(f"π Saved as: {click.style(str(Path(output).absolute()), fg='blue')}") | |
click.echo() | |
click.echo("π± " + click.style("Instructions:", fg='yellow', bold=True)) | |
click.echo(" 1. Open your phone's camera app") | |
click.echo(" 2. Point it at the QR code") | |
click.echo(" 3. Tap the notification to connect to WiFi") | |
except Exception as e: | |
click.echo() | |
click.echo("β " + click.style("Error generating QR code:", fg='red', bold=True)) | |
click.echo(f" {str(e)}") | |
raise click.Abort() | |
else: | |
click.echo("β QR code generation cancelled") | |
@click.command() | |
@click.argument('url', required=True) | |
@click.option('--output', '-o', | |
default='url_qr.png', | |
help='Output filename for the QR code image') | |
def url_qr(url, output): | |
"""Generate a QR code for a URL""" | |
click.echo() | |
click.echo("π " + click.style("URL QR Code Generator", fg='blue', bold=True)) | |
click.echo("=" * 40) | |
click.echo(f"π URL: {click.style(url, fg='green', bold=True)}") | |
click.echo(f"πΎ Output File: {click.style(output, fg='magenta')}") | |
click.echo() | |
if click.confirm('Generate QR code?', default=True): | |
try: | |
qr = qrcode.QRCode( | |
version=1, | |
error_correction=qrcode.constants.ERROR_CORRECT_L, | |
box_size=10, | |
border=4, | |
) | |
qr.add_data(url) | |
qr.make(fit=True) | |
img = qr.make_image(fill_color="black", back_color="white") | |
img.save(output) | |
click.echo("β " + click.style("URL QR code generated successfully!", fg='green', bold=True)) | |
click.echo(f"π Saved as: {click.style(str(Path(output).absolute()), fg='blue')}") | |
except Exception as e: | |
click.echo("β " + click.style("Error generating QR code:", fg='red', bold=True)) | |
click.echo(f" {str(e)}") | |
raise click.Abort() | |
@click.group() | |
@click.version_option(version='1.0.0', prog_name='QR Generator') | |
def cli(): | |
"""QR Code Generator for WiFi networks and URLs""" | |
pass | |
# Add commands to the group | |
cli.add_command(main, name='wifi') | |
cli.add_command(url_qr, name='url') | |
if __name__ == '__main__': | |
cli() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment