-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
backend.py
63 lines (51 loc) · 1.96 KB
/
backend.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
from flask import Flask, request, Response, jsonify
from flask_cors import CORS
import sqlite3
import datetime
import socket
app = Flask(__name__)
CORS(app) # This will enable CORS for all routes
@app.route('/submit', methods=['POST'])
def submit_text():
data = request.get_json()
print(f"Received data: {data}") # This line prints the received data
text = data['text']
ip_address = request.remote_addr
# Get the current timestamp
timestamp = datetime.datetime.now()
conn = sqlite3.connect('database.db')
c = conn.cursor()
# Include the timestamp when inserting data into the database
c.execute("INSERT INTO submissions (ip_address, text, timestamp) VALUES (?, ?, ?)", (ip_address, text, timestamp))
conn.commit()
conn.close()
return 'Success', 200
@app.route('/getData', methods=['GET'])
def get_data():
# Set the server's IP address
server_ip = '192.168.178.44'
# Set the allowed client's IP addresses
allowed_client_ips = ['192.168.178.77', '127.0.0.1']
# Get the client's IP address
client_ip = request.remote_addr
# If the client's IP address is not in the list of allowed client's IP addresses, return an error
if client_ip not in allowed_client_ips and client_ip != server_ip:
print(f"Access denied for IP address: {client_ip}") # Print a message to the console
return 'Access denied', 403
conn = sqlite3.connect('database.db')
c = conn.cursor()
# Fetch the timestamp from the database
c.execute("SELECT ip_address, text, timestamp FROM submissions")
data = c.fetchall()
conn.close()
return jsonify(data), 200
@app.route('/clearDb', methods=['POST'])
def clear_db():
conn = sqlite3.connect('database.db')
c = conn.cursor()
c.execute("DELETE FROM submissions")
conn.commit()
conn.close()
return 'Success', 200
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000)