Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/362.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python套接字编程将图像文件从服务器传输到客户端_Python_Sockets - Fatal编程技术网

使用python套接字编程将图像文件从服务器传输到客户端

使用python套接字编程将图像文件从服务器传输到客户端,python,sockets,Python,Sockets,我正在做一个项目,图像由我的android手机拍摄并存储在SD卡的文件夹中。我正在编写一个python脚本,需要定期将文件夹从SD移动到电脑中的特定文件夹。手机和电脑通过移动热点连接 我写了一个socket程序,用我的PC作为客户端,用手机作为服务器。但我面临着一些问题。虽然我无法移动文件夹,但我尝试从文件夹中移动图像,我面临以下问题 图像以未知文件格式复制 我无法在服务器端重复移动文件夹中所有图像的过程 在客户端,我无法将其存储在我想要的位置。在发送图像之前,我尝试从服务器发送文件夹名和文件

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

我写了一个socket程序,用我的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()
我的服务器端代码

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

我已经修改了足够的代码来解决我的许多问题,现在我唯一想解决的问题是图像传输。我在客户端打开了a.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)

问题似乎是在服务器端的二进制文件上使用
readline()

file_name = open(imgs,'rb')
while True:
strng = file_name.readline()
readline()
从文件中读取数据,直到下一个
'\n'
字符。在二进制文件上使用它可能会导致读取很长的缓冲区!(甚至可能达到EOF)。在这种情况下,使用
socket.send()
可能无法传递整个数据,应检查返回值(=传输的字节)。修复的可能性是:

  • 发送时使用
    socket.sendall()
    将发送整个缓冲区 或者,或者(可以同时使用两者)

  • 使用
    file\u name.read(1024)
    -这将限制每个周期读取的数据量

  • 尝试使用
    'wb'
    'rb'
    打开,这样就不会更改格式。提到了一些你应该考虑的陷阱。你可以用魔术库来猜测图像文件格式(使用底层UNIX文件命令LIB)。我也建议你把问题分解成许多独立的子问题,因为它很难回答现在的问题。这就是它的用途。图像不是文本文件,也不包含行。不要在它们上使用
    readline()
    。这是你问题的延伸,不是答案。