Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - BaseHTTPServer do_GET() - wfile.write(filedata) Broken PIPE

Tags:

python

I need to setup a Python web server which returns a few 3MB files. It uses baseHTTPServer to handle GET requests. How do you send a 3MB file using wfile.write() ?

from SocketServer import ThreadingMixIn
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import BaseHTTPServer


class StoreHandler(BaseHTTPServer.BaseHTTPRequestHandler):
    request_queue_size = 100

    def do_GET(self):
        try:

            filepath = os.path.join(os.path.join(os.path.dirname(__file__), "tools"), "tools.zip")
            if not os.path.exists:
                print 'Tool doesnt exist'

            f = open(filepath, 'rb')
            file_data = f.read()
            f.close()

            self.send_header("Content-type", "application/octet-stream")
            self.end_headers()
            self.wfile.write(file_data) 
            self.send_response(200)
        except Exception,e:
            print e
            self.send_response(400)

Error:

----------------------------------------
Exception happened during processing of request from ('192.168.0.6', 41025)
Traceback (most recent call last):
  File "/usr/lib/python2.7/SocketServer.py", line 593, in process_request_thread
    self.finish_request(request, client_address)
  File "/usr/lib/python2.7/SocketServer.py", line 334, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python2.7/SocketServer.py", line 651, in __init__
    self.finish()
  File "/usr/lib/python2.7/SocketServer.py", line 710, in finish
    self.wfile.close()
  File "/usr/lib/python2.7/socket.py", line 279, in close
    self.flush()
  File "/usr/lib/python2.7/socket.py", line 303, in flush
    self._sock.sendall(view[write_offset:write_offset+buffer_size])
error: [Errno 32] Broken pipe

Edit:

Client code:

import requests

headers = {'user-agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; FSL 7.0.5.01003)'}
r = requests.get(url, headers=headers, timeout=60)
like image 991
mbudge Avatar asked Oct 18 '22 14:10

mbudge


1 Answers

You were not that far from a correct server...

You simply did not respect HTTP protocol with the order of your commands: the first command must be the send_response (or send_error), followed by other eventual header, then end_header and the data.

Also you are loading the whole file in memory, when it is not necessary. Your do_GET method could be:

def do_GET(self):
    try:

        filepath = os.path.join(os.path.join(os.path.dirname(__file__), "tools"), "tools.zip")
        if not os.path.exists:
            print 'Tool doesnt exist'

        f = open(filepath, 'rb')

        self.send_response(200)
        self.send_header("Content-type", "application/octet-stream")
        self.end_headers()
        while True:
            file_data = f.read(32768) # use an appropriate chunk size
            if file_data is None or len(file_data) == 0:
                break
            self.wfile.write(file_data) 
        f.close()
    except Exception,e:
        print e
        self.send_response(400)
like image 199
Serge Ballesta Avatar answered Oct 23 '22 22:10

Serge Ballesta