-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
54 lines (49 loc) · 1.22 KB
/
utils.py
File metadata and controls
54 lines (49 loc) · 1.22 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
import json
import socket
# Standard Configuration
PORT = 65432
# Using UTF-8 to even support emoji and special characters
ENCODING = 'utf-8'
def get_local_ip():
""" Find Local Machine IP Address """
try:
# Get effective IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("8.8.8.8", 80))
ip = s.getsockname()[0]
s.close()
return ip
except:
return "127.0.0.1"
def create_json_msg(sender_name, sender_ip, target, text):
"""
Create dictionary folllowing the standard:
{
"from": { "name": "...", "ip": "..." },
"to": "...",
"msg": "..."
}
"""
return json.dumps({
"from": {
"name": sender_name,
"ip": sender_ip
},
"to": target,
"msg": text
})
def decode_json_msg(bytes_data):
"""Transform the received bytes into a Python dictionary"""
try:
return json.loads(bytes_data.decode(ENCODING))
except json.JSONDecodeError:
return None
def get_standard_json():
"""Return the standard JSON as a string(for debugging)"""
return """
{
"from": { "name": "...", "ip": "..." },
"to": "...",
"msg": "..."
}
"""