forked from xyyangkun/python-dvr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AlarmServer.py
55 lines (46 loc) · 1.36 KB
/
AlarmServer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os, sys, struct, json
from time import sleep
from socket import *
from datetime import *
if len(sys.argv) > 1:
port = sys.argv[1]
else:
print("Usage: %s [Port]" % os.path.basename(sys.argv[0]))
port = input("Port(default 15002): ")
if port == "":
port = "15002"
server = socket(AF_INET, SOCK_STREAM)
server.bind(("0.0.0.0", int(port)))
# server.settimeout(0.5)
server.listen(1)
log = "info.txt"
def tolog(s):
logfile = open(datetime.now().strftime("%Y_%m_%d_") + log, "a+")
logfile.write(s)
logfile.close()
def GetIP(s):
return inet_ntoa(struct.pack("<I", int(s, 16)))
while True:
try:
conn, addr = server.accept()
head, version, session, sequence_number, msgid, len_data = struct.unpack(
"BB2xII2xHI", conn.recv(20)
)
sleep(0.1) # Just for recive whole packet
data = conn.recv(len_data)
conn.close()
reply = json.loads(data, encoding="utf8")
print(datetime.now().strftime("[%Y-%m-%d %H:%M:%S]>>>"))
print(head, version, session, sequence_number, msgid, len_data)
print(json.dumps(reply, indent=4, sort_keys=True))
print("<<<")
tolog(repr(data) + "\r\n")
except (KeyboardInterrupt, SystemExit):
break
# except:
# e = 1
# print "no"
server.close()
sys.exit(1)