2

我正在做一个项目,其中图像由我的 android 手机拍摄并存储在我的 SD 卡中的文件夹中。我正在编写一个 python 脚本,该脚本需要定期将文件夹从 SD 移动到我 PC 中的特定文件夹。手机和电脑通过移动热点连接。

我写了一个套接字程序,我的 PC 作为客户端,移动设备作为服务器。但我面临一些问题。虽然我无法移动文件夹,但我尝试从文件夹中移动图像,但我面临以下问题

  • 图像以未知文件格式的形式复制。
  • 我无法在服务器端迭代该过程以移动文件夹中存在的所有图像
  • 在客户端,我无法将其存储在我想要的位置。我尝试在发送图像之前从服务器发送文件夹名称和文件名,但客户端没有使用我发送的文件名,而是搜索该名称的文件夹。
  • 我也对发送到客户端的名称大小有疑问,如何根据从服务器发送的名称随机更改客户端的大小。

我需要有人帮助解决这个问题。

这是客户端代码

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)

我的服务器端代码

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()
4

2 回答 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.
于 2013-02-09T12:08:04.570 回答
-1

我已经修改了足够多的代码来解决我的许多问题,现在我想要解决的唯一问题是图像传输。我在客户端打开了一个 .jpg 文件并将数据写入其中。但最终文件大小仅比原始大小小 1kb。我想如果我解决这个问题,我的工作就会完成。有人可以帮我吗。

这是代码

服务器:

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()

客户:

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)
于 2013-01-30T19:30:13.917 回答