0

I have made a simple server -client code in python socket programming. Client side takes the screenshot(image) of its side and send it to server. It works fine when i am transferring image using 'localhost' i.e from one folder to other.....but the main problem comes when image is transferred to another computer...the received image on server side is corrupted...further more i have observed that the difference between client(sender side image i.e non corrupted one) and server(receivers side image i.e corrupted one ) is almost 1Kb every time.........

my code for client (sender) side is--

os.system('scrot screen.bmp') #command to take screen shot


FILE = "screen.bmp"
f = open(FILE, "rb")
data = f.read()
f.close()
del f

imagesize = int(os.path.getsize('screen.bmp'))

sendsize =  '%1024s' %imagesize
s.sendall(str(sendsize))
print 'length of data = ',len(data)
s.sendall(str(len(data)))
s.sendall(str(data))

and server side(receiver side) ---

filename='screen.bmp'
print '[Media] Starting media transfer for ',filename   
os.system('rm -f screen.bmp')
f = open(filename,"wb")
expsizeimage = int(conn.recv(1024))
data1 = conn.recv(1024)

data2=''
for i in range(0,len(data1)):
    if(not(data1[i]=='0' or data1[i]=='1' or data1[i]=='2' or data1[i]=='3' or data1[i]=='4' or data1[i]=='5' or data1[i]=='6' or data1[i]=='7' or data1[i]=='8' or data1[i]=='9')):
        break
    data2=data2+data1[i]
print '------------'+data2+'-------------'+str(m)+'----------------'
print 'size of data:' ,int(data2)
print 'the expected size of image is: ', expsizeimage
data=9
del data
sized=0;
while 1:

    data = conn.recv(expsizeimage)
    print 'received length of image = ',len(data)               
    f.write(data)

    sized=sized+len(data)
    print "sized------"+str(sized)
    del data
    if(sized>=int(data2)):

        break

print "saved the screentshot data recieved"
4

1 回答 1

0

These two programs are untested, were written for the latest version of Python, and switch your client/server relationship for security purposes.


Source.py

import os, struct, socket

def main():
    # Take screenshot and load the data.
    os.system('scrot image.bmp')
    with open('image.bmp', 'rb') as file:
        data = file.read()
    # Construct message with data size.
    size = struct.pack('!I', len(data))
    message = size + data
    # Open up a server socket.
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.bind(('', 65000))
    server.listen(5)
    # Constantly server incoming clients.
    while True:
        client, address = server.accept()
        print('Sending data to:', address)
        # Send the data and shutdown properly.
        client.sendall(message)
        client.shutdown(socket.SHUT_RDWR)
        client.close()

if __name__ == '__main__':
    main()

Destination.py

import socket, struct

def main(host):
    # Connect to server and get image size.
    client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    client.connect((host, 65000))
    packed = recvall(client, struct.calcsize('!I'))
    # Decode the size and get the image data.
    size = struct.unpack('!I', packed)[0]
    print('Receiving data from:', host)
    data = recvall(client, size)
    # Shutdown the socket and create the image file.
    client.shutdown(socket.SHUT_RDWR)
    client.close()
    with open('image.bmp', 'wb') as file:
        file.write(data)

def recvall(sock, size):
    message = bytearray()
    # Loop until all expected data is received.
    while len(message) < size:
        buffer = sock.recv(size - len(message))
        if not buffer:
            # End of stream was found when unexpected.
            raise EOFError('Could not receive all expected data!')
        message.extend(buffer)
    return bytes(message)

if __name__ == '__main__':
    main('localhost')
于 2012-05-15T17:34:38.387 回答