From a9603ec450b14220b288f4999e6b8ca3f93683a9 Mon Sep 17 00:00:00 2001 From: Dmitry Ermakov Date: Tue, 6 Feb 2024 21:30:56 +0300 Subject: [PATCH] Allow binding TCP socket to interface by passing 'iface' arg. Linux only. --- dvrip.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/dvrip.py b/dvrip.py index b234ca2..820e74d 100644 --- a/dvrip.py +++ b/dvrip.py @@ -4,7 +4,7 @@ import json from time import sleep import hashlib import threading -from socket import socket, AF_INET, SOCK_STREAM, SOCK_DGRAM +from socket import socket, AF_INET, SOCK_STREAM, SOCK_DGRAM, SOL_SOCKET from datetime import * from re import compile import time @@ -100,6 +100,7 @@ class DVRIPCam(object): def __init__(self, ip, **kwargs): self.logger = logging.getLogger(__name__) self.ip = ip + self.iface = kwargs.get("iface", None) self.user = kwargs.get("user", "admin") hash_pass = kwargs.get("hash_pass") self.hash_pass = kwargs.get( @@ -130,6 +131,9 @@ class DVRIPCam(object): self.socket_send = self.tcp_socket_send self.socket_recv = self.tcp_socket_recv self.socket = socket(AF_INET, SOCK_STREAM) + if self.iface: + self.socket.setsockopt( + SOL_SOCKET, 25, str(self.iface + '\0').encode()) self.socket.connect((self.ip, self.port)) elif self.proto == "udp": self.socket_send = self.udp_socket_send