Skip to content

Instantly share code, notes, and snippets.

@heisvoid
Last active November 17, 2022 04:16
Dahua IP camera event listener
import argparse
import pycurl
connected = False
ap = argparse.ArgumentParser(description="Monitor event notifications from Dahua IP camera.")
ap.add_argument("-i", required=True, help="User ID", dest="id")
ap.add_argument("-p", required=True, help="User Password", dest="pw")
ap.add_argument("-a", required=True, help="Address for a camera", dest="addr")
ap.add_argument("-n", default=80, required=False, help="Port number for a camera", dest="port")
args = ap.parse_args()
def on_receive(data):
d = data.decode()
for line in d.split("\r\n"):
if "HTTP/1.1 200 OK" == line:
global connected
assert False == connected
connected = True
elif line.startswith("Code="):
print(line, flush=True)
if "__main__" == __name__:
url = "http://{0}:{1}@{2}:{3}/cgi-bin/eventManager.cgi?action=attach&codes=[VideoMotion]"
url = url.format(args.id, args.pw, args.addr, args.port)
c = pycurl.Curl()
c.setopt(pycurl.URL, url)
c.setopt(pycurl.CONNECTTIMEOUT, 10)
c.setopt(pycurl.TCP_KEEPALIVE, 1)
c.setopt(pycurl.HTTPAUTH, pycurl.HTTPAUTH_DIGEST)
c.setopt(pycurl.WRITEFUNCTION, on_receive)
c.perform()
c.close()
assert connected
@heisvoid
Copy link
Author

heisvoid commented Dec 24, 2017

You can do timestamping the output like the follwoing.

python dahua_ipc_event_listener.py -i user -p password -a 192.168.0.100 | while read line; do echo `date +%FT%T%::z` "$line"; done

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment