How to use _receive_file_chunks method in autotest

Best Python code snippet using autotest_python

rss_client.py

Source:rss_client.py Github

copy

Full Screen

...164 e.filename = filename165 raise166 finally:167 f.close()168 def _receive_file_chunks(self, filename, timeout=60):169 if self._log_func:170 self._log_func("Receiving file %s" % filename)171 f = open(filename, "wb")172 try:173 try:174 end_time = time.time() + timeout175 while True:176 data = self._receive_packet(end_time - time.time())177 f.write(data)178 if len(data) < CHUNKSIZE:179 break180 except FileTransferError, e:181 e.filename = filename182 raise183 finally:184 f.close()185 def _send_msg(self, msg, timeout=60):186 self._send(struct.pack("=I", msg))187 def _receive_msg(self, timeout=60):188 s = self._receive(4, timeout)189 return struct.unpack("=I", s)[0]190 def _handle_transfer_error(self):191 # Save original exception192 e = sys.exc_info()193 try:194 # See if we can get an error message195 msg = self._receive_msg()196 except FileTransferError:197 # No error message -- re-raise original exception198 raise e[0], e[1], e[2]199 if msg == RSS_ERROR:200 errmsg = self._receive_packet()201 raise FileTransferServerError(errmsg)202 raise e[0], e[1], e[2]203class FileUploadClient(FileTransferClient):204 """205 Connect to a RSS (remote shell server) and upload files or directory trees.206 """207 def __init__(self, address, port, log_func=None, timeout=20):208 """209 Connect to a server.210 @param address: The server's address211 @param port: The server's port212 @param log_func: If provided, transfer stats will be passed to this213 function during the transfer214 @param timeout: Time duration to wait for connection to succeed215 @raise FileTransferConnectError: Raised if the connection fails216 @raise FileTransferProtocolError: Raised if an incorrect magic number217 is received218 @raise FileTransferSocketError: Raised if the RSS_UPLOAD message cannot219 be sent to the server220 """221 super(FileUploadClient, self).__init__(address, port, log_func, timeout)222 self._send_msg(RSS_UPLOAD)223 def _upload_file(self, path, end_time):224 if os.path.isfile(path):225 self._send_msg(RSS_CREATE_FILE)226 self._send_packet(os.path.basename(path))227 self._send_file_chunks(path, end_time - time.time())228 elif os.path.isdir(path):229 self._send_msg(RSS_CREATE_DIR)230 self._send_packet(os.path.basename(path))231 for filename in os.listdir(path):232 self._upload_file(os.path.join(path, filename), end_time)233 self._send_msg(RSS_LEAVE_DIR)234 def upload(self, src_pattern, dst_path, timeout=600):235 """236 Send files or directory trees to the server.237 The semantics of src_pattern and dst_path are similar to those of scp.238 For example, the following are OK:239 src_pattern='/tmp/foo.txt', dst_path='C:\\'240 (uploads a single file)241 src_pattern='/usr/', dst_path='C:\\Windows\\'242 (uploads a directory tree recursively)243 src_pattern='/usr/*', dst_path='C:\\Windows\\'244 (uploads all files and directory trees under /usr/)245 The following is not OK:246 src_pattern='/tmp/foo.txt', dst_path='C:\\Windows\\*'247 (wildcards are only allowed in src_pattern)248 @param src_pattern: A path or wildcard pattern specifying the files or249 directories to send to the server250 @param dst_path: A path in the server's filesystem where the files will251 be saved252 @param timeout: Time duration in seconds to wait for the transfer to253 complete254 @raise FileTransferTimeoutError: Raised if timeout expires255 @raise FileTransferServerError: Raised if something goes wrong and the256 server sends an informative error message to the client257 @note: Other exceptions can be raised.258 """259 end_time = time.time() + timeout260 try:261 try:262 self._send_msg(RSS_SET_PATH)263 self._send_packet(dst_path)264 matches = glob.glob(src_pattern)265 for filename in matches:266 self._upload_file(os.path.abspath(filename), end_time)267 self._send_msg(RSS_DONE)268 except FileTransferTimeoutError:269 raise270 except FileTransferError:271 self._handle_transfer_error()272 else:273 # If nothing was transferred, raise an exception274 if not matches:275 raise FileTransferNotFoundError("Pattern %s does not "276 "match any files or "277 "directories" %278 src_pattern)279 # Look for RSS_OK or RSS_ERROR280 msg = self._receive_msg(end_time - time.time())281 if msg == RSS_OK:282 return283 elif msg == RSS_ERROR:284 errmsg = self._receive_packet()285 raise FileTransferServerError(errmsg)286 else:287 # Neither RSS_OK nor RSS_ERROR found288 raise FileTransferProtocolError("Received unexpected msg")289 except:290 # In any case, if the transfer failed, close the connection291 self.close()292 raise293class FileDownloadClient(FileTransferClient):294 """295 Connect to a RSS (remote shell server) and download files or directory trees.296 """297 def __init__(self, address, port, log_func=None, timeout=20):298 """299 Connect to a server.300 @param address: The server's address301 @param port: The server's port302 @param log_func: If provided, transfer stats will be passed to this303 function during the transfer304 @param timeout: Time duration to wait for connection to succeed305 @raise FileTransferConnectError: Raised if the connection fails306 @raise FileTransferProtocolError: Raised if an incorrect magic number307 is received308 @raise FileTransferSendError: Raised if the RSS_UPLOAD message cannot309 be sent to the server310 """311 super(FileDownloadClient, self).__init__(address, port, log_func, timeout)312 self._send_msg(RSS_DOWNLOAD)313 def download(self, src_pattern, dst_path, timeout=600):314 """315 Receive files or directory trees from the server.316 The semantics of src_pattern and dst_path are similar to those of scp.317 For example, the following are OK:318 src_pattern='C:\\foo.txt', dst_path='/tmp'319 (downloads a single file)320 src_pattern='C:\\Windows', dst_path='/tmp'321 (downloads a directory tree recursively)322 src_pattern='C:\\Windows\\*', dst_path='/tmp'323 (downloads all files and directory trees under C:\\Windows)324 The following is not OK:325 src_pattern='C:\\Windows', dst_path='/tmp/*'326 (wildcards are only allowed in src_pattern)327 @param src_pattern: A path or wildcard pattern specifying the files or328 directories, in the server's filesystem, that will be sent to329 the client330 @param dst_path: A path in the local filesystem where the files will331 be saved332 @param timeout: Time duration in seconds to wait for the transfer to333 complete334 @raise FileTransferTimeoutError: Raised if timeout expires335 @raise FileTransferServerError: Raised if something goes wrong and the336 server sends an informative error message to the client337 @note: Other exceptions can be raised.338 """339 dst_path = os.path.abspath(dst_path)340 end_time = time.time() + timeout341 file_count = 0342 dir_count = 0343 try:344 try:345 self._send_msg(RSS_SET_PATH)346 self._send_packet(src_pattern)347 except FileTransferError:348 self._handle_transfer_error()349 while True:350 msg = self._receive_msg()351 if msg == RSS_CREATE_FILE:352 # Receive filename and file contents353 filename = self._receive_packet()354 if os.path.isdir(dst_path):355 dst_path = os.path.join(dst_path, filename)356 self._receive_file_chunks(dst_path, end_time - time.time())357 dst_path = os.path.dirname(dst_path)358 file_count += 1359 elif msg == RSS_CREATE_DIR:360 # Receive dirname and create the directory361 dirname = self._receive_packet()362 if os.path.isdir(dst_path):363 dst_path = os.path.join(dst_path, dirname)364 if not os.path.isdir(dst_path):365 os.mkdir(dst_path)366 dir_count += 1367 elif msg == RSS_LEAVE_DIR:368 # Return to parent dir369 dst_path = os.path.dirname(dst_path)370 elif msg == RSS_DONE:...

