Python 错误:发送图像时出现“socket.error: [Errno 11] 资源暂时不可用”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39145357/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:55:34  来源:igfitidea点击:

Python error: "socket.error: [Errno 11] Resource temporarily unavailable" when sending image

pythonimagesockets

提问by Gilgamesch von Uruk

I want to make a program that accesses images from files, encodes them, and sends them to an server. Than the server is supposed to decode the image, and save it to file. I tested the image encoding itself, and it worked, so the problem lies in the server and client connection.

我想制作一个程序来访问文件中的图像,对它们进行编码,然后将它们发送到服务器。比服务器应该解码图像,并将其保存到文件中。我测试了图像编码本身,它有效,所以问题出在服务器和客户端连接上。

Here is the server:

这是服务器:

import socket
import errno
import base64

from PIL import Image
import StringIO

def connect(c):
    try:
        image = c.recv(8192)
        return image
    except IOError as e:
        if e.errno == errno.EWOULDBLOCK:
            connect(c)


def Main():
    host = '138.106.180.21'
    port = 12345

    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
    s.setsockopt(socket.SOL_SOCKET,socket.SO_REUSEADDR,1)
    s.bind((host, port))
    s.listen(1)


    while True:

        c, addr = s.accept()
        c.setblocking(0)

        print "Connection from: " + str(addr)

        image = c.recv(8192)#connect(c)

        imgname = 'test.png'

        fh = open(imgname, "wb")
        if image == 'cusdom_image':
            with open('images.png', "rb") as imageFile:
                image = ''
                image = base64.b64encode(imageFile.read())
                print image
        fh.write(image.decode('base64'))
        fh.close()


if __name__ == '__main__':
    Main()

And here is the client:

这是客户端:

import socket
import base64

from PIL import Image
import StringIO
import os, sys

ip = '138.106.180.21'
port = 12345
print 'Add event executed'
s = socket.socket()
s.connect((ip, port))

image_path = '/home/gilgamesch/Bilder/Bildschirmfoto.png'

print os.getcwd()
olddir = os.getcwd()
os.chdir('/')
print os.getcwd()

if image_path != '':
    with open(image_path, "rb") as imageFile:
        image_data = base64.b64encode(imageFile.read())
        print 'open worked'
else:
    image_data = 'cusdom_image'

os.chdir(olddir)

s.send(image_data)


s.close()

And the error message is:

错误信息是:

Traceback (most recent call last):
  File "imgserv.py", line 49, in <module>
    Main()
  File "imgserv.py", line 34, in Main
    image = c.recv(8192)#connect(c)
socket.error: [Errno 11] Resource temporarily unavailable

回答by mhawke

In the server you are setting the remote socket (that returned by accept()) to non-blocking mode, which means that I/O on that socket will terminate immediately by an exception if there is no data to read.

在服务器中,您将远程套接字(由 返回accept())设置为非阻塞模式,这意味着如果没有要读取的数据,该套接字上的 I/O 将立即因异常终止。

There will usually be a period of time between establishing the connection with the server and the image data being sent by the client. The server attempts to immediately read data from the client once the connection is accepted, however, there might not be any data to read yet, so c.recv()raises a socket.error: [Errno 11] Resource temporarily unavailableexception. Errno 11 corresponds to EWOULDBLOCK, so recv()aborted because there was no data ready to read.

在与服务器建立连接和客户端发送图像数据之间通常会有一段时间。一旦连接被接受,服务器尝试立即从客户端读取数据,但是,可能还没有任何数据要读取,因此c.recv()引发socket.error: [Errno 11] Resource temporarily unavailable异常。Errno 11 对应于EWOULDBLOCK,因此recv()中止,因为没有准备好读取的数据。

Your code does not seem to require non-blocking sockets because there is an accept()at the top of the while loop, and so only one connection can be handled at a time. You can just remove the call to c.setblocking(0)and this problem should go away.

您的代码似乎不需要非阻塞套接字,因为accept()在 while 循环的顶部有一个,因此一次只能处理一个连接。您可以删除对 的调用c.setblocking(0),此问题应该会消失。