forked from raphaelmenges/StupidSurvey.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
processor.py
68 lines (57 loc) · 1.71 KB
/
processor.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
67
68
#!/usr/bin/env python3
import requests
import json
from requests.auth import HTTPBasicAuth
from cgi import FieldStorage
from pathlib import Path
from datetime import datetime
import cgi
import os
# Setup
passphrase_key = 'submission.passphrase' # page.name
passphrase_value = 'foo'
# Variables
data = {}
print("Content-Type: text/html, charset=\"utf-8\"")
print() # very important whitespace
# Get data from form
form = FieldStorage(encoding='utf-8', keep_blank_values=True)
if len(form.keys()) <= 0: # check for non-empty form
exit()
# Check passphrase
if not form.getfirst(passphrase_key, '') == passphrase_value:
print("Incorrect passphrase, try <a href='/?page=-1'>again</a>! Your data is still entered.")
exit()
# Store data from form in dict
data['form'] = {}
form_data = data['form']
for key in form.keys():
if not key == passphrase_key:
form_data[key] = form.getfirst(key, '').replace('\r\n', '\n')
# Add IP address of client
data['meta'] = {}
meta_data = data['meta']
meta_data['ip'] = cgi.escape(os.environ["REMOTE_ADDR"])
# Prepare PUT
headers = {'Content-type': 'application/json; charset=utf-8'}
url = 'foo' + datetime.now().strftime('%Y-%m-%d-%H-%M-%S-%f') + '.json' # extension
token = 'foo'
# Perform PUT
request = requests.put(
url,
data=json.dumps(data, ensure_ascii=False, indent=2, sort_keys=True).encode('utf-8'),
headers=headers,
auth=HTTPBasicAuth(token, '')) # token as username
# print(request.text) # print message from server
# print("Form sent!")
print("""
<html>
<head>
<meta http-equiv="refresh" content="0;url=../thanks.html" />
<title>You are going to be redirected</title>
</head>
<body>
Redirecting... <a href="../thanks.html">Click here if you are not redirected</a>
</body>
</html>
""")