-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPRIP.py
More file actions
192 lines (164 loc) Β· 5.64 KB
/
Copy pathPRIP.py
File metadata and controls
192 lines (164 loc) Β· 5.64 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
from flask import Flask, request, render_template, jsonify, redirect, url_for
import json
import os
from datetime import datetime
from Extractor import process_request
app = Flask(__name__)
# Configuration
LOG_FILE = 'prip_test_logs.json'
def log_test_result(test_data):
"""Log test results to JSON file"""
try:
if os.path.exists(LOG_FILE):
with open(LOG_FILE, 'r') as f:
logs = json.load(f)
else:
logs = []
logs.append(test_data)
with open(LOG_FILE, 'w') as f:
json.dump(logs, f, indent=2)
except Exception as e:
print(f"Error logging test result: {e}")
@app.route('/')
def index():
"""Main test page with HTML template"""
return render_template('template.html')
@app.route('/test', methods=['POST'])
def test_extractor():
"""Test the extractor functionality"""
try:
# Process the request using the extractor
extracted_data = process_request(request)
print(f"Extracted Data: {extracted_data}")
# Create test log entry
test_log = {
"timestamp": datetime.now().isoformat(),
"test_type": "extractor_test",
"request_info": {
"method": request.method,
"user_agent": request.headers.get('User-Agent', ''),
"remote_addr": request.remote_addr,
"cookies_count": len(request.cookies)
},
"extracted_data": extracted_data,
"status": "success"
}
log_test_result(test_log)
return jsonify({
"status": "success",
"message": "Data extracted successfully",
"data": extracted_data
})
except Exception as e:
error_log = {
"timestamp": datetime.now().isoformat(),
"test_type": "extractor_test",
"error": str(e),
"status": "error"
}
log_test_result(error_log)
return jsonify({
"status": "error",
"message": str(e)
}), 400
@app.route('/reconnect_user', methods=['GET', 'POST'])
def reconnect_user():
"""Handle reconnect user requests from the JS library"""
try:
# Log the reconnect attempt
reconnect_log = {
"timestamp": datetime.now().isoformat(),
"test_type": "reconnect_user",
"request_info": {
"method": request.method,
"user_agent": request.headers.get('User-Agent', ''),
"remote_addr": request.remote_addr,
"headers": dict(request.headers),
"cookies": dict(request.cookies)
},
"status": "success"
}
log_test_result(reconnect_log)
# Return response for the JS library
response_data = {
"status": "connected",
"timestamp": datetime.now().isoformat(),
"session_id": f"session_{datetime.now().strftime('%Y%m%d_%H%M%S')}",
"user_id": request.cookies.get('PRIP_USERID', 'unknown'),
"device_id": request.cookies.get('PRIP_USERDEVICE', 'unknown')
}
return jsonify(response_data)
except Exception as e:
error_log = {
"timestamp": datetime.now().isoformat(),
"test_type": "reconnect_user",
"error": str(e),
"status": "error"
}
log_test_result(error_log)
return jsonify({
"status": "error",
"message": str(e)
}), 500
@app.route('/logs')
def view_logs():
"""View test logs"""
try:
if os.path.exists(LOG_FILE):
with open(LOG_FILE, 'r') as f:
logs = json.load(f)
else:
logs = []
return jsonify({
"status": "success",
"logs": logs,
"count": len(logs)
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 500
@app.route('/clear-logs', methods=['POST'])
def clear_logs():
"""Clear test logs"""
try:
if os.path.exists(LOG_FILE):
os.remove(LOG_FILE)
return jsonify({
"status": "success",
"message": "Logs cleared successfully"
})
except Exception as e:
return jsonify({
"status": "error",
"message": str(e)
}), 500
@app.route('/status')
def status():
"""Server status endpoint"""
return jsonify({
"status": "running",
"timestamp": datetime.now().isoformat(),
"version": "1.0.0",
"endpoints": {
"/": "Main test interface",
"/test": "POST - Test extractor",
"/reconnect_user": "GET/POST - User reconnection",
"/logs": "GET - View logs",
"/clear-logs": "POST - Clear logs",
"/status": "GET - Server status"
}
})
if __name__ == '__main__':
# Create templates directory if it doesn't exist
if not os.path.exists('templates'):
os.makedirs('templates')
print("π PRIP Flask Test Server Starting...")
print("π Available endpoints:")
print(" β’ http://localhost:5000/ - Main test interface")
print(" β’ http://localhost:5000/test - Test extractor (POST)")
print(" β’ http://localhost:5000/reconnect_user - User reconnection")
print(" β’ http://localhost:5000/logs - View test logs")
print(" β’ http://localhost:5000/status - Server status")
app.run(debug=True, host='0.0.0.0', port=5000)