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