本文整理汇总了Python中utils.Utils.to_kilobytes方法的典型用法代码示例。如果您正苦于以下问题:Python Utils.to_kilobytes方法的具体用法?Python Utils.to_kilobytes怎么用?Python Utils.to_kilobytes使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类utils.Utils
的用法示例。
在下文中一共展示了Utils.to_kilobytes方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_file_with_oob_tcp
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def send_file_with_oob_tcp(s, filename):
sending_file = open(filename, 'rb')
filesize = os.stat(filename).st_size
oob_sent = 0
try:
bytes_sent = int(s.recv(Constants.FILE_CHUNK_SIZE))
print "Already sent {0} / {1}".format(bytes_sent, filesize)
except:
print 'Lost Connection'
return 0
sending_file.seek(int(bytes_sent), 0)
while True:
chunk = sending_file.read(Constants.FILE_CHUNK_SIZE)
if not chunk:
break
try:
s.settimeout(Constants.DEFAULT_TIMEOUT)
s.send(chunk)
except socket.error:
print 'Transfer fail'
return 0
bytes_sent += Constants.FILE_CHUNK_SIZE
percent = int(float(bytes_sent) * 100 / float(filesize))
print "{0} / {1} Kb sent ({2}%)".format(Utils.to_kilobytes(bytes_sent),
Utils.to_kilobytes(filesize), percent)
sys.stdout.write('\033M')
if (percent % 10 == 0) & (oob_sent != percent) & (percent < 91):
oob_sent = percent
sys.stdout.write('\033D')
print '\033[37;1;41m Urgent flag sent at {0}% \033[0m'.format(percent)
s.send(b'{}'.format(percent / 10), socket.MSG_OOB)
sending_file.close()
示例2: send_file_multicast
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def send_file_multicast(s, filename):
connections = {}
filesize = os.stat(filename).st_size
try:
while True:
readable, _, _ = select.select([s], [], [])
for rd in readable:
bytes_sent = 0
package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
unpacked_package = Utils.unpack_package(package)
if not connections.has_key(client_address) or connections[client_address] is None:
connections[client_address] = open(filename, 'rb')
if unpacked_package['command'] == Constants.INIT_TRANSMIT:
bytes_sent = int(unpacked_package['payload'])
connections[client_address].seek(bytes_sent)
data = connections[client_address].read(Constants.FILE_CHUNK_SIZE)
if not data:
rd.sendto(Utils.pack_package(Constants.FIN, ''), client_address)
connections[client_address].close()
connections[client_address] = None
else:
rd.sendto(Utils.pack_package(Constants.ACK, data), client_address)
bytes_sent += len(data)
percent = int(float(bytes_sent) * 100 / float(filesize))
print "{0} / {1} Kb sent to client {2}({3}%)".format(Utils.to_kilobytes(bytes_sent),
Utils.to_kilobytes(filesize), client_address,
percent)
sys.stdout.write('\033M')
except socket.error, value:
print value
示例3: send_file_udp
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def send_file_udp(s, filename):
bytes_sent = 0
sending_file = open(filename, 'rb')
filesize = os.stat(filename).st_size
while True:
package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
unpacked_package = Utils.unpack_package(package)
if unpacked_package['command'] == Constants.INIT_TRANSMIT:
bytes_sent = int(unpacked_package['payload'])
sending_file.seek(bytes_sent)
data = sending_file.read(Constants.FILE_CHUNK_SIZE)
if not data:
s.sendto(Utils.pack_package(Constants.FIN, ''), client_address)
sending_file.close()
break
else:
s.sendto(Utils.pack_package(Constants.ACK, data), client_address)
package, client_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
unpacked_package = Utils.unpack_package(package)
if unpacked_package['command'] == Constants.ACK:
bytes_sent += len(data)
percent = int(float(bytes_sent) * 100 / float(filesize))
print "{0} / {1} Kb sent ({2}%)".format(Utils.to_kilobytes(bytes_sent),
Utils.to_kilobytes(filesize), percent)
示例4: receive_file_tcp
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def receive_file_tcp(s, filename):
receiving_file = open(filename, 'ab')
bytes_received = os.path.getsize(filename)
s.settimeout(Constants.DEFAULT_TIMEOUT)
server_address = s.getpeername()
print "Already received {0} bytes".format(bytes_received)
try:
s.send(str(bytes_received))
except Exception:
print Exception.message
return
while True:
response = s.recv(Constants.FILE_CHUNK_SIZE)
if not response:
print '\nFile received'
receiving_file.close()
break
else:
receiving_file.write(response)
bytes_received += len(response)
print "Received {0} Kb from {1}".format(Utils.to_kilobytes(bytes_received), server_address)
sys.stdout.write('\033M')
s.close()
示例5: receive_file_with_oob_tcp
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def receive_file_with_oob_tcp(s, filename):
receiving_file = open(filename, 'ab')
bytes_received = os.path.getsize(filename)
s.settimeout(Constants.DEFAULT_TIMEOUT)
print "Already received {0}".format(bytes_received)
try:
s.send(str(bytes_received))
except:
print 'Lost Connection'
return 0
while True:
try:
oob = s.recv(2, socket.MSG_OOB)
except socket.error, value:
oob = None
if oob:
print '\033[0;32m OOB data: {0} Kb ({1}0%) received \033[0m' \
.format(Utils.to_kilobytes(bytes_received), oob)
else:
chunk = s.recv(Constants.FILE_CHUNK_SIZE)
bytes_received += Constants.FILE_CHUNK_SIZE
receiving_file.write(chunk)
if not chunk:
break
示例6: receive_file_multicast
# 需要导入模块: from utils import Utils [as 别名]
# 或者: from utils.Utils import to_kilobytes [as 别名]
def receive_file_multicast(s, filename, host, port):
receiving_file = open(filename, 'ab')
bytes_received = os.path.getsize(filename)
s.settimeout(Constants.DEFAULT_TIMEOUT)
while True:
s.sendto(Utils.pack_package(Constants.INIT_TRANSMIT, str(bytes_received)), (host, port))
response, server_address = s.recvfrom(Constants.FILE_CHUNK_SIZE)
unpacked_response = Utils.unpack_package(response)
if not unpacked_response['command'] == Constants.FIN:
receiving_file.write(unpacked_response['payload'])
else:
print '\nFile received'
receiving_file.close()
break
bytes_received += len(unpacked_response['payload'])
print "Received {0} Kb from {1}".format(Utils.to_kilobytes(bytes_received), server_address)
sys.stdout.write('\033M')
s.close()