Untitled

 avatar
unknown
plain_text
3 years ago
1.8 kB
3
Indexable
# encoding: utf-8
# Revisión 2019 (a Python 3 y base64): Pablo Ventura
# Copyright 2014 Carlos Bederián
# $Id: connection.py 455 2011-05-01 00:32:09Z carlos $

import os
import socket
import datatime
from constants import *
from base64 import b64encode


class Connection(object):
    """
    Conexión punto a punto entre el servidor y un cliente.
    Se encarga de satisfacer los pedidos del cliente hasta
    que termina la conexión.
    """

    def __init__(self, socket, directory):
        # FALTA: Inicializar atributos de Connection
        self.s = socket
        self.dir = directory
        dir = os.path("directory")
        if not os.path.exists(dir):
            os.mkdir(dir)

    def handle(self, data):
        # FALTA: Manejar la conexión entrante
        # FALTA: Leer y procesar los pedidos del cliente
        # FALTA: Enviar respuestas al cliente
        # FALTA: Cerrar la conexión
        x = datatime.now()
        # Procesa el pedido del cliente
        request_str = request.decode("uft-8")
        #Parseamos
        request_str = request_str.split(" ")
        #Obtenemos el comando

        command = request_str[0]
        if(command == "get_file_listing"):
            get_file_listing()
        elif(command == "get_metadata"):
            get_metadata(FILENAME)
        elif(command == "get_slice"):
            get_slice(FILENAME, OFFSET, SIZE)
        elif(command == "quit"):
            quit()
        else:
            print("Invalid command")

        # Obtiene el contenido del archivo del pedido
        try:
            file = open("directory/" + srt(x) + "txt", "w")
            file.write(request_str)
            file.close()
        except:
            print("Error al crear el archivo")
Editor is loading...