Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7946261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:05:59+00:00 2026-06-04T01:05:59+00:00

I have made a simple server -client code in python socket programming. Client side

  • 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"
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T01:06:00+00:00Added an answer on June 4, 2026 at 1:06 am

    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')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have made a simple html page with an image on it to display
I have started to write a Python 3.x client application. The server application exists
I have assignment when I need to write simple time server and a client
I have a simple class that handles the connection being made between a client
I have made a simple table form here in jsfiddle . I also put
I have made a simple test application for the issue, two winforms each containing
I have made a simple web-page including a couple of static pages, a css
I have made a simple web form in Google app engine where I have
i have made a simple php contact form following this tutorial: http://www.catswhocode.com/blog/how-to-create-a-built-in-contact-form-for-your-wordpress-theme The big
I have made my own file type (.ddd) and I made a simple program

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.