11

I am trying to send an image file using socket programming in python. I am able to send a text file. But I have been trying to send an image file, by opening it and reading the contents of the image file in a string and then sending the string and receiving it on the client side and then writing it to a file with the same name in a different directory, but I am unable to open the file. Also I am posting my code below, the commented parts are an indication that I have already tried it. Also I am having problems sending the integer size but I am receiving some random string on the other side.

This is the Server script

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)
import os


client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"
while (1):
    choice = client_socket.recv(1024)
    choice = int(choice)
    if(choice == 1):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        size = os.path.getsize(data)
        size = str(size)
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

    if (choice == 2):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        #strng = str(fp)
        size = os.path.getsize(data)
        print size
        size = str(size)
        print size
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

And this is the client side script -

#!/usr/bin/python
# TCP client example
import socket,os
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("", 5000))
k = ' '
size = 1024

while(1):
    print "Do you want to transfer a \n1.Text File\n2.Image\n3.Video\n"
    k = raw_input()
    client_socket.send(k)
    k = int (k)
    if(k == 1):
        print "Enter file name\n"
        strng = raw_input()
        client_socket.send(strng)
        size = client_socket.recv(1024)
        size = int(size)
        print "The file size is - ",size," bytes"
        size = size*2
        strng = client_socket.recv(size)
        print "\nThe contents of that file - "
        print strng

    if (k==2):
        print "Enter file name of the image with extentsion (example: filename.jpg,filename.png) - "
        fname = raw_input()
        client_socket.send(fname)
        size = client_socket.recv(1)
        print size
        #size = int(size)
        print "The file size is - ",size
        size = size*2
        strng = client_socket.recv(256456)
        print "\nThe file will be saved and opened- "
        fname = 'downloads/'+fname
        nf = open(fname,'w')
        nf.write(strng)
        nf.close()
        fname = 'viewnior '+ fname
        print fname
        os.system(fname)

I am programming on Crunchbang Linux - Unofficial Debian based Distro viewnior is the image viewer.

mihirk
  • 299
  • 1
  • 5
  • 10
  • 1
    Have you tried to debug it? `print repr(client)`? – Glaslos Jan 24 '12 at 23:58
  • 1
    Will try that thank you. I tried vimdiff between the new created file and the original one and it turned out that the files are identical still the newly created image won't open. The size of the original image and the cloned image is same and so are the contents. – mihirk Jan 25 '12 at 00:01

2 Answers2

9

Thank you Sanket. :D I had a problem sending data in huge chunks so I split it into 512 bytes and now it works perfectly even for videos. :D Just need to add the pygtk interface for confirmation for receiving files and sending them :D

#!/usr/bin/python
# TCP client example
import socket,os
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("", 5005))
k = ' '
size = 1024

while(1):
    print "Do you want to transfer a \n1.Text File\n2.Image\n3.Video\n"
    k = raw_input()
    client_socket.send(k)
    k = int (k)
    if(k == 1):
        print "Enter file name\n"
        strng = raw_input()
        client_socket.send(strng)
        size = client_socket.recv(1024)
        size = int(size)
        print "The file size is - ",size," bytes"
        size = size*2
        strng = client_socket.recv(size)
        print "\nThe contents of that file - "
        print strng

    if (k==2 or k==3):
        print "Enter file name of the image with extentsion (example: filename.jpg,filename.png or if a video file then filename.mpg etc) - "
        fname = raw_input()
        client_socket.send(fname)
        fname = 'documents/'+fname
        fp = open(fname,'w')
        while True:
            strng = client_socket.recv(512)
            if not strng:
                break
            fp.write(strng)
        fp.close()
        print "Data Received successfully"
        exit()
        #data = 'viewnior '+fname
        #os.system(data)

And here is the server code

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5005))
server_socket.listen(5)
import os


client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"
while (1):
    choice = client_socket.recv(1024)
    choice = int(choice)
    if(choice == 1):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        size = os.path.getsize(data)
        size = str(size)
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

    if (choice == 2 or choice == 3):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        img = open(data,'r')
        while True:
            strng = img.readline(512)
            if not strng:
                break
            client_socket.send(strng)
        img.close()
        print "Data sent successfully"
        exit()
        #data = 'viewnior '+data
        #os.system(data)
mihirk
  • 299
  • 1
  • 5
  • 10
  • First thanks for the helpful post. But I am having a problem. When running both scripts sending an image it prints `Data sent successfully` and stops, although the received image should be used latter in the code. I am using it like this `input_image = Image.open("data").convert('L').resize((100, 100))` – user2229953 Jun 06 '13 at 22:57
  • 2
    I was unable to run this on windows. Changing ```open(data,'r')``` to ```open(data,'rb')``` in both files and replacing ```readline``` with ```read``` to transfer data in binary solved my problem – SkyNT Jan 23 '17 at 04:52
8

I ran the same code on my system (Ubuntu 11.10) and I found that, there is a problem with sending size as a string. When I inserted logic to handle that part, it ran smoothly. I could open the file also. This is how I could solve your problem : 1st change in client code(client.py) is while accepting size and sending acknowledgement about it :-

size = ' '
while(1):
   tmpsize = client_socket.recv(1)
   if tmpsize.isdigit() == True:
      print "Here : ",tmpsize
      size += tmpsize
   else:
      break

client_socket.send("received")

2nd change is in server side(server.py) code, to accept acknowledgement :-

client_socket.send(size)
ack = client_socket.recv(1024)
if ack == "received":
   client_socket.send (strng)

I hope this will help you to solve your problem.

curiousMind
  • 155
  • 3
  • 10
  • 1
    Thank you sanket. I worked out the problem. But it turns out I was having problem sending big files so I decided to send them line by line. Here is the revised code :D – mihirk Jan 26 '12 at 00:28