Skip to content

Commit

Permalink
Set a global default timeout of 30s
Browse files Browse the repository at this point in the history
  • Loading branch information
BjarniRunar committed May 17, 2020
1 parent ccb4267 commit b7385da
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions sockschain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
b = lambda s: s.encode('latin-1')

DEBUG = False
DEFAULT_TIMEOUT = 30
#def DEBUG(foo): print foo


Expand Down Expand Up @@ -554,18 +555,21 @@ def __setattr__(self, name, value):
return setattr(object.__getattribute__(self, "_socksocket__sock"),
name, value)

def __settimeout(self, timeout):
try:
self.__sock.settimeout(timeout)
except:
# Python 2.2 compatibility hacks.
pass

def __recvall(self, count):
"""__recvall(count) -> data
Receive EXACTLY the number of bytes requested from the socket.
Blocks until the required number of bytes have been received or a
timeout occurs.
"""
self.__sock.setblocking(1)
try:
self.__sock.settimeout(20)
except:
# Python 2.2 compatibility hacks.
pass
self.__settimeout(DEFAULT_TIMEOUT)

data = self.recv(count)
while len(data) < count:
Expand Down Expand Up @@ -982,15 +986,18 @@ def __do_connect(self, addrspec):
if ':' in addrspec[0]:
self.__sock = _orgsocket(socket.AF_INET6, self.__type, self.__proto,
*self.__args, **self.__kwargs)
self.__settimeout(DEFAULT_TIMEOUT)
return self.__sock.connect(addrspec)
else:
try:
self.__sock = _orgsocket(socket.AF_INET, self.__type, self.__proto,
*self.__args, **self.__kwargs)
self.__settimeout(DEFAULT_TIMEOUT)
return self.__sock.connect(addrspec)
except socket.gaierror:
self.__sock = _orgsocket(socket.AF_INET6, self.__type, self.__proto,
*self.__args, **self.__kwargs)
self.__settimeout(DEFAULT_TIMEOUT)
return self.__sock.connect(addrspec)

def connect(self, destpair):
Expand Down

0 comments on commit b7385da

Please sign in to comment.