简体   繁体   English

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

[英]Transferring image files from sever to client using python socket programming

I am working on a project where images are taken by my android phone and are stored in folders in my SD card. 我正在研究一个项目,其中图像由我的Android手机拍摄并存储在SD卡的文件夹中。 I am working on a python script that needs to periodically move the folders from the SD to a particular folder in my PC. 我正在处理一个python脚本,该脚本需要定期将文件夹从SD移到PC中的特定文件夹。 The phone and the PC are connected over the mobile Hotspot. 手机和PC通过移动热点连接。

I wrote a socket program with my PC as client and the mobile as server. 我写了一个套接字程序,我的PC作为客户端,移动设备作为服务器。 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()

The problem seems to be using readline() on a binary file at the server side: 问题似乎是在服务器端的二进制文件上使用readline()

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

readline() reads data from file up to the next '\\n' character. readline()从文件中读取数据,直到下一个'\\n'字符。 Using it on a binary file may result in reading a very long buffer! 在二进制文件上使用它可能会导致读取非常长的缓冲区! (Maybe even up to EOF). (甚至可能达到EOF)。 In that case, using socket.send() may fail to deliver the entire data, and the return value (=bytes transmitted) should be checked. 在这种情况下,使用socket.send()可能无法传递全部数据,因此应检查返回值(=传输的字节)。 The possibilities for fixing that is: 解决该问题的可能性是:

  1. using socket.sendall() when sending, will send the entire buffer. 发送时使用socket.sendall() ,将发送整个缓冲区。

or, alternatively (may use both) 或者,(可以同时使用)

  1. using file_name.read(1024) - which will bound the amount of data read each cycle. 使用file_name.read(1024) -将限制每个周期读取的数据量。

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. 我在客户端打开一个.jpg文件,并将数据写入其中。 But the final file size is just 1kb less that the original size. 但是最终文件的大小仅比原始大小小1kb。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM