0

I have a situation where I need to get data from a number of potentially large files via FTP, but the data I need is within the first 16 KB of each file, so to significantly speed up the operation I'd like to read only the first 16 KB, then extract the necessary info from that "string".

I've figured out how to read the file, but I'm having trouble figuring out how to output the data properly. Here's what I have right now:

req = urllib.request.urlopen('ftp://host.domain:port/path/to/file.bin')
content = req.read(16384)
req.close()
contenthex = content.decode("latin1")
print(contenthex[64:36])

The goal is to print 36 decoded bytes starting at offset 64. if I have it print the contenthex as a whole, it does seem to contain the expected data, but when specifying the range I don't get the expected strings.

I'd guess I'm doing something wrong in the conversion from hex? I tried decoding as both ASCII and utf-8, but both of those gave me errors like "'ascii' codec can't decode byte 0x80 in position 27: ordinal not in range(128)". latin1 seems to work, but like I said, can't get the range to work properly.

My question: how do I properly get the content included in that range?

For comparison, if this were a local file I'd do this with hexdump: hexdump -e '36 "%_p"' -s 64 -n 36 file.bin

I guess another way to ask the question would be how to I replicate that hexdump command with python when operating on bytes read via urllib?

Martin Prikryl
  • 188,800
  • 56
  • 490
  • 992
Jared
  • 171
  • 7
  • 3
    `contenthex[64:36]` you start at byte 64 and stop at byte 36, which results in an empty string. See https://stackoverflow.com/questions/509211/understanding-slice-notation – Wups Feb 06 '22 at 17:31
  • 1
    Oh my goodness, all I needed to do was change contenthex[64:36] to contenthex[64:100]. For some reason I had it in my head that the second value was length, not end. That's embarrassing. Would you mind pasting that as an answer and I'll happily accept it? Thanks so much. – Jared Feb 06 '22 at 17:35

0 Answers0