pymensago/serverconn.py
def download(conn: ServerConnection, server_path: str, local_path: str, offset=-1) -> RetVal:
	'''Downloads a file from the server. If an offset is given, resuming an interrupted download 
	is possible.'''

	try:
		handle = open(local_path, "wb")
	except Exception as e:
		return RetVal().wrap_exception(e)
	
	request = {
		'Action' : 'DOWNLOAD',
		'Data' : {
			'Path' : server_path
		}
	}
	if offset > 0:
		request['Data']['Offset'] = str(offset)
	
	status = conn.send_message(request)
	if status.error():
		handle.close()
		return status
	
	response = conn.read_response(server_response)
	if response.error():
		handle.close()
		return response

	if response['Code'] != 100:
		handle.close()
		return wrap_server_error(response)

	if 'Size' not in response['Data']:
		handle.close()
		return RetVal(ErrServerError, "Server gave invalid response: missing Size field")
	
	# This might seem silly at first, but adding the Size field is the client's way of confirming
	# readiness to download the file	
	try:
		sizeToRead = int(response['Data']['Size'])
	except:
		handle.close()
		return RetVal(ErrServerError, "Server gave invalid response: bad Size field")

	if offset > 0:
		handle.seek(offset)
		sizeToRead = sizeToRead - offset	

	request['Data']['Size'] = response['Data']['Size']

	status = conn.send_message(request)
	if status.error():
		handle.close()
		return status

	rawdata = conn.read()
	sizeRead = len(rawdata)
	while rawdata and sizeToRead > 0:
		handle.write(rawdata.encode())
		sizeToRead = sizeToRead - sizeRead
		if sizeToRead == 0:
			break
		rawdata = conn.read()
		sizeRead = len(rawdata)

	handle.close()
	return RetVal().set_value('Size', int(request['Data']['Size']))
Downloads a file from the server. If an offset is given, resuming an interrupted download 
	is possible.
Downloads a file from the server. If an offset is given, resuming an interrupted download
is possible.
Downloads a file from the server . If an offset is given resuming an interrupted download is possible .
def download(conn: ServerConnection, server_path: str, local_path: str, offset=-1) -> RetVal:
	try:
		handle = open(local_path, "wb")
	except Exception as e:
		return RetVal().wrap_exception(e)
	request = {
		'Action' : 'DOWNLOAD',
		'Data' : {
			'Path' : server_path
		}
	}
	if offset > 0:
		request['Data']['Offset'] = str(offset)
	status = conn.send_message(request)
	if status.error():
		handle.close()
		return status
	response = conn.read_response(server_response)
	if response.error():
		handle.close()
		return response
	if response['Code'] != 100:
		handle.close()
		return wrap_server_error(response)
	if 'Size' not in response['Data']:
		handle.close()
		return RetVal(ErrServerError, "Server gave invalid response: missing Size field")
	try:
		sizeToRead = int(response['Data']['Size'])
	except:
		handle.close()
		return RetVal(ErrServerError, "Server gave invalid response: bad Size field")
	if offset > 0:
		handle.seek(offset)
		sizeToRead = sizeToRead - offset	
	request['Data']['Size'] = response['Data']['Size']
	status = conn.send_message(request)
	if status.error():
		handle.close()
		return status
	rawdata = conn.read()
	sizeRead = len(rawdata)
	while rawdata and sizeToRead > 0:
		handle.write(rawdata.encode())
		sizeToRead = sizeToRead - sizeRead
		if sizeToRead == 0:
			break
		rawdata = conn.read()
		sizeRead = len(rawdata)
	handle.close()
	return RetVal().set_value('Size', int(request['Data']['Size']))
def download ( conn : ServerConnection , server_path : str , local_path : str , offset = - 1 ) -> RetVal : try : handle = open ( local_path , "wb" ) except Exception as e : return RetVal ( ) . wrap_exception ( e ) request = { 'Action' : 'DOWNLOAD' , 'Data' : { 'Path' : server_path } } if offset > 0 : request [ 'Data' ] [ 'Offset' ] = str ( offset ) status = conn . send_message ( request ) if status . error ( ) : handle . close ( ) return status response = conn . read_response ( server_response ) if response . error ( ) : handle . close ( ) return response if response [ 'Code' ] != 100 : handle . close ( ) return wrap_server_error ( response ) if 'Size' not in response [ 'Data' ] : handle . close ( ) return RetVal ( ErrServerError , "Server gave invalid response: missing Size field" ) try : sizeToRead = int ( response [ 'Data' ] [ 'Size' ] ) except : handle . close ( ) return RetVal ( ErrServerError , "Server gave invalid response: bad Size field" ) if offset > 0 : handle . seek ( offset ) sizeToRead = sizeToRead - offset request [ 'Data' ] [ 'Size' ] = response [ 'Data' ] [ 'Size' ] status = conn . send_message ( request ) if status . error ( ) : handle . close ( ) return status rawdata = conn . read ( ) sizeRead = len ( rawdata ) while rawdata and sizeToRead > 0 : handle . write ( rawdata . encode ( ) ) sizeToRead = sizeToRead - sizeRead if sizeToRead == 0 : break rawdata = conn . read ( ) sizeRead = len ( rawdata ) handle . close ( ) return RetVal ( ) . set_value ( 'Size' , int ( request [ 'Data' ] [ 'Size' ] ) )
Downloads a file from the server.
Downloads a file from the server .
'''Downloads a file from the server. If an offset is given, resuming an interrupted download 
	is possible.''' # This might seem silly at first, but adding the Size field is the client's way of confirming # readiness to download the file	
conn
ServerConnection
server_path
str
local_path
str
offset