title: Upload file via POST multipart form-data using Python
body: <p>I have the following code in C# that I need to convert into Python.</p>
  <p>Code in C#:</p>
  <pre><code>{
              var fileName = new FileInfo(file);
              
              string requestString = &quot;url to be used&quot;
              var request = (HttpWebRequest)WebRequest.Create(requestString);
              request.KeepAlive = false;      
              request.AllowAutoRedirect = false;
              request.Method = &quot;POST&quot;;
              request.Timeout = 50000;
  
              var boundary = &quot;------WebKitFormBoundaryv4JOuEW2XZeLh2TK&quot;;
              byte[] startBoundaryBytes = Encoding.ASCII.GetBytes(&quot;--&quot; + boundary + &quot;\r\n&quot;);
              string bodyHeader = &quot;Content-Disposition: form-data; name=\&quot;BackupFile\&quot;; filename=\&quot;&quot; + fileName.Name + &quot;\&quot;\r\nContent-Type: application/octet-stream\r\n\r\n&quot;;
              byte[] bodyHeaderBytes = Encoding.ASCII.GetBytes(bodyHeader);
              byte[] trailerBytes = Encoding.ASCII.GetBytes(&quot;\r\n--&quot; + boundary + &quot;--\r\n&quot;);
  
              request.ContentType = string.Format(&quot;multipart/form-data; boundary={0}&quot;, boundary);
              using (var requestStream = request.GetRequestStream()) {
                  requestStream.Write(startBoundaryBytes, 0, startBoundaryBytes.Length);
                  requestStream.Write(bodyHeader2Bytes, 0, bodyHeader2Bytes.Length);
                  //read file and add it to request
                  using(var fileStream = new FileStream(fileName.FullName, FileMode.Open, FileAccess.Read)) {
                      var buffer = new byte[4096];
                      int bytesRead = 0;
                      do {
                          bytesRead = fileStream.Read(buffer, 0, buffer.Length);
                          if(bytesRead &gt; 0) {
                              requestStream.Write(buffer, 0, bytesRead);
                          }
                      } while(bytesRead &gt; 0);
                  }
                  //write body trailer bytes
                  requestStream.Write(trailerBytes, 0, trailerBytes.Length);
              }
              
              try {
                  var response = request.GetResponse();
                  using(var reader = new StreamReader(response.GetResponseStream())) {
                      string responseText = reader.ReadToEnd();
                  }
              } catch (WebException e) {
                  ...
          }
  </code></pre>
  <p>What's the best way to convert this code to Python? So far I have the basics in Python, and from my searches I think I should be using requests library. But how can I add those boundary bytes as well as the file itself to the request?</p>
  <p>Code in Python so far (really just the basic stuff, I've been trying different stuff with the requests lib but with no success at all):</p>
  <pre><code>        url = &quot;url_to_be_used&quot;
  
          fileName = &quot;sample.sample&quot;
          boundary = &quot;------WebKitFormBoundaryv4JOuEW2XZeLh2TK&quot;
          startBoundaryBytes = (&quot;--&quot; + boundary + &quot;\r\n&quot;).encode(&quot;ascii&quot;)
          bodyHeader2 = &quot;Content-Disposition: form-data; name=\&quot;BackupFile\&quot;; filename=\&quot;&quot; + fileName + &quot;\&quot;\r\nContent-Type: application/octet-stream\r\n\r\n&quot;
          bodyHeader2Bytes = bodyHeader2.encode(&quot;ascii&quot;)
          trailerBytes = (&quot;\r\n--&quot; + boundary + &quot;--\r\n&quot;).encode(&quot;ascii&quot;)
  
          headers = {&quot;Content-Type&quot;: &quot;multipart/form-data; boundary={}&quot;.format(boundary)}
  </code></pre>
  <p>Any help would be appreciated!</p>
  <p>Thank you</p>