Last active
December 26, 2023 21:27
-
-
Save wheybags/262ce8860c33b7f9825ed85747770937 to your computer and use it in GitHub Desktop.
email_blog_subscribe.py
This file contains 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 | |
# license MIT | |
import sys | |
import traceback | |
print("Content-type: text/html\r\n\r\n") | |
def run(): | |
import os | |
import urllib.parse | |
import sqlite3 | |
print('<!DOCTYPE html><html><head><meta charset="UTF-8"></head><body>') | |
query_params = urllib.parse.parse_qs(os.environ["QUERY_STRING"]) | |
filename = os.path.basename(__file__) | |
action = "show_form" | |
if "action" in query_params: | |
action = query_params["action"][0] | |
if action == "submit": | |
email = query_params["email"][0] | |
conn = sqlite3.connect('/home/wheybags/blog_subscribe_sql/db.sqlite') | |
conn.execute('INSERT INTO subscriptions (email) VALUES (?)', (email,)) | |
conn.commit() | |
conn.close() | |
print('<meta http-equiv="refresh" content="0; url=./' + filename + '?action=success" />') | |
elif action == "show_form": | |
print('Please enter your email address to subscribe<br>') | |
print('<form action="./' + filename + '" method="get">') | |
print(' <input type="hidden" name="action" value="submit">') | |
print(' <label for="email">Email:</label>') | |
print(' <input type="text" name="email"><br><br>') | |
print(' <button type="submit">Submit</button>') | |
print('</form>') | |
elif action == "success": | |
print("Successfully subscribed. Thanks!") | |
print("</body></html>") | |
debug = False | |
if debug: | |
try: | |
run() | |
except: | |
traceback.print_exc(file=sys.stdout) | |
else: | |
run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment