-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspock.py
More file actions
70 lines (55 loc) · 2.16 KB
/
spock.py
File metadata and controls
70 lines (55 loc) · 2.16 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
from flask import Flask, request, jsonify, render_template
import uuid
import datetime
import os
spock = Flask(__name__)
# In-memory storage (replace with a database in a production environment)
endpoints = {}
# Use environment variable to determine protocol
USE_HTTPS = os.environ.get('USE_HTTPS', '').lower() == 'true'
def get_public_url():
protocol = 'https' if USE_HTTPS else 'http'
return f"{protocol}://{request.host}"
@spock.route('/')
def index():
return render_template('index.html', endpoints=endpoints)
@spock.route('/create_endpoint', methods=['POST'])
def create_endpoint():
endpoint_id = str(uuid.uuid4())[:8] # Generate a random 8-character hash
endpoints[endpoint_id] = []
return jsonify({'endpoint_id': endpoint_id})
@spock.route('/endpoint/<endpoint_id>', methods=['GET', 'POST', 'PUT', 'DELETE', 'PATCH'])
def capture_request(endpoint_id):
if endpoint_id not in endpoints:
return "Endpoint not found", 404
request_data = {
'timestamp': datetime.datetime.now().isoformat(),
'method': request.method,
'headers': dict(request.headers),
'args': dict(request.args),
'form': dict(request.form),
'data': request.get_data(as_text=True)
}
if request.is_json:
request_data['json'] = request.get_json(silent=True)
else:
request_data['json'] = None
endpoints[endpoint_id].append(request_data)
return "Request captured", 200
@spock.route('/view/<endpoint_id>')
def view_endpoint(endpoint_id):
if endpoint_id not in endpoints:
return "Endpoint not found", 404
base_url = get_public_url()
endpoint_url = f"{base_url}/endpoint/{endpoint_id}"
return render_template('endpoint.html',
endpoint_id=endpoint_id,
requests=endpoints[endpoint_id],
endpoint_url=endpoint_url)
@spock.route('/delete_all_endpoints', methods=['DELETE'])
def delete_all_endpoints():
global endpoints
endpoints.clear()
return jsonify({'message': 'All endpoints have been deleted'}), 200
if __name__ == '__main__':
spock.run(debug=True, host='0.0.0.0', port=8080)