=> POST text <=
Main.py code:
self.cropped.save(os.environ['TEMP']+'\\BFCP.SCRSH.TEMP.png',format='PNG')
file = open(os.environ['TEMP']+'\\BFCP.SCRSH.TEMP.png','rb')
files = {sd: file.read()}
file.close()
rs = requests.post('http://192.168.1.48:8000', files=files)
sd - image code (5dfghr7efh) Here I want to send PIL.image (self.cropped.save) to server by post method. server.py code:
import socket
client_socket,adress = server.accept()
print('Accepted')
data = client_socket.recv(2048)
f = open('code','wb')
f.write(data)
f.close()
data = data.decode('utf-8')
print(client_socket, adress,data)
Main.py error:
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response'))
server.py error:
Traceback (most recent call last):
File "server.py", line 75, in <module>
start()
File "server.py", line 21, in start
data = data.decode('utf-8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x89 in position 363: invalid start byte
What's wrong with my code ?
decode()
is for text files but you send image.
Don't use decode()
for images, audio files, movies, excel files, PDF, etc.
You may get only headers
(all before empty line \\n\\n
) and decode headers
but rest ( body
with form
data) display without decoding.
headers, body = data.split(b'\n\n')
headers = headers.decode('utf-8')
print(headers)
print(body)
Eventually you should split body
into parts image info
, image data
using also \\n\\n
and decode info
but not data
. And then you may save data
as image.png
.
It shows that using socket
may need a lot of work and it may be simpler to use module http
to create server. OR event simpler can be use web frameworks like flask
to run server.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.