title: Python Receive HTTP file via POST
body: <p>I'm trying to create a Python web server that can receive files. So someone can vist the website, click the upload button on the form, then the file will be sent to the server and stored locally on the server.</p>
  <p>Here is the contents of index.html</p>
  <pre><code>&lt;form enctype=&quot;multipart/form-data&quot; action=&quot;&quot; method=&quot;POST&quot;&gt;
      &lt;input type=&quot;hidden&quot; name=&quot;MAX_FILE_SIZE&quot; value=&quot;8000000&quot; /&gt;
      &lt;input name=&quot;uploadedfile&quot; type=&quot;file&quot; /&gt;&lt;br /&gt;
      &lt;input type=&quot;submit&quot; value=&quot;Upload File&quot; /&gt;
  &lt;/form&gt;
  </code></pre>
  <p>Contents of Server.py</p>
  <pre><code>import socket
  
  class server():
      def __init__(self):
          self.host_ip = socket.gethostbyname(socket.gethostname())
          self.host_port = 81
          self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
          self.data_recv_size = 1024
  
      def get_data(self, conn):
          &quot;&quot;&quot; gets the data from client &quot;&quot;&quot;
          data = b&quot;&quot;
          while b&quot;\r\n\r\n&quot; not in data:
              data += conn.recv(self.data_recv_size)
          return data
  
      def server(self):
          &quot;&quot;&quot; main method starts the server &quot;&quot;&quot;
          print(f&quot;[+] Server started listening on port {self.host_port}!&quot;)
          print(f&quot;[+] Server Ip: {self.host_ip}&quot;)
          self.s.bind((self.host_ip, self.host_port))
          self.s.listen()
  
          while True:
              conn, addr = self.s.accept()
              with conn:
                  data = self.get_data(conn)
                  
                  # GET request
                  if data[0:5] == b&quot;GET /&quot;:
                      index = open(&quot;index.html&quot;, &quot;rb&quot;).read()
                      conn.sendall(b&quot;HTTP/1.0 200 OK\nContent-Type: text/html\n\n&quot; + index)
                      print(&quot;[+] Responded to GET request&quot;)
  
                  # POST request
                  elif data[0:4] == b&quot;POST&quot;:
                      with open(&quot;output.txt&quot;, &quot;ab&quot;) as file:
                          file.write(data)
                          print(f&quot;{len(data)} bytes received from post!&quot;)
                          conn.sendall(b&quot;HTTP/1.0 200 OK\r\nContent-Type: text/html&quot;)
  
  s = server()
  s.server()
  </code></pre>
  <p>The GET part of the server works correctly, when I visit the website the index.html file is display in my web browser and I can see the file upload form.</p>
  <p>EDIT: I updated the form to max file size 8 million <code>name=&quot;MAX_FILE_SIZE&quot; value=&quot;8000000&quot;</code>, The POST response the server receives is much bigger (I updated it below), but it still doesn't look like it contains the file contents.</p>
  <pre><code>POST / HTTP/1.1
  Host: 169.254.126.211:81
  Connection: keep-alive
  Content-Length: 2857323
  Cache-Control: max-age=0
  Origin: http://169.254.126.211:81
  Upgrade-Insecure-Requests: 1
  DNT: 1
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryjbf7KaGShYBQ75wT
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 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://169.254.126.211:81/
  Accept-Encoding: gzip, deflate
  Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,ru;q=0.7
  
  ------WebKitFormBoundaryjbf7KaGShYBQ75wT
  Content-Disposition: form-data; name=&quot;MAX_FILE_SIZE&quot;
  
  8000000
  ------WebKitFormBoundaryjbf7KaGShYBQ75wT
  Content-Disposition: form-data; name=&quot;uploadedfile&quot;; filename=&quot;IMG_20210131_165637.jpg&quot;
  Content-Type: image/jpeg
  
  ÿØÿá„ÙExif  MM *         @      
  °         ö       ¶       ¾POST / HTTP/1.1
  Host: 169.254.126.211:81
  Connection: keep-alive
  Content-Length: 2857323
  Cache-Control: max-age=0
  Origin: http://169.254.126.211:81
  Upgrade-Insecure-Requests: 1
  DNT: 1
  Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryjbf7KaGShYBQ75wT
  User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.107 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://169.254.126.211:81/
  Accept-Encoding: gzip, deflate
  Accept-Language: en-GB,en-US;q=0.9,en;q=0.8,ru;q=0.7
  </code></pre>
  <p>Screenshot showing the output in Python IDLE when I run the script.</p>
  <p><a href="https://i.stack.imgur.com/ez6rR.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/ez6rR.png" alt="enter image description here" /></a></p>
  <p>Edit: It only says 1024 bytes received from post!, so it looks like the full file isn't being sent.</p>
  <p>How do I sent a file from a web browser via POST, and receive the file on the server?</p>