So i needed some quick way to verify email newsletter subscriptiosn .. (new european dataprotection laws ..)
I decided I don't want php and no DB, so - since it is installed anyway - I used mod_wsgi again.
Here's the parts of the apache config (from within the VirtualHost:
#newsletter verification wsgi app
WSGIDaemonProcess myapp home=/path_to/newsletter_verify/myenv processes=1 threads= 5 display-name=[wsgi-myapp]httpd python-path=/path_to/newsletter_verify:/path_to/newsletter_verify/myenv/lib64/python3.6/site-packages
WSGIProcessGroup myapp
WSGIApplicationGroup %{GLOBAL}
WSGIScriptAlias /newsletter /path_to/newsletter_verify/apache.wsgi
Simple config, just added parts for virtualenv stuff (not really needed in the end but needed it while I was testing stuff.
here's the apache.wsgi:
import newsletter
def application(environ, start_response):
return newsletter.handle_request(environ, start_response)
then the newsletter.py file:
import sys
import csv
import datetime
from urllib.parse import urlparse, parse_qs
sys.stdout = sys.stderr
DEBUG = False
def handle_request(environ, start_response):
# standard values
status = '200 OK'
output = b'<html><head><title>Hello World</title></head><body><h1>hello world!</h1></body></html>'
# use urlib instead of seperate environ stuff
url = urlparse(environ['REQUEST_URI'])
qs = parse_qs(url.query)
if url.path == '/newsletter' and 'confirm' in qs:
myuuid = qs['confirm'][0]
contact = None
with open('/path_to/newsletter_verify/uuids.csv', 'r') as uuid_file:
uuids = [line.rstrip() for line in uuid_file]
# write results to csv files
if myuuid in uuids:
contact = myuuid
with open('/path_to/newsletter_verify/uuids_confirmed.csv', 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow([myuuid,
str(datetime.datetime.now()),
environ['REMOTE_ADDR'] + ':' + environ['REMOTE_PORT'],
environ['HTTP_USER_AGENT']])
else:
# uuid not found .. log the error though
with open('/path_to/newsletter_verify/uuids_error.csv', 'a') as csvfile:
csvwriter = csv.writer(csvfile)
csvwriter.writerow([myuuid,
str(datetime.datetime.now()),
environ['REMOTE_ADDR'] + ':' + environ['REMOTE_PORT'],
environ['HTTP_USER_AGENT']])
if contact:
output = bytes('''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Infomail...</title>
</head>
<body>
<h1>thanks for letting us send you emails.</h1>
</body>
</html>
''', 'utf-8')
else:
output = bytes('''<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Infomail...</title>
</head>
<body>
<h1>We did not find your address .. please try again/h1>
</body>
</html>
''', 'utf-8')
else:
status = '404 NOT FOUND'
output = b'<html><head><title>404 Page not found</title></head><body><h1>404 Page not found</h1><p>please check the link</p></body></html>'
response_headers = [('Content-type', 'text/html'),
('Content-Length', str(len(output)))]
start_response(status, response_headers)
return [output]
The logic is simple: check request, if correct, extract uuid, compare it to a list from a csv and then write to output files who did click the link +ip, user agent. The rest is just plain static html, manual handling of .. well everything.
Share on Twitter Share on Facebook
Comments
There are currently no comments
New Comment