I am creating a client server program on which client downloads server's files. it works fine in localhost, but the problem occurs in except localhost connection. I've tried googling but none of the solutions(from previously suggested to the same problem) works.

Here is my code:

def upload(sock): # server.py
    filename = str(sock.recv(4096)).split(' end')[0]
    if os.path.exists(filename):
        sock.send('YES')
        sock.send(str(os.path.getsize(filename)) + ' end')
        foo = open(filename, 'rb')
        upbytes = foo.read(4096)
        sock.sendall(upbytes)
        while upbytes != '':
            upbytes = foo.read(4096)
            sock.sendall(upbytes)
        foo.close()
        print "\tUpload Complete !"
    else:
        sock.send('NO')

def download(sock, filename): #client.py
    sock.send(filename + ' end')
    if sock.recv(4096) == 'YES':
        filesize = int(str(sock.recv(4096)).split(' end')[0])
        print filesize, 'filesize'
        foo = open('downloaded_' + os.path.basename(filename), 'wb')
        downbytes = sock.recv(4096)
        foo.write(downbytes)
        downlen = len(downbytes)
        while downlen < filesize:
            downbytes = sock.recv(4096)
            if not downbytes:
                break
            foo.write(downbytes)
            downlen += len(downbytes)
        foo.close()
        print 'filesize', filesize, 'downlen', downlen
        print "\t\tDownload Complete !\n"
    else:
        print "\t\tFile not found ! <" + filename + '>'

I've tried different solutions but none of them worked!

I was going to look at this but the code looks incomplete. Or I just missed where the socket is opened. I wanted to see if you used TCP or UDP. If UDP your code won't handle a lost packet so there's that. A lot more code on both end is needed to make a reliable transfer.

So https://stackoverflow.com/questions/19727606/how-to-differentiate-tcp-udp-when-programming-sockets for reference so you can learn why I asked about TCP or UDP.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.