File size: 1,442 Bytes
a460fdc 059bbb9 0b67e3c a460fdc 059bbb9 a460fdc 33f8f7c a460fdc 0b67e3c a460fdc 0b67e3c a460fdc 6048a58 a460fdc 0b67e3c a460fdc 07d1566 a460fdc 059bbb9 0b67e3c a460fdc |
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 |
from flask import Flask, request, jsonify
import random
import string
import threading
app = Flask(__name__)
connections = {}
lock = threading.Lock()
def generate_id():
return ''.join(random.choices(string.ascii_letters + string.digits, k=8))
@app.route('/create_offer', methods=['POST'])
def create_offer():
offer = request.json
conn_id = generate_id()
with lock:
connections[conn_id] = {
'offer': offer,
'answer': None
}
return jsonify({'connection_id': conn_id})
@app.route('/get_offer/<conn_id>', methods=['GET'])
def get_offer(conn_id):
with lock:
conn = connections.get(conn_id)
if not conn:
return jsonify({'error': 'Invalid ID'}), 404
return jsonify(conn['offer'])
@app.route('/send_answer/<conn_id>', methods=['POST'])
def send_answer(conn_id):
with lock:
if conn_id not in connections:
return jsonify({'error': 'Invalid ID'}), 404
connections[conn_id]['answer'] = request.json
return jsonify({'status': 'success'})
@app.route('/get_answer/<conn_id>', methods=['GET'])
def get_answer(conn_id):
with lock:
conn = connections.get(conn_id)
if not conn or not conn['answer']:
return jsonify({'error': 'Not ready'}), 404
return jsonify(conn['answer'])
if __name__ == '__main__':
app.run(host='0.0.0.0', port=5000, threaded=True) |