我们从Python开源项目中,提取了以下42个代码示例,用于说明如何使用socket.MSG_WAITALL。
def _SendRecv(): """Communicate with the Developer Shell server socket.""" port = int(os.getenv(DEVSHELL_ENV, 0)) if port == 0: raise NoDevshellServer() sock = socket.socket() sock.connect(('localhost', port)) data = CREDENTIAL_INFO_REQUEST_JSON msg = '%s\n%s' % (len(data), data) sock.sendall(_to_bytes(msg, encoding='utf-8')) header = sock.recv(6).decode() if '\n' not in header: raise CommunicationError('saw no newline in the first 6 bytes') len_str, json_str = header.split('\n', 1) to_read = int(len_str) - len(json_str) if to_read > 0: json_str += sock.recv(to_read, socket.MSG_WAITALL).decode() return CredentialInfoResponse(json_str)
def _SendRecv(): """Communicate with the Developer Shell server socket.""" port = int(os.getenv(DEVSHELL_ENV, 0)) if port == 0: raise NoDevshellServer() sock = socket.socket() sock.connect(('localhost', port)) data = CREDENTIAL_INFO_REQUEST_JSON msg = '{0}\n{1}'.format(len(data), data) sock.sendall(_helpers._to_bytes(msg, encoding='utf-8')) header = sock.recv(6).decode() if '\n' not in header: raise CommunicationError('saw no newline in the first 6 bytes') len_str, json_str = header.split('\n', 1) to_read = int(len_str) - len(json_str) if to_read > 0: json_str += sock.recv(to_read, socket.MSG_WAITALL).decode() return CredentialInfoResponse(json_str)
def __recv_msg_compat(sock,size,timeout): # compatibility implementation for non-MSG_WAITALL / M2Crypto msglen=0 msglist=[] # Receive chunks of max. 60kb size: # (rather arbitrary limit, but it avoids memory/buffer problems on certain OSes -- VAX/VMS, Windows) while msglen<size: chunk=sock.recv(min(60000,size-msglen)) if not chunk: if hasattr(sock,'pending'): # m2crypto ssl socket - they have problems with a defaulttimeout if socket.getdefaulttimeout() != None: raise ConnectionClosedError("m2crypto SSL can't be used when socket.setdefaulttimeout() has been set") err = ConnectionClosedError('connection lost') err.partialMsg=''.join(msglist) # store the message that was received until now raise err msglist.append(chunk) msglen+=len(chunk) return ''.join(msglist) # Send a message over a socket. Raises ConnectionClosedError if the msg # couldn't be sent (the connection has probably been lost then). # We need this because 'send' isn't guaranteed to send all desired # bytes in one call, for instance, when network load is high.
def _read_packet(self, socket_): if hasattr(socket, "MSG_WAITALL"): data = socket_.recv(3, socket.MSG_WAITALL) else: # Windows lacks MSG_WAITALL data = b'' while len(data) < 3: data += socket_.recv(3 - len(data)) type, length = struct.unpack(">BH", data) body_len = length - 3 while body_len > 0: read_len = 32767 if body_len > 32767 else body_len data += socket_.recv(read_len, socket.MSG_WAITALL) body_len -= read_len return type, data
def receive_data(self, channel, oc): # push data from a socket into an OutputConnector (oc) self.last_timestamp = datetime.datetime.now() # wire format is just: [size, buffer...] sock = self._chan_to_rsocket[channel] # TODO receive 4 or 8 bytes depending on sizeof(size_t) msg = sock.recv(8) # reinterpret as int (size_t) msg_size = struct.unpack('n', msg)[0] buf = sock.recv(msg_size, socket.MSG_WAITALL) if len(buf) != msg_size: logger.error("Channel %s socket msg shorter than expected" % channel.channel) logger.error("Expected %s bytes, received %s bytes" % (msg_size, len(buf))) # assume that we cannot recover, so stop listening. loop = asyncio.get_event_loop() loop.remove_reader(sock) return data = np.frombuffer(buf, dtype=channel.dtype) asyncio.ensure_future(oc.push(data))
def receive_data(self, channel, oc): # push data from a socket into an OutputConnector (oc) self.last_timestamp = datetime.datetime.now() self.fetch_count += 1 # wire format is just: [size, buffer...] sock = self._chan_to_rsocket[channel] # TODO receive 4 or 8 bytes depending on sizeof(size_t) msg = sock.recv(8) # reinterpret as int (size_t) msg_size = struct.unpack('n', msg)[0] buf = sock.recv(msg_size, socket.MSG_WAITALL) if len(buf) != msg_size: logger.error("Channel %s socket msg shorter than expected" % channel.channel) logger.error("Expected %s bytes, received %s bytes" % (msg_size, len(buf))) # assume that we cannot recover, so stop listening. loop = asyncio.get_event_loop() loop.remove_reader(sock) return data = np.frombuffer(buf, dtype=np.float32) asyncio.ensure_future(oc.push(data))
def _SendRecv(): """Communicate with the Developer Shell server socket.""" port = int(os.getenv(DEVSHELL_ENV, 0)) if port == 0: raise NoDevshellServer() import socket sock = socket.socket() sock.connect(('localhost', port)) data = CREDENTIAL_INFO_REQUEST_JSON msg = '%s\n%s' % (len(data), data) sock.sendall(msg.encode()) header = sock.recv(6).decode() if '\n' not in header: raise CommunicationError('saw no newline in the first 6 bytes') len_str, json_str = header.split('\n', 1) to_read = int(len_str) - len(json_str) if to_read > 0: json_str += sock.recv(to_read, socket.MSG_WAITALL).decode() return CredentialInfoResponse(json_str)
def test_bad_message_to_mock_server(self): request_content = devshell.CREDENTIAL_INFO_REQUEST_JSON + 'extrastuff' request_message = _helpers._to_bytes( '{0}\n{1}'.format(len(request_content), request_content)) response_message = 'foobar' with _AuthReferenceServer(response_message) as auth_server: self.assertFalse(auth_server.bad_request) sock = socket.socket() port = int(os.getenv(devshell.DEVSHELL_ENV, 0)) sock.connect(('localhost', port)) sock.sendall(request_message) # Mimic the receive part of _SendRecv header = sock.recv(6).decode() len_str, result = header.split('\n', 1) to_read = int(len_str) - len(result) result += sock.recv(to_read, socket.MSG_WAITALL).decode() self.assertTrue(auth_server.bad_request) self.assertEqual(result, response_message)
def assertXServer(self): """Assert that we can connect to an X-Server that is listening on the display set in the DISPLAY environment variable.""" display = Display.get() try: with socket.socket(socket.AF_UNIX, socket.SOCK_STREAM) as sock: sock.settimeout(1) sock.connect("/tmp/.X11-unix/X%d" % (int(display))) sock.sendall(b"l\0\x0b\0\0\0\0\0\0\0\0\0") # ^-- little-endian # ^^^^--- protocol version (11) response = sock.recv(8, socket.MSG_WAITALL) self.assertEqual(len(response), 8) self.assertNotEqual(response[0], 0) # 0 = Failure except Exception as e: raise AssertionError("No X-Server") from e
def read_varint32(sock): mask = (1 << 32) - 1 result = 0 shift = 0 while True: unpacker = struct.Struct('! B') data = sock.recv(unpacker.size, socket.MSG_WAITALL) (b,) = unpacker.unpack(data) result |= ((b & 0x7f) << shift) if not (b & 0x80): result &= mask return result shift += 7 if shift >= 64: raise IOError("Too many bytes when decoding varint.")
def _SendRecv(): """Communicate with the Developer Shell server socket.""" port = int(os.getenv(DEVSHELL_ENV, 0)) if port == 0: raise NoDevshellServer() sock = socket.socket() sock.connect(('localhost', port)) data = CREDENTIAL_INFO_REQUEST_JSON msg = '{0}\n{1}'.format(len(data), data) sock.sendall(_to_bytes(msg, encoding='utf-8')) header = sock.recv(6).decode() if '\n' not in header: raise CommunicationError('saw no newline in the first 6 bytes') len_str, json_str = header.split('\n', 1) to_read = int(len_str) - len(json_str) if to_read > 0: json_str += sock.recv(to_read, socket.MSG_WAITALL).decode() return CredentialInfoResponse(json_str)
def listen_data_avail(self, what): conn, addr = self.socket.accept() buf = conn.recv(8, socket.MSG_WAITALL) x = array.array('H') x.fromstring(buf) if x[0] == 0: self.catchPage = False elif x[0] == 1: self.ttx.update(0,0,492,250, self.zoom, self.filter_mode) if x[1] == 2303: x[1] = 0x0100 self.cur_page = "%s%s%s-%s%s/%s%s" % ((x[1]&0x0F00)>>8, (x[1]&0xF0)>>4, x[1]&0x0F, x[2]>>4, x[2]&0x0F, x[3]>>4, x[3]&0x0F) for i in self.onChangedEntry: i() elif x[0] == 2: self.daemonVersion = "%s.%s" % (x[1], x[2]) log("daemon version %s" % self.daemonVersion) conn.close()
def connect_zabbix(self, payload): """ Method used to send information to Zabbix :param payload: refers to the json message prepared to send to Zabbix :rtype : returns the response received by the Zabbix API """ s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((self.zabbix_host, int(self.zabbix_port))) s.send(payload) # read its response, the first five bytes are the header again response_header = s.recv(5, socket.MSG_WAITALL) if not response_header == 'ZBXD\1': raise ValueError('Got invalid response') # read the data header to get the length of the response response_data_header = s.recv(8, socket.MSG_WAITALL) response_data_header = response_data_header[:4] response_len = struct.unpack('i', response_data_header)[0] # read the whole rest of the response now that we know the length response_raw = s.recv(response_len, socket.MSG_WAITALL) s.close() LOG.info(response_raw) response = json.loads(response_raw) return response
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
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")