天天看點

python/C++根據ip位址擷取ipc錄影機rtsp位址

倉庫代碼:https://gitee.com/liudegui/gsoap-onvif

  • 借鑒于https://github.com/xris-hu/gsoap-onvif ,根據ip位址擷取ipc錄影機rtsp位址;
  • merge高版本openssl不能編譯的bug;
  • 去除備援代碼,修改app為動态庫,增加

    get_rtsp_url

    c接口,增加測試程式;
  • 傳回rtsp位址增加賬号和密碼;
  • Makefile改為CMakeLists.txt;
  • 增加基于cffi的封裝子產品ipconvif.py

C++測試代碼

#include "ipconvif.h"
#include <iostream>
using namespace std;

int main()
{
    char buf1[1024] = {0};
    char buf2[1024] = {0};
    int ret = get_rtsp_url(buf1, buf2, "192.21.1.201", "admin", "123cpucpu");
    if (ret != 0)
    {
        std::cout << "not find" << std::endl;
    }

    return 0;
}
           

Python調用C++庫代碼

from cffi import FFI
import os
import sys


ffi = FFI()

ffi.cdef('''
    int get_rtsp_url(char* rtsp_url1, char* rtsp_url2, const char *ip, const char *username, const char *password);
''')

LIB_NAME = 'libipconvif.so'


class Ipconvif():
    def __init__(self, ip, username, passwd):
        self.rtsp_url1 = ffi.new("char[]", 1024) # first url
        self.rtsp_url2 = ffi.new("char[]", 1024) # second url
        self.ip = ffi.new("char[]", ip.encode())
        self.username = ffi.new("char[]", username.encode())
        self.passwd = ffi.new("char[]", passwd.encode())
        
        self.lib = None
        self.load_library()
    
    def load_library(self):
        lib_path = LIB_NAME
        if os.path.exists(lib_path):
            self.lib = ffi.dlopen(lib_path)
            return True
        return False
        
    def get_rtsp_url(self):
        self.lib.get_rtsp_url(self.rtsp_url1, self.rtsp_url2, self.ip, self.username, self.passwd)
        return ffi.string(self.rtsp_url1), ffi.string(self.rtsp_url2)
        
                
        
if __name__ == "__main__":
    ipc = Ipconvif('192.21.1.201', 'admin', '123cpucpu')
    urls = ipc.get_rtsp_url();
    print(urls[0])
    print(urls[1])
           

繼續閱讀