File: utils.py

package info (click to toggle)
libsearpc 3.2.1-1%2Breally3.2%2Bgit20220902.15f6f0b-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 524 kB
  • sloc: ansic: 4,094; python: 863; makefile: 111; sh: 68
file content (45 lines) | stat: -rw-r--r-- 1,099 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
import os
import socket

from pysearpc.errors import NetworkError

def recvall(fd, total):
    remain = total
    data = bytearray()
    while remain > 0:
        try:
            new = fd.recv(remain)
        except socket.error as e:
            raise NetworkError('Failed to read from socket: %s' % e)

        n = len(new)
        if n <= 0:
            raise NetworkError("Failed to read from socket")
        else:
            data.extend(new)
            remain -= n

    return bytes(data)

def sendall(fd, data):
    total = len(data)
    offset = 0
    while offset < total:
        try:
            n = fd.send(data[offset:])
        except socket.error as e:
            raise NetworkError('Failed to write to socket: %s' % e)

        if n <= 0:
            raise NetworkError('Failed to write to socket')
        else:
            offset += n

def is_win32():
    return os.name == 'nt'

def make_socket_closeonexec(fd):
    if not is_win32():
        import fcntl
        old_flags = fcntl.fcntl(fd, fcntl.F_GETFD)
        fcntl.fcntl(fd, fcntl.F_SETFD, old_flags | fcntl.FD_CLOEXEC)