def _recv_msg(sock,size,timeout):
_sock_timeout_recv(sock,timeout)
try:
chunk=sock.recv(size, socket.MSG_WAITALL) # receive all data in one call
except TypeError:
# M2Crypto sock.recv() doesn't support MSG_WAITALL parameter
return __recv_msg_compat(sock,size,timeout)
else:
if len(chunk)!=size:
err=ConnectionClosedError('connection lost')
err.partialMsg=chunk # store the message that was received until now
raise err
return chunk
python类MSG_WAITALL的实例源码
def run(self):
s = None
try:
# Do not set the timeout on the socket, leave it in the blocking
# mode as setting the timeout seems to cause spurious EAGAIN
# errors on OSX.
self._socket.settimeout(None)
s, unused_addr = self._socket.accept()
resp_buffer = ''
resp_1 = s.recv(6).decode()
nstr, extra = resp_1.split('\n', 1)
resp_buffer = extra
n = int(nstr)
to_read = n - len(extra)
if to_read > 0:
resp_buffer += _helpers._from_bytes(
s.recv(to_read, socket.MSG_WAITALL))
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
self.bad_request = True
response_len = len(self.response)
s.sendall('{0}\n{1}'.format(response_len, self.response).encode())
finally:
# Will fail if s is None, but these tests never encounter
# that scenario.
s.close()
def run(self):
s = None
try:
# Do not set the timeout on the socket, leave it in the blocking
# mode as setting the timeout seems to cause spurious EAGAIN
# errors on OSX.
self._socket.settimeout(None)
s, unused_addr = self._socket.accept()
resp_buffer = ''
resp_1 = s.recv(6).decode()
nstr, extra = resp_1.split('\n', 1)
resp_buffer = extra
n = int(nstr)
to_read = n - len(extra)
if to_read > 0:
resp_buffer += _helpers._from_bytes(
s.recv(to_read, socket.MSG_WAITALL))
if resp_buffer != devshell.CREDENTIAL_INFO_REQUEST_JSON:
self.bad_request = True
response_len = len(self.response)
s.sendall('{0}\n{1}'.format(response_len, self.response).encode())
finally:
# Will fail if s is None, but these tests never encounter
# that scenario.
s.close()
def test_slow_receiving_client(self):
"""
This test checks that the NGAS server doesn't hang forever on a slow
client, since it would block the server for ever
"""
timeout = 3
amount_of_data = 10*1024*1024 # 10 MBs
spaces = " " * amount_of_data
self.prepExtSrv(cfgProps=[["NgamsCfg.Server[1].TimeOut",str(timeout)]])
client = sendPclCmd()
status = client.archive_data(spaces, 'some-file.data', 'application/octet-stream')
self.assertEquals(NGAMS_SUCCESS, status.getStatus())
# Normal retrieval works fine
self.assertEquals(NGAMS_SUCCESS, client.retrieve(fileId='some-file.data').getStatus())
os.unlink('some-file.data')
# Now retrieve the data, but sloooooooooooowly and check that the server
# times out and closes the connection, which in turn makes our receiving
# end finish earlier than expected. This is detected on the client side
# because we receive less data than we ask for).
#
# We have to make sure that the receiving buffer is tiny so the server
# really can't write any more data into the socket. In the same spirit
# we specify a very small send buffer for the server. We don't need to
# specify a timeout because the recv will return immediately if the
# server has closed the connection.
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_RCVBUF, 256)
s.connect(('localhost', 8888))
s.send('GET /RETRIEVE?file_id=some-file.data&send_buffer=1024 HTTP/1.0\r\n')
s.send('\r\n')
time.sleep(timeout + 2) # More than enough to provoke a server timeout
data = s.recv(amount_of_data, socket.MSG_WAITALL)
self.assertLess(len(data), amount_of_data, "Should have read less data")
self.assertEquals('', s.recv(amount_of_data - len(data)))
s.close()
def _process(self):
try:
# Receive UDP packets.
recv_string = self.acq_socket.recv(self.buf_size, socket.MSG_WAITALL)
# Change data format.
batch = self.read_live_udp_packet(recv_string, self.acq_dtype, self.acq_nb_chan, self.dtype)
# Send output.
self.output.send(batch)
# Increment counter for buffer receptions.
self.step_nb += 1
except:
raise NotImplementedError()
return
def _initialize(self):
'''TODO add docstring.'''
self.output.configure(dtype=self.dtype, shape=(self.nb_samples, self.nb_channels))
self.queue = Queue.Queue()
self.size = self.nb_channels * self.nb_samples * 2 # i.e. nb_chan * nb_step * size(uint16)
def recv_target(queue, size, host, port):
# Define the address of the input socket.
address = (host, port)
# Bind an input socket.
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Create a connection to this address.
s.connect(address)
# Receive data.
while True:
try:
recv_string = s.recv(size, socket.MSG_WAITALL)
except socket.error as e:
if e.errno == errno.ECONNRESET:
# Discard error message.
break
else:
raise e
queue.put(recv_string)
# Prepare background thread for data acquisition.
args = (self.queue, self.size, self.host, self.port)
self.recv_thread = threading.Thread(target=recv_target, args=args)
self.recv_thread.deamon = True
# Launch background thread for data acquisition.
self.log.info("{n} starts listening for data on {f}...".format(n=self.name, f="%s:%d" %(self.host, self.port)))
self.recv_thread.start()
return
def getData( self, mgroup, hostip, port=29495, pkts=1000, pktlen=1080, block=True, returnSddsAnalyzer=True):
totalRead=0.0
startTime = _time.time()
sock = None
ismulticast=False
blen=10240
bytesRead=0
requestedBytes=pkts*pktlen
data=[]
rawdata=''
try:
try:
ip_class=int(mgroup.split('.')[0])
if ip_class == '224' or ip_class == '239':
ismulticast=True
except:
pass
#print " Capturing ", mgroup, " host ", hostip, " port ", port
sock = _socket.socket(_socket.AF_INET, _socket.SOCK_DGRAM, _socket.IPPROTO_UDP)
sock.setsockopt(_socket.SOL_SOCKET, _socket.SO_REUSEADDR, 1)
sock.bind(("",port))
if ismulticast:
mreq=struct.pack('4s4s',_socket.inet_aton(mgroup),_socket.inet_aton(hostip))
sock.setsockopt(_socket.IPPROTO_IP, _socket.IP_ADD_MEMBERSHIP, mreq)
print "Capturing Socket Interface: (MULTICAST) Host Interface: " + hostip + " Multicast: " + mgroup + " Port: "+ str(port)
else:
print "Capturing Socket Interface: (UDP) Host Interface: " + hostip + " Source Address: " + mgroup + " Port: "+ str(port)
ncnt=0
while totalRead < requestedBytes:
rcvddata = sock.recv(blen,_socket.MSG_WAITALL)
rawdata=rawdata+rcvddata
data=data+list(rcvddata)
totalRead = totalRead + len(rcvddata)
ncnt += 1
print " read ", ncnt, " pkt ", len(rcvddata)
except KeyboardInterrupt,e :
traceback.print_exc()
print "Exception during packet capture: " + str(e)
except Exception, e :
traceback.print_exc()
print "Exception during packet capture: " + str(e)
finally:
endTime=_time.time()
deltaTime=endTime -startTime
if sock: sock.close()
print "Elapsed Time: ", deltaTime, " Total Data (kB): ", totalRead/1000.0, " Rate (kBps): ", (totalRead/1000.0)/deltaTime
if returnSddsAnalyzer:
from ossie.utils.sdds import SDDSAnalyzer
return SDDSAnalyzer( rawdata, pkts, pktlen, totalRead )
else:
return data, rawdata, (pktlen,pkts,totalRead)
def run(self):
video_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
video_socket.connect((self.host, ardrone.constant.VIDEO_PORT))
nav_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
nav_socket.setblocking(False)
nav_socket.bind(('', ardrone.constant.NAVDATA_PORT))
nav_socket.sendto(b'\x01\x00\x00\x00', (self.host, ardrone.constant.NAVDATA_PORT))
stopping = False
while not stopping:
inputready, outputready, exceptready = select.select([nav_socket, video_socket, self.com_pipe], [], [])
for i in inputready:
if i == video_socket:
# get first few bytes of header
data = video_socket.recv(12, socket.MSG_WAITALL)
if len(data) != 12:
continue
# decode relevant portions of the header
sig_p, sig_a, sig_v, sig_e, version, codec, header, payload = struct.unpack('4cBBHI', data)
# check signature (and ignore packet otherwise)
if sig_p != b'P' or sig_a != b'a' or sig_v != b'V' or sig_e != b'E':
continue
# get remaining frame
data += video_socket.recv(header - 12 + payload, socket.MSG_WAITALL)
try:
# decode the frame
image = ardrone.video.decode(data)
self.video_pipe.send(image)
except ardrone.video.DecodeError:
pass
elif i == nav_socket:
while 1:
try:
data = nav_socket.recv(65535)
except IOError:
# we consumed every packet from the socket and
# continue with the last one
break
navdata = ardrone.navdata.decode(data)
self.nav_pipe.send(navdata)
elif i == self.com_pipe:
_ = self.com_pipe.recv()
stopping = True
break
video_socket.close()
nav_socket.close()
def run(self):
server_sock = self._init_server()
server_sock.setblocking(0)
while self.is_active:
(rlist, _, _) = select.select(
[server_sock], [], [], SELECT_LOOP_INTERVAL)
if len(rlist) == 0:
continue
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
# header: a single '0' and then the length of the request string
just_zero = read_varint32(client_sock)
request_length = read_varint32(client_sock)
if just_zero != 0:
raise IOError
if request_length > 2 ** 24:
raise IOError
# request string
unpacker = struct.Struct('! %ss' % request_length)
body = client_sock.recv(unpacker.size, socket.MSG_WAITALL)
request = unpacker.unpack(body)
# send ok
write_varint32(client_sock, 200)
# monkey patch the payment request
# to include our Bluetooth address
fixed_width_bluetooth_address = self.get_bluetooth_address()
payment_request = PaymentRequest()
payment_request.ParseFromString(self.serialized_payment_request)
payment_details = PaymentDetails()
payment_details.ParseFromString(
payment_request.serialized_payment_details)
payment_details.payment_url = 'bt:%s' % \
fixed_width_bluetooth_address.replace(':', '')
payment_request.serialized_payment_details = \
payment_details.SerializeToString()
payment_request.ClearField('pki_type')
payment_request.ClearField('pki_data')
payment_request.ClearField('signature')
data = payment_request.SerializeToString()
# send payment request
write_varint32(client_sock, len(data))
client_sock.send(data)
except IOError:
pass
print "Bluetooth client disconnected"
client_sock.close()
server_sock.close()
def run(self):
server_sock = self._init_server()
server_sock.setblocking(0)
while self.is_active:
(rlist, _, _) = select.select(
[server_sock], [], [], SELECT_LOOP_INTERVAL)
if not rlist:
continue
client_sock, client_info = server_sock.accept()
print "Accepted connection from ", client_info
try:
# read length
tx_length = read_varint32(client_sock)
if tx_length > 2 ** 24:
raise IOError
# transaction
unpacker = struct.Struct('! %ss' % tx_length)
body = client_sock.recv(unpacker.size, socket.MSG_WAITALL)
(tx,) = unpacker.unpack(body)
# submit
r = requests.post(self.submission_url,
headers=TX_SUBMISSION_HEADERS, data=tx)
# monkey patch ack
payment_ack = PaymentACK()
payment_ack.ParseFromString(r.content)
payment_ack.memo = "ack"
payment_ack_data = payment_ack.SerializeToString()
# pass on ack
write_varint32(client_sock, len(payment_ack_data))
client_sock.send(payment_ack_data)
except IOError:
pass
print "Bluetooth client disconnected"
client_sock.close()
server_sock.close()
def connect(self):
if self.sock:
self.close()
s = socket.socket()
s.connect(self.endpoint)
hello = s.recv(8, socket.MSG_WAITALL)
if hello != b"FLUX0003":
raise NotSupportError()
self.sock = ssl.SSLSocket(s)
# Stage 1: Recv randbytes
self.randbytes = self.recv_bytes(64)
# Stage 2: Send public key
strkey = self.client_key.public_key_pem.decode("ascii")
self.send_text(strkey)
# Stage 3: Get public key status
resp = self.recv_text()
if resp == "sign":
# Stage 4.a: Sign
doc = HMAC(self.uuid.bytes, self.randbytes, sha1).digest()
signature = self.client_key.sign(doc)
self.send_text(to_hex(signature))
elif resp == "password":
# Stage 4.b: Send password
return
elif resp.startswith("error "):
raise raise_error(resp)
else:
raise NotSupportError("Auth method %s not support", resp)
resp = self.recv_text()
if resp == "ok":
self._authorized = True
return
elif resp.startswith("error "):
err = resp[6:]
if err == "AUTH_ERROR":
raise AuthError()
else:
raise UpnpError(err)
def receive_data(sock, size):
"""Retrieve a given number of bytes from a socket.
It is expected the socket is able to supply that number of bytes.
If it isn't, an exception is raised (you will not get a zero length result
or a result that is smaller than what you asked for). The partial data that
has been received however is stored in the 'partialData' attribute of
the exception object."""
try:
delays = __retrydelays()
msglen = 0
data = bytearray()
if USE_MSG_WAITALL and not hasattr(sock, "getpeercert"): # ssl doesn't support recv flags
while True:
try:
chunk = sock.recv(size, socket.MSG_WAITALL)
if len(chunk) == size:
return chunk
# less data than asked, drop down into normal receive loop to finish
msglen = len(chunk)
data.extend(chunk)
break
except socket.timeout:
raise TimeoutError("receiving: timeout")
except socket.error as x:
err = getattr(x, "errno", x.args[0])
if err not in ERRNO_RETRIES:
raise ConnectionClosedError("receiving: connection lost: " + str(x))
time.sleep(next(delays)) # a slight delay to wait before retrying
# old fashioned recv loop, we gather chunks until the message is complete
while True:
try:
while msglen < size:
# 60k buffer limit avoids problems on certain OSes like VMS, Windows
chunk = sock.recv(min(60000, size - msglen))
if not chunk:
break
data.extend(chunk)
msglen += len(chunk)
if len(data) != size:
err = ConnectionClosedError("receiving: not enough data")
err.partialData = data # store the message that was received until now
raise err
return data # yay, complete
except socket.timeout:
raise TimeoutError("receiving: timeout")
except socket.error as x:
err = getattr(x, "errno", x.args[0])
if err not in ERRNO_RETRIES:
raise ConnectionClosedError("receiving: connection lost: " + str(x))
time.sleep(next(delays)) # a slight delay to wait before retrying
except socket.timeout:
raise TimeoutError("receiving: timeout")