Python Forum
Copy data from 1 blk device in machine A to another blk device in machine B
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Copy data from 1 blk device in machine A to another blk device in machine B
#3
An implementation which uses a buffer and memoryview:

import socket


host = '127.0.0.1'
port = 1337

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# use TCP for data transmission
sock.connect((host, port))
buffer = bytearray(1024**2) # 1MiB
view = memoryview(buffer)
with open(file, 'rb') as fd:
    while True:
        readlen = fd.readinto(buffer)
        # buffer is reused
        if not readlen:
            break
        sock.send(view[:readlen]
        # sends read data from memoryview to socket
    sock.close()
The buffer is reused for the chunks. The method readinto of a fileobject, puts the data into the buffer and returns the amount of data, which has been written to the buffer. The memoryview is just a method to have fast access on slices.
The socket object has also the function sendfile which takes a fileobject. Maybe this is the fastest implementation. I never tried it.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
RE: Copy data from 1 blk device in machine A to another blk device in machine B - by DeaD_EyE - Feb-01-2018, 11:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  sending packet onto dummy network device but receiving echo sabuzaki 2 1,492 Feb-12-2023, 10:31 AM
Last Post: Vadanane
  Netmiko Program ssh from the Jumpssh ( host -> Jumphost -> Network Device matya0403 3 6,887 Jul-31-2020, 08:22 AM
Last Post: dtw
  Connect to device without paramiko/netmiko modules kang18 3 3,076 Jun-05-2020, 10:54 AM
Last Post: Larz60+
  telnet to a device under tacacs management kang18 0 1,593 Jun-05-2020, 06:11 AM
Last Post: kang18
  [Socket] Is there a way to recognize a client by their device? SheeppOSU 1 1,957 Jun-03-2020, 08:37 AM
Last Post: Knight18
  Machine actively refused it. External IP only ramboahoe 1 3,442 Mar-28-2020, 12:46 AM
Last Post: ramboahoe
  How to access shared folder on Windows from Ubuntu machine Pavel_47 22 13,852 Nov-09-2019, 04:06 PM
Last Post: Pavel_47
  Trying to connect to a server running on another machine PythonForever 1 2,354 Jun-07-2019, 02:16 PM
Last Post: heiner55
  Issue: Script from jumpserver to another server to target device? searching1 0 2,108 May-29-2019, 03:43 AM
Last Post: searching1
  How to run local python script to remote machine without sending krishna1989 1 8,371 Feb-21-2019, 05:18 PM
Last Post: marienbad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020