使用套接字Python发送图像 [英] Send images using sockets Python

查看:137
本文介绍了使用套接字Python发送图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码的一些问题...发送不是整数图像,但有些字节,是否有人可以帮助我?我想发送我在文件夹中找到的所有图像。谢谢。

I have some problems with this code... send not the integer image but some bytes, is there someone than can help me? I want to send all images I find in a folder. Thank you.

import socket
import sys
import os

s = socket.socket()
s.connect(("localhost",9999))       #IP address, port
sb = 'c:\\python27\\invia'

os.chdir(sb)                        #path

dirs =os.listdir(sb)                #list of file
print dirs

for file in dirs:
   f=open(file, "rb")               #read image
   l = f.read()
   s.send(file)                     #send the name of the file
   st = os.stat(sb+'\\'+file).st_size  
   print str(st)
   s.send(str(st))                  #send the size of the file
   s.send(l)                        #send data of the file
   f.close()
s.close()



服务器



SERVER

import socket
import sys
import os

s = socket.socket()
s.bind(("localhost",9999))
s.listen(4)                             #number of people than can connect it
sc, address = s.accept()

print address
sb = 'c:\\python27\\ricevi'

os.chdir(sb)
while True:
    fln=sc.recv(5)                      #read the name of the file
    print fln
    f = open(fln,'wb')                  #create the new file
    size = sc.recv(7)                   #receive the size of the file
    #size=size[:7]
    print size
    strng = sc.recv(int(size))          #receive the data of the file 
    #if strng:
    f.write(strng)                      #write the file
    f.close()
sc.close()
s.close()   


推荐答案

要通过单个套接字传输一系列文件,您需要一些方法来描述每个文件。实际上,您需要在套接字顶部运行一个小协议,以便您了解每个文件的元数据,例如其大小和名称,当然还有图像数据。

To transfer a sequence of files over a single socket, you need some way of delineating each file. In effect, you need to run a small protocol on top of the socket which allows to you know the metadata for each file such as its size and name, and of course the image data.

您似乎正在尝试这样做,但发件人和收件人必须就协议达成一致。

It appears you're attempting to do this, however both sender and receiver must agree on a protocol.

您的发件人中有以下内容:

You have the following in your sender:

s.send(file)                     #send the name of the file
st = os.stat(sb+'\\'+file).st_size  
s.send(str(st))                  #send the size of the file
s.send(l)

接收方如何知道文件名的长度?或者,接收方将如何知道文件名末尾的位置以及文件大小的起始位置?你可以想象接收者获得像 foobar.txt8somedata 这样的字符串,并且必须推断该文件的名称是 foobar.txt ,文件长度为8个字节,包含数据 somedata

How is the receiver to know how long the file name is? Or, how will the receiver know where the end of the file name is, and where the size of the file starts? You could imagine the receiver obtaining a string like foobar.txt8somedata and having to infer that the name of the file is foobar.txt, the file is 8 bytes long containing the data somedata.

你需要做的是分开带有某种分类的数据,例如 \ n ,以指示每条元数据的边界。

What you need to do is separate the data with some kind of delimeter such as \n to indicate the boundary for each piece of metadata.

你可以设想数据包结构为< filename> \ n< file_size> \ n< file_contents> 。来自发射机的示例数据流可能如下所示:

You could envisage a packet structure as <filename>\n<file_size>\n<file_contents>. An example stream of data from the transmitter may then look like this:

foobar.txt\n8\nsomedata

然后接收器会解码传入的流,寻找 \ n 在输入中确定每个字段的值,例如文件名和大小。

The receiver would then decode the incoming stream, looking for \n in the input to determine each field's value such as the file name and size.

另一种方法是为文件名和大小分配固定长度的字符串,然后是文件的数据。

Another approach would be to allocate fixed length strings for the file name and size, followed by the file's data.

这篇关于使用套接字Python发送图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