Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File transfering pretty #3

Open
wants to merge 2 commits into
base: file_transfering
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 103 additions & 0 deletions daiquiri/server/server_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# =============================================================================
# IMPORTS
# =============================================================================
import abc
import os

# =============================================================================
# MODULE CLASSES
# =============================================================================

class ServerAgentBase(abc.ABC):
""" Base class for server agent.

Methods
-------
upload
download
"""
def __init__(self, *args, **kwargs):
super(UserAgentBase, self).__init__()

@abc.abstractmethod
def receive(self, *args, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def send(self, *args, **kwargs):
raise NotImplementedError

class SocketServerAgent(ServerAgentBase):
""" User agent with `socket` package.

"""
def __init__(self, sock):
super(SocketServerAgent, self).__init__()

# local imports
import socket
self.sock = sock

def send(self, path, buffer_size=1024):
""" Upload file using socket.

"""

# send size
sock.send('size' + str(
os.path.getsize(path)))

# send name
sock.send(
os.path.basename(path))

with open(path, 'rb') as f_handle:
# get data
data = f.read(buffer_size)

# send data
self.sock.send(data)

# TODO:
# shall we use len() == 0 or equivalent
# since there could be various encodings?
# not sure
while data != '':
data = f_handle.read(buffer_size)
self.send(data)

self.sock.close()

def receive(self, path, buffer_size=1024):
""" Download file using socket.

"""
# get size
# TODO: is this hard-coded `4` right?
size = int(self.sock.recv(buffer_size)[4:])

# TODO:
# this is not consistent with your original implemtation,
# the file name argument should be speicifed, right?
with open(path, 'wb') as f_handle:
# initial receive
data = sock.recv(buffer_size)

# get total receive
total_recv = len(data)

# write to file
f_handle.write(data)

while total_recv < size:
data = sock.recv(buffer_size)
total_recv += len(data)
f_handle.write(data)








92 changes: 92 additions & 0 deletions daiquiri/submitter/user_agent.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
# =============================================================================
# IMPORTS
# =============================================================================
import abc
import os

# =============================================================================
# MODULE CLASSES
# =============================================================================

class SubmitterAgentBase(abc.ABC):
""" Base class for submitter agent.

Methods
-------
upload
download
"""
def __init__(self, *args, **kwargs):
super(UserAgentBase, self).__init__()

@abc.abstractmethod
def upload(self, *args, **kwargs):
raise NotImplementedError

@abc.abstractmethod
def download(self, *args, **kwargs):
raise NotImplementedError

class SocketSubmitterAgent(SubmitterAgentBase):
""" User agent with `socket` package.

"""
def __init__(self, sock):
super(SocketSubmitterAgent, self).__init__()

# local imports
import socket
self.sock = sock

def upload(self, path, buffer_size=1024):
""" Upload file using socket.

"""
assert os.path.isfile(path), 'Path does not exist.'

# send size
sock.send('size' + str(
os.path.getsize(path)))

with open(path, 'rb') as f_handle:
# get data
data = f.read(buffer_size)

# send data
self.sock.send(data)

self.sock.close()

def download(self, path, buffer_size=1024):
""" Download file using socket.

"""
# get size
# TODO: is this hard-coded `4` right?
size = int(self.sock.recv(buffer_size)[4:])

# TODO:
# this is not consistent with your original implemtation,
# the file name argument should be speicifed, right?
with open(path, 'wb') as f_handle:
# initial receive
data = sock.recv(buffer_size)

# get total receive
total_recv = len(data)

# write to file
f_handle.write(data)

while total_recv < size:
data = sock.recv(buffer_size)
total_recv += len(data)
f_handle.write(data)