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()
Rad
  • 62
  • 1
  • 1
  • 8
  • Try opening with `'wb'` and `'rb'` so it doesn't change the format. – f p Jan 29 '13 at 13:59
  • [Socket Programming HOWTO on docs.python.org](http://docs.python.org/2/howto/sockets.html) mentions a few pitfalls you should consider. – Janne Karila Jan 29 '13 at 14:11
  • You can guess the image file format using Magic library http://pypi.python.org/pypi/python-magic/ (uses underlying UNIX file command lib) – Mikko Ohtamaa Jan 29 '13 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 – Mikko Ohtamaa Jan 29 '13 at 14:17
  • Try using [FTP](http://docs.python.org/2/library/ftplib.html#module-ftplib). This is what it's for. – f p Jan 29 '13 at 14:21
  • well I dont know much about FTP but the point is can i run this FTP server along with another http server thats running in the mobile for another android application i created? and how can i transfer folders using FTP – Rad Jan 29 '13 at 15:27
  • I would like to proceed with this socket porgramming only but i am having a problem in moving the image files some one can please help me with an example. – Rad Jan 29 '13 at 18:45
  • @mihirk can you help me with this I used a lot of help from one of your questions. http://stackoverflow.com/questions/8994937/send-image-using-socket-programming-python. – Rad Jan 30 '13 at 15:32

2 Answers2

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.
Brad Solomon
  • 38,521
  • 31
  • 149
  • 235
sonatanos
  • 11
  • 3
-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)
Rad
  • 62
  • 1
  • 1
  • 8
  • 1
    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. – user207421 Apr 13 '19 at 02:26