-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.py
executable file
·66 lines (51 loc) · 1.51 KB
/
store.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#! /usr/bin/env python
# encoding: UTF-8
from __future__ import print_function
"""
Rather simple processing: just take the email address from the form, append it
to a text file and redirect the user to a confirmation / error page. It would
be nice to do something more elaborate, but I don't think the effort is worth
it for a coming-soon website.
"""
import cgi
import os
import time
EMAILS_FILE = 'emails.txt'
OK_URL = './exito.html'
ERROR_URL = './error.html'
def store_address():
""" Store email address, return False if the field was left empty. """
form = cgi.FieldStorage()
address = form.getvalue('email-address')
if not address:
return False
else:
address = address.strip()
if not address:
return False
with open(EMAILS_FILE, 'at') as fd:
utctime = time.asctime(time.gmtime()) + ' UTC'
fd.write("{0} | {1}\n".format(utctime, address))
# Make sure nobody can read the file
os.chmod(EMAILS_FILE, 0600)
return True
def redirect(url):
""" Redirect the user to the specified URL. """
html = """Content-type: text/html
<html>
<head>
<meta http-equiv="refresh" content="0;url={0}" />
<title>Redirigiendo...</title>
</head>
<body>
Redirigiendo...
<a href="{0}">Haz click aqui si no eres redirigido automaticamente</a>
</body>
</html>
"""
print(html.format(url))
if __name__ == "__main__":
if store_address():
redirect(OK_URL)
else:
redirect(ERROR_URL)