Full Screen

Full Screen

rss_file_transfer.py

Source:rss_file_transfer.py Github

copy

Full Screen

...110 raise FileTransferTimeoutError("Timeout expired while sending "111 "file %s" % filename)112 finally:113 f.close()114 def _receive_file_chunks(self, filename, timeout=30):115 f = open(filename, "wb")116 try:117 end_time = time.time() + timeout118 while True:119 try:120 data = self._receive_packet(end_time - time.time())121 except FileTransferTimeoutError:122 raise FileTransferTimeoutError("Timeout expired while "123 "receiving file %s" %124 filename)125 except FileTransferProtocolError:126 raise FileTransferProtocolError("Error receiving file %s" %127 filename)128 f.write(data)129 if len(data) < CHUNKSIZE:130 break131 finally:132 f.close()133 def _send_msg(self, msg, timeout=10):134 self._send(struct.pack("=I", msg))135 def _receive_msg(self, timeout=10):136 s = self._receive(4, timeout)137 return struct.unpack("=I", s)[0]138 def _handle_transfer_error(self):139 # Save original exception140 e = sys.exc_info()141 try:142 # See if we can get an error message143 msg = self._receive_msg()144 except FileTransferError:145 # No error message -- re-raise original exception146 raise e[0], e[1], e[2]147 if msg == RSS_ERROR:148 errmsg = self._receive_packet()149 raise FileTransferServerError("Server said: %s" % errmsg)150 raise e[0], e[1], e[2]151class FileUploadClient(FileTransferClient):152 """153 Connect to a RSS (remote shell server) and upload files or directory trees.154 """155 def __init__(self, address, port, timeout=10):156 """157 Connect to a server.158 @param address: The server's address159 @param port: The server's port160 @param timeout: Time duration to wait for connection to succeed161 @raise FileTransferConnectError: Raised if the connection fails162 @raise FileTransferProtocolError: Raised if an incorrect magic number163 is received164 @raise FileTransferSendError: Raised if the RSS_UPLOAD message cannot165 be sent to the server166 """167 super(FileUploadClient, self).__init__(address, port, timeout)168 self._send_msg(RSS_UPLOAD)169 def _upload_file(self, path, end_time):170 if os.path.isfile(path):171 self._send_msg(RSS_CREATE_FILE)172 self._send_packet(os.path.basename(path))173 self._send_file_chunks(path, max(0, end_time - time.time()))174 elif os.path.isdir(path):175 self._send_msg(RSS_CREATE_DIR)176 self._send_packet(os.path.basename(path))177 for filename in os.listdir(path):178 self._upload_file(os.path.join(path, filename), end_time)179 self._send_msg(RSS_LEAVE_DIR)180 def upload(self, src_pattern, dst_path, timeout=600):181 """182 Send files or directory trees to the server.183 The semantics of src_pattern and dst_path are similar to those of scp.184 For example, the following are OK:185 src_pattern='/tmp/foo.txt', dst_path='C:\\'186 (uploads a single file)187 src_pattern='/usr/', dst_path='C:\\Windows\\'188 (uploads a directory tree recursively)189 src_pattern='/usr/*', dst_path='C:\\Windows\\'190 (uploads all files and directory trees under /usr/)191 The following is not OK:192 src_pattern='/tmp/foo.txt', dst_path='C:\\Windows\\*'193 (wildcards are only allowed in src_pattern)194 @param src_pattern: A path or wildcard pattern specifying the files or195 directories to send to the server196 @param dst_path: A path in the server's filesystem where the files will197 be saved198 @param timeout: Time duration in seconds to wait for the transfer to199 complete200 @raise FileTransferTimeoutError: Raised if timeout expires201 @raise FileTransferServerError: Raised if something goes wrong and the202 server sends an informative error message to the client203 @note: Other exceptions can be raised.204 """205 end_time = time.time() + timeout206 try:207 try:208 self._send_msg(RSS_SET_PATH)209 self._send_packet(dst_path)210 matches = glob.glob(src_pattern)211 for filename in matches:212 self._upload_file(os.path.abspath(filename), end_time)213 self._send_msg(RSS_DONE)214 except FileTransferTimeoutError:215 raise216 except FileTransferError:217 self._handle_transfer_error()218 else:219 # If nothing was transferred, raise an exception220 if not matches:221 raise FileTransferNotFoundError("Pattern %s does not "222 "match any files or "223 "directories" %224 src_pattern)225 # Look for RSS_OK or RSS_ERROR226 msg = self._receive_msg(max(0, end_time - time.time()))227 if msg == RSS_OK:228 return229 elif msg == RSS_ERROR:230 errmsg = self._receive_packet()231 raise FileTransferServerError("Server said: %s" % errmsg)232 else:233 # Neither RSS_OK nor RSS_ERROR found234 raise FileTransferProtocolError("Received unexpected msg")235 except:236 # In any case, if the transfer failed, close the connection237 self.close()238 raise239class FileDownloadClient(FileTransferClient):240 """241 Connect to a RSS (remote shell server) and download files or directory trees.242 """243 def __init__(self, address, port, timeout=10):244 """245 Connect to a server.246 @param address: The server's address247 @param port: The server's port248 @param timeout: Time duration to wait for connection to succeed249 @raise FileTransferConnectError: Raised if the connection fails250 @raise FileTransferProtocolError: Raised if an incorrect magic number251 is received252 @raise FileTransferSendError: Raised if the RSS_UPLOAD message cannot253 be sent to the server254 """255 super(FileDownloadClient, self).__init__(address, port, timeout)256 self._send_msg(RSS_DOWNLOAD)257 def download(self, src_pattern, dst_path, timeout=600):258 """259 Receive files or directory trees from the server.260 The semantics of src_pattern and dst_path are similar to those of scp.261 For example, the following are OK:262 src_pattern='C:\\foo.txt', dst_path='/tmp'263 (downloads a single file)264 src_pattern='C:\\Windows', dst_path='/tmp'265 (downloads a directory tree recursively)266 src_pattern='C:\\Windows\\*', dst_path='/tmp'267 (downloads all files and directory trees under C:\\Windows)268 The following is not OK:269 src_pattern='C:\\Windows', dst_path='/tmp/*'270 (wildcards are only allowed in src_pattern)271 @param src_pattern: A path or wildcard pattern specifying the files or272 directories, in the server's filesystem, that will be sent to273 the client274 @param dst_path: A path in the local filesystem where the files will275 be saved276 @param timeout: Time duration in seconds to wait for the transfer to277 complete278 @raise FileTransferTimeoutError: Raised if timeout expires279 @raise FileTransferServerError: Raised if something goes wrong and the280 server sends an informative error message to the client281 @note: Other exceptions can be raised.282 """283 dst_path = os.path.abspath(dst_path)284 end_time = time.time() + timeout285 file_count = 0286 dir_count = 0287 try:288 try:289 self._send_msg(RSS_SET_PATH)290 self._send_packet(src_pattern)291 except FileTransferError:292 self._handle_transfer_error()293 while True:294 msg = self._receive_msg()295 if msg == RSS_CREATE_FILE:296 # Receive filename and file contents297 filename = self._receive_packet()298 if os.path.isdir(dst_path):299 dst_path = os.path.join(dst_path, filename)300 self._receive_file_chunks(301 dst_path, max(0, end_time - time.time()))302 dst_path = os.path.dirname(dst_path)303 file_count += 1304 elif msg == RSS_CREATE_DIR:305 # Receive dirname and create the directory306 dirname = self._receive_packet()307 if os.path.isdir(dst_path):308 dst_path = os.path.join(dst_path, dirname)309 if not os.path.isdir(dst_path):310 os.mkdir(dst_path)311 dir_count += 1312 elif msg == RSS_LEAVE_DIR:313 # Return to parent dir314 dst_path = os.path.dirname(dst_path)...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful