Skip to content

Instantly share code, notes, and snippets.

@ariscop
Created March 2, 2015 01:11
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ariscop/478d827283c8d67d4768 to your computer and use it in GitHub Desktop.
Save ariscop/478d827283c8d67d4768 to your computer and use it in GitHub Desktop.
Python script that 'emulates' topsee devices
#!/usr/bin/env python3
from socket import socket
import xml.etree.ElementTree as ET
magic = b'\x58\x91\x58\x51'
auth_response = """<?xml version="1.0" encoding="GB2312" ?>
<XML_TOPSEE>
<MESSAGE_HEADER
Msg_type="USER_AUTH_MESSAGE"
Msg_code="CMD_USER_AUTH"
Msg_flag="0" />
<MESSAGE_BODY>
<USER_AUTH_RESPONSE
Sessionid="20150226100807_2ba514d2ca8ef870"
Group="Administrator"
/>
</MESSAGE_BODY>
</XML_TOPSEE>
""".encode()
heartbeat_response = """<?xml version="1.0" encoding="GB2312" ?>
<XML_TOPSEE>
<MESSAGE_HEADER
Msg_type="AUXPTZ_HEARTBEAT_MESSAGE"
Msg_code="CMD_HEARTBEAT"
Msg_flag="0"
/>
<MESSAGE_BODY>
</MESSAGE_BODY>
</XML_TOPSEE>
""".encode()
def to_hex(a):
return ''.join("%02x" % x for x in a)
server = socket()
server.bind(("0.0.0.0", 8091))
server.listen(1)
client, address = server.accept()
print("Connection from",address)
while True:
client.recv(4) # magic
length = client.recv(4)
length = int.from_bytes(length, "little")
xml = ET.fromstring(client.recv(length).decode())
hdr = xml.find('./MESSAGE_HEADER')
msg_code = hdr.attrib['Msg_code']
print(msg_code, length)
if (msg_code == "CMD_USER_AUTH"):
client.send(magic)
client.send(len(auth_response).to_bytes(4, "little"))
client.send(auth_response)
elif(msg_code == "CMD_HEARTBEAT"):
client.send(magic)
client.send(len(heartbeat_response).to_bytes(4, "little"))
client.send(heartbeat_response)
else:
print(ET.tostring(xml))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment