'How to receive a binary file through html forms using python sockets?

The form:

<form action="" method="POST" enctype="multipart/form-data">
  <input type="file" name="fileToUpload" id="fileToUpload">
  <input type="submit" value="Submit">
</form>

My python server:

import socket
s = socket.socket()
host = input('Enter your IP address: ')
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host, 80))
s.listen(5)
print('Server is ready')
while True:
    conn, addr = s.accept()
    request = conn.recv(10240).decode()
    print(type(request))
    print(request)
    

The response:

<class 'str'>
POST / HTTP/1.1
Host: 172.16.8.168
Connection: keep-alive
Content-Length: 7963
Cache-Control: max-age=0
Upgrade-Insecure-Requests: 1
Origin: http://172.16.8.168
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryAX2PNXvz9QcIPNcO
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.88 Safari/537.36
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Referer: http://172.16.8.168/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9

If the response I get from the client after submitting the form is that of a string. How do I receive the image?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source