2

I am working on a project where images are taken by my android phone and are stored in folders in my SD card. I am working on a python script that needs to periodically move the folders from the SD to a particular folder in my PC. The phone and the PC are connected over the mobile Hotspot.

I wrote a socket program with my PC as client and the mobile as server. But I am facing some problems with it. Though I could not move folders i tried moving images from the folder and i am facing the following problems

  • the image is copied in the form of an unknown file format.
  • i am unable to iterate the process at the server side to move all the images present in the folder
  • at the client I am not able to store it in the location i want. I try to send the folder name and the file name from the server before sending the image but the client is not taking that file name i sent, instead it searches a folder in that name.
  • I also have a problem with the size of the names sent to the client, how do i randomly change the size at the client side depending on the name sent from the server.

I need someones help to sort this problem.

Here is the client side code

import socket,os
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("192.168.43.1", 5005))

size = 1024

while True:
    fln = client_socket.recv(size) # folder name
    fn = client_socket.recv(size) # file name
    fname = "E:\\Transfered\\"+fln+"\\"+fn
    fp = open(fname,'w')
    while True:
        strng = client_socket.recv(1024)
        if not strng:
            break
        fp.write(strng)
    fp.close()
    print "Data Received successfully"
    exit()
    #data = 'viewnior '+fname
    #os.system(data)

My Server side code

import os   
import sys,time 
import socket

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

client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"

sb = '/mnt/sdcard/sb'

while True:
    files = os.listdir(sb)
    pages = 0;
    while (files):
    print '\nMaybe, pending work'
    for au in files:
        if (au.find('d')>-1): # searching for folder with a d
            os.chdir(sb+'/'+au)
            imgFiles = os.listdir(sb+'/'+au)
            images = [img for img in imgFiles if img.endswith('.jpg')]
            print '\n%s user done' %au
            client_socket.send(au)
            pages = 0;
            #copies all .img files in the folder from server to client
            for imgs in images:
                print imgs
                client_socket.send(imgs)
                file_name = open(imgs,'r')
                while True:
                    strng = file_name.readline(1024)
                    if not strng:
                        break
                    client_socket.send(strng)
                file_name.close()
                print "Data sent successfully"                      
                os.remove(sb+'/'+au+'/'+imgs)
                pages = pages + 1

            time.sleep(1)
            os.chdir(sb)
            os.rmdir(au)

        else:
            time.sleep(2) 
        exit()
8
  • Try opening with 'wb' and 'rb' so it doesn't change the format. Commented Jan 29, 2013 at 13:59
  • Socket Programming HOWTO on docs.python.org mentions a few pitfalls you should consider. Commented Jan 29, 2013 at 14:11
  • You can guess the image file format using Magic library pypi.python.org/pypi/python-magic (uses underlying UNIX file command lib) Commented Jan 29, 2013 at 14:16
  • I also suggest that you break your questions to many independent subquestions because it is difficult to answer as its now hairball of issues Commented Jan 29, 2013 at 14:17
  • Try using FTP. This is what it's for. Commented Jan 29, 2013 at 14:21

2 Answers 2

1

The problem seems to be using readline() on a binary file at the server side:

file_name = open(imgs,'rb')
while True:
strng = file_name.readline()

readline() reads data from file up to the next '\n' character. Using it on a binary file may result in reading a very long buffer! (Maybe even up to EOF). In that case, using socket.send() may fail to deliver the entire data, and the return value (=bytes transmitted) should be checked. The possibilities for fixing that is:

  1. using socket.sendall() when sending, will send the entire buffer.

or, alternatively (may use both)

  1. using file_name.read(1024) - which will bound the amount of data read each cycle.
Sign up to request clarification or add additional context in comments.

Comments

-1

I have modified the code enough to solve many of my problems now the only problem i want to solve is the image transfer. I opened the a .jpg file at the client and wrote the data into it. But the final file size is just 1kb less that the original size. I guess my work will be done if I sort that out. Can some one help me with it.

heres the code

server:

import os   
import sys,time 
import socket

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

client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"

sb = '/mnt/sdcard/sb'

while True:
    files = os.listdir(sb)
    pages = 0;
    while (files):
        print '\nMaybe, pending work'
        for au in files:
            if (au.find('d')>-1):
                os.chdir(sb+'/'+au)
                imgFiles = os.listdir(sb+'/'+au)
                images = [img for img in imgFiles if img.endswith('.jpg')]
                print '\n%s user done' %au
                client_socket.send(au)

                #copies all .img files in the folder from server to client
                for imgs in images:
                    client_socket.send(imgs)
                    file_name = open(imgs,'rb')
                    while True:
                        strng = file_name.readline()
                        if not strng:
                            break
                        client_socket.send(strng)
                    file_name.close()
                    os.remove(sb+'/'+au+'/'+imgs)       
                print "Data sent successfully"                          
                time.sleep(1)
                os.chdir(sb)
                os.rmdir(au)

            else:
                time.sleep(2) 
            exit()

Client:

import socket,os
import time

client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("192.168.43.1", 5005))

dst="E:\\Kiosk\\"

while True:
#folder name
fln = client_socket.recv(4)
os.chdir(dst);
dst = "E:\\Kiosk\\"+fln+"\\"
if not os.path.exists(dst): os.makedirs(dst)
fname = client_socket.recv(4)
os.chdir(dst)
fname = fname+'.jpg'
fp = open(fname,'wb')
# image
while True:
    strng = client_socket.recv(1024)
    if not strng:
        break
    fp.write(strng)
fp.close()
print "Data Received successfully"
exit()
#time.sleep(10)

#data = 'viewnior '+fname
#os.system(data)

1 Comment

Images are not text files and do not contain lines. Don't use readline() on them. This is an extension to your question, not an answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.