Python Forum
Python3: how to read binary?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3: how to read binary?
#1
Hi!

I try to upload mp4 video file. When I use Python 2+ it works. When I use Python 3 - not

I read the file as binary.

def get_file_data(path):
    with open(path, 'rb') as f:
        fdata = f.read()       
    return fdata

dct = {}
dct['title'] = 'My title'
dct['video_data'] = get_file_data('test.mp4')

print(video_data[:50])
b'\x00\x00\x00 ftypisom\x00\x00\x02\x00isomiso2avc1mp41\x00\x00\x00\x08free%\x9d\xbb\xd7mdat\xff\xfb'
After that I make data, add header and try to post(upload).

data = '''-----------------------------48532483525862
Content-Disposition: form-data; name="title"

{title}
-----------------------------48532483525862
Content-Disposition: form-data; name="Filedata"; filename="test.mp4"
Content-Type: video/mp4

{video_data}
-----------------------------48532483525862--'''.format(**dct)


s.headers['Content-Type'] = 'multipart/form-data; boundary=-----------------------------48532483525862'

resp = s.post(upload_url, data)
But remote server returns error

"message":"Expecting a file during post.","code":0

I think it's because of bytes. I tried to decode fdata.decode('latin-1'), but this is useless. Server returns error.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How do I read and write a binary file in Python? blackears 6 6,593 Jun-06-2023, 06:37 PM
Last Post: rajeshgk
  Read/Write binary file deanhystad 3 3,190 Feb-01-2021, 10:29 AM
Last Post: Larz60+
  Python3 binary tree not replacing parent nodes with child nodes Aspect11 0 1,772 Sep-23-2020, 02:22 PM
Last Post: Aspect11
  Binary File Read Aussie 6 8,395 Sep-03-2020, 03:57 AM
Last Post: deanhystad
  python3 convert hex to binary 32 bit Mkt 3 3,949 Aug-28-2020, 02:34 PM
Last Post: Mkt
  python read binary file Pyguys 4 3,875 Jul-13-2020, 02:34 AM
Last Post: Pyguys
  hex file to binary or pcap to binary baran01 1 5,694 Dec-11-2019, 10:19 PM
Last Post: Larz60+
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,905 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  How to Read Binary Data pyth0nus3r 1 2,207 Jun-09-2019, 08:58 PM
Last Post: DeaD_EyE
  Read binary from file nextor 6 3,049 Nov-27-2018, 09:10 PM
Last Post: nextor

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020