Exemplo n.º 1
0
    def run(self):
        fs = framedSocket(self.conn)
        contents = (fs.receiveMessage())
        filename = contents
        canTransfer = self.checkTransfer(filename)

        if (canTransfer):
            if (os.path.isFile(filename)):
                fs.sendMessage(b"NO")
                conn.shutdown(socket.SHUT_WR)
            else:
                fs.sendMessage(b"OK")
        else:
            os.write(2, ("File in Transfer").encode())
            conn.shutdown(socket.SHUT_WR)

        fd = os.open(filename, os.O_CREAT | os.O_WRONLY)
        os.write(fd, fs.receiveMessage().encode())
        os.close(fd)
        self.endTransfer(filename)

        self.conn.shutdown(socket.SHUT_WR)
        s = None
        continue
    break

if s is None: # failed 
    print("could not open socket")
    sys.exit(1)

delay = float(paramMap['delay'])
if delay != 0:
    print(f"sleeping for {delays}s")
    time.sleep(delay)
    print("done sleeping")

    
fs = framedSocket(s)

input = my_getLine()
command,localfile,remotefile = parseTCPInput(input)

fs.sendMessage(remotefile.encode()) # send filename 

reply = fs.receiveMessage() # get server's response 

if reply == "NO": # File can't be transferred 
    os.write(2,("Failed").encode())
    sys.exit(1)
        
else: # "OK"
    fd = os.open(localfile, os.O_RDONLY)
    buffer = ""
Exemplo n.º 3
0
paramMap = paramMap = params.parseParams(switchesVarDefaults)

listenPort = paramMap['listenPort']
listenAddr = ''  # all available interfaces

if paramMap['usage']:
    params.usage()

s = socket.socket(socket.AF_INET,
                  socket.SOCK_STREAM)  # create new socket, stream = 2 way
s.bind((listenAddr, listenPort))  # attach socket to fd,      local host 50001
s.listen(1)  # listen for new connections

while True:  # For every connection
    conn, addr = s.accept()
    fs = framedSocket(conn)

    if os.fork() == 0:  # Fork off a child to become server
        contents = (fs.receiveMessage())  # get command from client

        filename = contents

        if os.path.isfile(
                filename):  # Check if there is already a file named filename
            fs.sendMessage(b"NO")
            conn.shutdown(socket.SHUT_WR)
        else:
            fs.sendMessage(b"OK")

        fd = os.open(filename, os.O_CREAT
                     | os.O_WRONLY)  # open the file, so it can be written to