使用套接字编程Python发送图像

时间:2012-01-24 22:06:21

标签: python image sockets

我正在尝试使用python中的套接字编程发送图像文件。我能够发送一个文本文件。但我一直在尝试发送一个图像文件,打开它并以字符串形式读取图像文件的内容,然后发送字符串并在客户端接收它,然后将其写入一个具有相同名称的文件中。不同的目录,但我无法打开该文件。我也在下面发布我的代码,评论的部分表明我已经尝试过了。此外,我在发送整数size时遇到问题,但我在另一端收到了一些随机字符串。

这是服务器脚本

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5000))
server_socket.listen(5)
import os


client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"
while (1):
    choice = client_socket.recv(1024)
    choice = int(choice)
    if(choice == 1):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        size = os.path.getsize(data)
        size = str(size)
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

    if (choice == 2):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        #strng = str(fp)
        size = os.path.getsize(data)
        print size
        size = str(size)
        print size
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

这是客户端脚本 -

#!/usr/bin/python
# TCP client example
import socket,os
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("", 5000))
k = ' '
size = 1024

while(1):
    print "Do you want to transfer a \n1.Text File\n2.Image\n3.Video\n"
    k = raw_input()
    client_socket.send(k)
    k = int (k)
    if(k == 1):
        print "Enter file name\n"
        strng = raw_input()
        client_socket.send(strng)
        size = client_socket.recv(1024)
        size = int(size)
        print "The file size is - ",size," bytes"
        size = size*2
        strng = client_socket.recv(size)
        print "\nThe contents of that file - "
        print strng

    if (k==2):
        print "Enter file name of the image with extentsion (example: filename.jpg,filename.png) - "
        fname = raw_input()
        client_socket.send(fname)
        size = client_socket.recv(1)
        print size
        #size = int(size)
        print "The file size is - ",size
        size = size*2
        strng = client_socket.recv(256456)
        print "\nThe file will be saved and opened- "
        fname = 'downloads/'+fname
        nf = open(fname,'w')
        nf.write(strng)
        nf.close()
        fname = 'viewnior '+ fname
        print fname
        os.system(fname)

我在Crunchbang Linux上编程 - 基于非官方Debian的Distro viewnior是图像查看器。

2 个答案:

答案 0 :(得分:8)

谢谢Sanket。 :D我在以巨大的块发送数据时出现问题,因此我将其拆分为512字节,现在即使对于视频也能正常工作。 :D只需要添加pygtk接口以确认接收文件并发送它们:D

#!/usr/bin/python
# TCP client example
import socket,os
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect(("", 5005))
k = ' '
size = 1024

while(1):
    print "Do you want to transfer a \n1.Text File\n2.Image\n3.Video\n"
    k = raw_input()
    client_socket.send(k)
    k = int (k)
    if(k == 1):
        print "Enter file name\n"
        strng = raw_input()
        client_socket.send(strng)
        size = client_socket.recv(1024)
        size = int(size)
        print "The file size is - ",size," bytes"
        size = size*2
        strng = client_socket.recv(size)
        print "\nThe contents of that file - "
        print strng

    if (k==2 or k==3):
        print "Enter file name of the image with extentsion (example: filename.jpg,filename.png or if a video file then filename.mpg etc) - "
        fname = raw_input()
        client_socket.send(fname)
        fname = 'documents/'+fname
        fp = open(fname,'w')
        while True:
            strng = client_socket.recv(512)
            if not strng:
                break
            fp.write(strng)
        fp.close()
        print "Data Received successfully"
        exit()
        #data = 'viewnior '+fname
        #os.system(data)

这是服务器代码

import socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind(("", 5005))
server_socket.listen(5)
import os


client_socket, address = server_socket.accept()
print "Conencted to - ",address,"\n"
while (1):
    choice = client_socket.recv(1024)
    choice = int(choice)
    if(choice == 1):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        fp = open(data,'r')
        strng = fp.read()
        size = os.path.getsize(data)
        size = str(size)
        client_socket.send(size)
        client_socket.send (strng)
        #client_socket.close()

    if (choice == 2 or choice == 3):
        data = client_socket.recv(1024)
        print "The following data was received - ",data
        print "Opening file - ",data
        img = open(data,'r')
        while True:
            strng = img.readline(512)
            if not strng:
                break
            client_socket.send(strng)
        img.close()
        print "Data sent successfully"
        exit()
        #data = 'viewnior '+data
        #os.system(data)

答案 1 :(得分:7)

我在我的系统上运行了相同的代码(Ubuntu 11.10),我发现将字符串发送大小有问题。当我插入逻辑来处理该部分时,它运行顺利。我也可以打开文件。 这就是我如何解决你的问题: 客户端代码(client.py)的第一个更改是接受大小并发送有关它的确认: -

size = ' '
while(1):
   tmpsize = client_socket.recv(1)
   if tmpsize.isdigit() == True:
      print "Here : ",tmpsize
      size += tmpsize
   else:
      break

client_socket.send("received")

第二个更改是服务器端(server.py)代码,接受确认: -

client_socket.send(size)
ack = client_socket.recv(1024)
if ack == "received":
   client_socket.send (strng)

我希望这可以帮助您解决问题。