Skip to content

Instantly share code, notes, and snippets.

@wheybags
Last active December 26, 2023 21:27
Show Gist options
  • Save wheybags/262ce8860c33b7f9825ed85747770937 to your computer and use it in GitHub Desktop.
Save wheybags/262ce8860c33b7f9825ed85747770937 to your computer and use it in GitHub Desktop.
email_blog_subscribe.py
#!/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