@@ -36,13 +36,16 @@ def _init_socket(self):
36
36
try :
37
37
self .clamd_socket = socket .socket (self .socket_type , socket .SOCK_STREAM )
38
38
39
+ # Set timeout prior to connecting to ensure that an initial
40
+ # connection timeout will respect the setting regardless of OS.
41
+ # https://docs.python.org/3/library/socket.html#timeouts-and-the-connect-method
42
+ self .clamd_socket .settimeout (self .timeout )
43
+
39
44
if self .socket_type == socket .AF_INET :
40
45
self .clamd_socket .connect ((self .host , self .port ))
41
46
elif self .socket_type == socket .AF_UNIX :
42
47
self .clamd_socket .connect (self .unix_socket )
43
48
44
- self .clamd_socket .settimeout (self .timeout )
45
-
46
49
except socket .error :
47
50
if self .socket_type == socket .AF_UNIX :
48
51
error_message = f'Error connecting to Unix socket "{ self .unix_socket } "'
@@ -129,11 +132,13 @@ def _file_system_scan(self, command, file):
129
132
finally :
130
133
self ._close_socket ()
131
134
132
- def instream (self , buff ):
135
+ def instream (self , buff , max_chunk_size = 1024 ):
133
136
"""
134
137
Scan a buffer
135
138
136
139
buff filelikeobj: buffer to scan
140
+ max_chunk_size int: Maximum size of chunk to send to clamd in bytes
141
+ MUST be < StreamMaxLength in /etc/clamav/clamd.conf
137
142
138
143
return:
139
144
- (dict): {filename1: ("virusname", "status")}
@@ -147,15 +152,13 @@ def instream(self, buff):
147
152
self ._init_socket ()
148
153
self ._send_command ("INSTREAM" )
149
154
150
- max_chunk_size = 1024 # MUST be < StreamMaxLength in /etc/clamav/clamd.conf
151
-
152
155
chunk = buff .read (max_chunk_size )
153
156
while chunk :
154
157
size = struct .pack (b"!L" , len (chunk ))
155
- self .clamd_socket .send (size + chunk )
158
+ self .clamd_socket .sendall (size + chunk )
156
159
chunk = buff .read (max_chunk_size )
157
160
158
- self .clamd_socket .send (struct .pack (b"!L" , 0 ))
161
+ self .clamd_socket .sendall (struct .pack (b"!L" , 0 ))
159
162
160
163
result = self ._recv_response ()
161
164
@@ -195,7 +198,7 @@ def _send_command(self, cmd, *args):
195
198
196
199
# cmd = 'n{cmd}{args}\n'.format(cmd=cmd, args=concat_args).encode('utf-8')
197
200
cmd = f"n{ cmd } { concat_args } \n " .encode ("utf-8" )
198
- self .clamd_socket .send (cmd )
201
+ self .clamd_socket .sendall (cmd )
199
202
200
203
def _recv_response (self ):
201
204
"""
0 commit comments