-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
81 lines (63 loc) · 2.08 KB
/
app.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
69
70
71
72
73
74
75
76
77
78
79
80
81
from flask import Flask, request, render_template
from flask_socketio import SocketIO, emit, join_room
from flask_cors import CORS, cross_origin
import requests
import json
app = Flask(__name__)
app.secret_key = 'random secret key!'
cors = CORS(app)
socketio = SocketIO(app, cors_allowed_origins="*")
@app.route("/")
@app.route("/index.html")
def index():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
return render_template('index.html', forwarded=ip)
@app.route("/ip/location")
@cross_origin()
def ip_location():
ip = request.headers.get('X-Forwarded-For', request.remote_addr)
r = requests.get(url='http://ip-api.com/json/' + ip +
'?fields=country,regionName,city,isp', timeout=60)
# Add ip to the response
r = r.json()
r['ip'] = ip
response = app.response_class(
response=json.dumps(r).encode('utf-8'),
status=200,
mimetype='application/json'
)
return response
@app.route("/ip/location/<ip>")
@cross_origin()
def ip_locationWithIp(ip):
r = requests.get(url='http://ip-api.com/json/' + ip +
'?fields=country,regionName,city,isp', timeout=60)
# Add ip to the response
r = r.json()
r['ip'] = ip
response = app.response_class(
response=json.dumps(r).encode('utf-8'),
status=200,
mimetype='application/json'
)
return response
@socketio.on('join')
def join(message):
username = message['username']
room = message['room']
join_room(room)
print('RoomEvent: {} has joined the room {}\n'.format(username, room))
emit('ready', {username: username}, to=room, skip_sid=request.sid)
@socketio.on('data')
def transfer_data(message):
username = message['username']
room = message['room']
data = message['data']
print('DataEvent: {} has sent the data:\n {}\n'.format(username, data))
emit('data', data, to=room, skip_sid=request.sid)
@socketio.on_error_default
def default_error_handler(e):
print("Error: {}".format(e))
socketio.stop()
if __name__ == '__main__':
socketio.run(app, host="0.0.0.0")