mirror of
https://github.com/haiwen/libsearpc.git
synced 2025-09-01 21:07:24 +00:00
pysearpc: implemented named pipe client (and server, for testing)
This commit is contained in:
45
pysearpc/utils.py
Normal file
45
pysearpc/utils.py
Normal file
@@ -0,0 +1,45 @@
|
||||
import os
|
||||
import socket
|
||||
|
||||
from pysearpc.errors import NetworkError
|
||||
|
||||
def recvall(fd, total):
|
||||
remain = total
|
||||
data = ''
|
||||
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 += new
|
||||
remain -= n
|
||||
|
||||
return 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)
|
Reference in New Issue
Block a user