Monday, 25 June 2018

Download several files from a local server to a client

The following codes let me download from server to client three files called tmp.bsp, tmp.seq and tmp.dms. However, just the first file tmp.bsp is completely downloaded. The others are created but stay 0KB and are not filled up with the informations.

client:

import socket 

skClient = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
skClient.connect(("127.0.0.1",2525))

#sFileName = raw_input("Enter Filename to download from server : ")
sData = "Temp"
n = 0 
while True:
    #skClient.send(sFileName)
    sData = skClient.recv(1024)
    print "sData received"
    sData2 = skClient.recv(1024)
    print "sData2 received"
    sData3 = skClient.recv(1024)
    print "sData3 received"
    fDownloadFile = open("tmp.bsp","wb")
    fDownloadFile2 = open("tmp.seq","wb")
    fDownloadFile3 = open("tmp.dms","wb")
    print "All files downloaded"
    while n <100:
        n+=1
        print n
        print "Starting while"
        fDownloadFile.write(sData)
        print "fDownloadFile"          # Working
        sData = skClient.recv(1024)    # Not working anymore
        print "Download tmp.bsp over"
        fDownloadFile2.write(sData2)
        sData2 = skClient.recv(1024)
        print "Download tmp.seq over"
        fDownloadFile3.write(sData3)
        sData3 = skClient.recv(1024)
        print "Download tmp.dms over"
    print "Download over"
    break

skClient.close()

n is a counter and the prints are for debugging. sFileName is to download one file, and used to work but since I want three files I just commented it.

server:

import socket

host = ''
skServer = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
skServer.bind((host,2525))
skServer.listen(10)
print "Server currently active"

while True:
    Content,Address = skServer.accept()
    print Address
    files = "C:\Users\Name_user\Desktop\Networking\Send_Receive/"
    fUploadFile = open(files+str('tmp.bsp'),"rb")
    fUploadFile2 = open(files+str('tmp.seq'),"rb")
    fUploadFile3 = open(files+str('tmp.dms'),"rb")
    sRead = fUploadFile.read(1024)
    sRead2 = fUploadFile2.read(1024)
    sRead3 = fUploadFile3.read(1024)
    while sRead:
        Content.send(sRead)
        sRead = fUploadFile.read(1024)
        print "Sending tmp.bsp is over"
        Content.send(sRead2)
        sRead2 = fUploadFile2.read(1024)
        print "Sending tmp.seq is over"
        Content.send(sRead3)
        sRead3 = fUploadFile3.read(1024)
        print "Sending tmp.dms is over"
    print "Sending is over"



Content.close()
skServer.close()

files I'm using:
server2.py is my server
Execution



from Download several files from a local server to a client

No comments:

Post a Comment