Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 347833
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T11:18:30+00:00 2026-05-12T11:18:30+00:00

Hey gang, I’m trying to convert a legacy php script over to python and

  • 0

Hey gang, I’m trying to convert a legacy php script over to python and not having much luck.

The intent of the script is to serve up a file while concealing it’s origin. Here’s what’s working in php:

<?php
$filepath = "foo.mp3";

$filesize = filesize($filepath);

header("Pragma: no-cache");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");

// force download dialog
//header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

header('Content-Disposition: attachment;filename="'.$filepath.'"');

header("Content-Transfer-Encoding: binary");

#header('Content-Type: audio/mpeg3');
header('Content-Length: '.$filesize);

@readfile($filepath);
exit(0);
?>

When I do the equivilent in Python, I get a download that is zero bytes. Here’s what I’m trying:

#!/usr/bin/env python
# encoding: utf-8

import sys
import os
import cgitb; cgitb.enable()

filepath = "foo.mp3" 
filesize = os.path.getsize(filepath)

print "Prama: no-cache"
print "Expires: 0"
print "Cache-Control: must-revalidate, post-check=0, pre-check=0"

print "Content-Type: application/octet-stream"
print "Content-Type: application/download"

print 'Content-Disposition: attachment;filename="'+filepath+'"'

print "Content-Transfer-Encoding: binary"

print 'Content-Length: '+str(filesize)

print  #required blank line

open(filepath,"rb").read()

Can anyone please help me?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T11:18:30+00:00Added an answer on May 12, 2026 at 11:18 am

    Well, maybe it’s just me missing something, but… You are actually not writing the contents of the file to stdout. You are just reading it into memory, so it will never show up on the other side of the TCP connection…

    Try:

    sys.stdout.write(open(filepath,"rb").read())
    sys.stdout.flush()
    

    Depending on the file size, it might be better to read the file in chunks, like so:

    chunk_size = 4096
    handle = open(filepath, "rb")
    
    while True:
        buffer = handle.read(chunk_size)
        if buffer:
            sys.stdout.write(buffer)
        else:
            break
    

    Another thing to be aware of: writing binary data to stdout may cause Python to choke due to encoding issues. This depends on the Python version you are using.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 166k
  • Answers 166k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Type.GetType("class1") May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer This seems really unlikely. Have you used some other tool… May 12, 2026 at 1:19 pm
  • Editorial Team
    Editorial Team added an answer I had a stray space in a file somewhere, lots… May 12, 2026 at 1:19 pm

Related Questions

Hey gang, I need to know if i need to restart my computer to
Hey gang. I have just written a client and server in C++ using sys/socket.
Hey gang, I'm trying to convert a legacy php script over to python and
Hey everyone, I'm using Virtual PC and working with a virtual hard disk (*.vhd)
Hey so what I want to do is snag the content for the first

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.