Posts: 203
Threads: 41
Joined: Mar 2019
Nov-05-2019, 12:49 PM
(This post was last modified: Nov-05-2019, 12:49 PM by mcgrim.)
I am trying to send a few pics via socket from server to client.
I get this error on the client side, and I am not sure how to fix it.
Error: Traceback (most recent call last):
File "C:/Users/PycharmProjects/client-server/client.py", line 34, in <module>
im = Image.open(fp)
File "C:\Users\PycharmProjects\client-server\venv\lib\site-packages\PIL\Image.py", line 2818, in open
raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file <_io.BufferedReader name='test.png'>
here are both of my codes
SERVER
import cv2
import time
import socket
import glob
import os
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
print("server started...")
path1 = (r"C:\Users\Desktop\opencvpics")
def readFileImages():
st = os.path.join(path1, "*.png")
print(st)
return glob.glob(st)
list1=readFileImages()
print(list1, "list1......")
while True:
c, addr = s.accept()
print(f"connection from {addr} has been established !")
c.send(bytes("welcome to the server".encode()))
for pics in list1:
f=open(pics, 'rb')
while True:
veri=f.read()
if not veri:
break
s.send(veri)
f.close()
data = s.recv(4096)
data_arr = pickle.loads(data)
newrow = numpy.asarray(data_arr)
myoutput = numpy.vstack([myoutput, newrow])
s.close() import socket
from PIL import Image
import pickle
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
msg=s.recv(1024)
print(msg.decode())
fname="test.png"
fp = open(fname,'rb')
# image
while True:
strng = s.recv(1024)
if not strng:
break
fp.write(strng)
im = Image.open(fp)
T= im.size
data=pickle.dumps(T)
s.send(data)
fp.close() In the client code, I tried to move 'fp.close()'before 'im=Image.open(fp)' but it would then complain about dealing with a closed file.
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-05-2019, 02:41 PM
(This post was last modified: Nov-05-2019, 02:41 PM by mcgrim.)
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-05-2019, 03:28 PM
(This post was last modified: Nov-05-2019, 03:28 PM by mcgrim.)
I kept working on the code and changed a few lines.
Now I get no error, however, it looks like that the client is not fully executed,
in other words, I should be able to see these two statements being printed on the last lines
print('Successfully get the file')
print('connection closed') but I don't. What exactly is stopping it?
Here is my edited code for serve and client.
SERVER
import cv2
import time
import socket
import glob
import os
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
print("server started...")
path1 = (r"C:\Users\Desktop\opencvpics")
def readFileImages():
st = os.path.join(path1, "*.png")
print(st)
return glob.glob(st)
list1=readFileImages()
print(list1, "list1......")
while True:
c, addr = s.accept()
print(f"connection from {addr} has been established !")
c.send(bytes("welcome to the server".encode()))
for pics in list1:
f=open(pics, 'rb')
l = f.read(1024)
while(l):
c.send(l)
print('Sent ', repr(l))
l = f.read(1024)
f.close()
print('Done sending')
c.send('Thank you for connecting'.encode()) CLIENT
import socket
from PIL import Image
import pickle
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send('Hello server!'.encode())
with open('received_file.png', 'wb') as f:
print('file is open')
while True:
print('receiving data...')
data = s.recv(1024)
print('data=%s', data)
print('Successfully get the file')
s.close()
print('connection closed') By the way, this time I included my code into the code box as usual, but somehow it doesn't look like I did. I am not sure why.
In addition, only one picture is received, instead of all of them.
Posts: 12,053
Threads: 488
Joined: Sep 2016
Starting a new thread on same subject is taboo see: https://python-forum.io/misc.php?action=help&hid=22
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-05-2019, 03:41 PM
(This post was last modified: Nov-05-2019, 04:10 PM by mcgrim.)
sorry, I haven't thought about it.
Thanks for reminding me.
Can you actually please help me?
I have been stuck in this for awhile, :)
Can anyone help me ?
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-05-2019, 05:45 PM
(This post was last modified: Nov-05-2019, 05:45 PM by mcgrim.)
I continued to work on it, by turning bytes back into images, and I am now getting an error on the client side,
even though the files seem to be transferred.
Error: Traceback (most recent call last):
File "C:/Users/PycharmProjects/client-server/client.py", line 31, in <module>
img = Image.open(io.BytesIO(data))
File "C:\Users\\PycharmProjects\client-server\venv\lib\site-packages\PIL\Image.py", line 2818, in open
raise IOError("cannot identify image file %r" % (filename if filename else fp))
OSError: cannot identify image file <_io.BytesIO object at 0x076EE450>
SERVER
import cv2
import time
import socket
import glob
import os
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, port))
s.listen(5)
print("server started...")
path1 = (r"C:\Users\Desktop\opencvpics")
def readFileImages():
st = os.path.join(path1, "*.png")
print(st)
return glob.glob(st)
list1 = readFileImages()
print(list1, "list1......")
print(type(list1))
while True:
c, addr = s.accept()
print(f"connection from {addr} has been established !")
c.send(bytes("welcome to the server".encode()))
for pics in list1:
f = open(pics, 'rb')
l = f.read(1024000)
while (l):
c.send(l)
print('Sent ', repr(l))
l = f.read(1024)
f.close()
print('Done sending')
c.send('Thank you for connecting'.encode()) CLIENT
import socket
import cv2
from PIL import Image
import io
import pickle
host = "127.0.0.1"
port = 5000
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
s.send('Hello server!'.encode())
with open('received_file.png', 'wb') as f:
print('file is open')
#while f:
print('receiving data...')
data = s.recv(1024)
print('data=%s', data, "ASDFASDSADFASDFVSSDSAFSADDFDSAFD")
f.write(data)
print('Successfully get the files')
print('connection closed')
print(type(data))
print(len(data))
img = Image.open(io.BytesIO(data))
img.show()
s.close() P.S.
The directory in the client contains a few images, and I desire to send them all to the client.
Posts: 12,053
Threads: 488
Joined: Sep 2016
this is not something I would do very often, but here's a link to an example: https://stackoverflow.com/a/42534868
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-06-2019, 08:16 AM
(This post was last modified: Nov-06-2019, 08:16 AM by mcgrim.)
can you or anyone actually figure out what is the mistake in the last code I posted?
I was already aware of that link, but I need to see how "multiple" images are sent to the client (not from).
Posts: 203
Threads: 41
Joined: Mar 2019
Nov-06-2019, 01:53 PM
(This post was last modified: Nov-06-2019, 02:37 PM by mcgrim.)
I noticed that the loop in line 33 on the server code only takes the first picture, when I thought it would iterate through the entire list, how so? how to fix it ?
these lines alone
path1 = (r"C:\Users\Desktop\opencvpics")
def readFileImages():
st = os.path.join(path1, "*.png")
print(st)
return glob.glob(st)
for i in readFileImages()[:]:
f=open(i, 'rb')
l=f.read()
print(len(l)) iterate as many times as the length of the list, but these same lines implemented in the server code above, stop after the first iteration. Why ?
Posts: 203
Threads: 41
Joined: Mar 2019
Doesn't anyone know how to help me?
I am not asking anything too demanding.
Let me know if there are any problems in my way of asking,
so I can rearrange.
|