Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Serving binary file from web server to client

Usually, when I want to transfer a web server text file to client, here is what I did

import cgi

print "Content-Type: text/plain"
print "Content-Disposition: attachment; filename=TEST.txt"
print

filename = "C:\\TEST.TXT"
f = open(filename, 'r')
for line in f:
    print line

Works very fine for ANSI file. However, say, I have a binary file a.exe (This file is in web server secret path, and user shall not have direct access to that directory path). I wish to use the similar method to transfer. How I can do so?

  • What content-type I should use?
  • Using print seems to have corrupted content received at client side. What is the correct method?

I use the following code.

#!c:/Python27/python.exe -u

import cgi

print "Content-Type: application/octet-stream"
print "Content-Disposition: attachment; filename=jstock.exe"
print

filename = "C:\\jstock.exe"
f = open(filename, 'rb')
for line in f:
    print line

However, when I compare the downloaded file with original file, it seems there is an extra whitespace (or more) for after every single line.

alt text

like image 811
Cheok Yan Cheng Avatar asked Dec 23 '10 08:12

Cheok Yan Cheng


People also ask

What is a server binary file?

A binary file is a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length. The content must be interpreted by a program or a hardware processor that understands in advance exactly how that content is formatted and how to read the data.

How do you handle binary files?

You can choose one of two methods for loading the data. 1) Use the commands open file, read from file and close file. 2) Use the URL keyword with the put command, prefixing the file path with "binfile:". Either approach allows you to place binary data into a variable so that it can be processed.

What is a binary file transfer?

Binary file transfer is a standard used to transmit data files through protocols of different telematic services, including Telefax Group 3 and 4, Teletex and Data Transfer and Manipulation (DTAM) normal mode.

Are binary files readable?

Any formatted or unformatted binary data is stored in a binary file, and this file is not human-readable and is used by the computer directly. When a binary file is required to read or transfer from one location to another location, the file's content is converted or encoded into a human-readable format.


2 Answers

Agree with the above posters about 'rb' and Content-Type headers.

Additionally:

for line in f:
    print line

This might be a problem when encountering \n or \r\n bytes in the binary file. It might be better to do something like this:

import sys
while True:
    data = f.read(4096)
    sys.stdout.write(data)
    if not data:
        break

Assuming this is running on windows in a CGI environment, you will want to start the python process with the -u argument, this will ensure stdout isn't in text-mode

like image 113
Knio Avatar answered Oct 16 '22 09:10

Knio


When opening a file, you can use open(filename, 'rb') - the 'b' flag marks it as binary. For a general handler, you could use some form of mime magic (I'm not familiar with using it from Python, I've only ever used it from PHP a couple of years ago). For the specific case, .exe is application/octet-stream.

like image 2
Chris Morgan Avatar answered Oct 16 '22 08:10

Chris Morgan