def socket_read_n(sock, n):
 buf = ''
 while (n > 0):
  data = sock.recv(n)
  if (data == ''):
   raise RuntimeError('unexpected connection close')
  buf += data
  n -= len(data)
 return buf

def socket_read_n ( sock , n ) : buf = '' while ( n > 0 ) : data = sock . recv ( n ) if ( data == '' ) : raise RuntimeError ( 'unexpected connection close' ) buf += data n -= len ( data ) return buf
read exactly n bytes from the socket .