Am trying to send a big file in http response by writing into wfile
variable of BaseHTTPRequestHandler
in Python, when I am trying to do that I am ending with the below exception in my Python code always.
error: [Errno 10053] An established connection was aborted by the software in your machine
Can any one help me to resolve this?? why am getting the error? If the way am sending large file in HTTP response is not the good one, please suggest where I can refer.
Thanks in advance!!!
import os
import urlparse
import BaseHTTPServer
from SocketServer import ThreadingMixIn
import urlparse
class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
def handle(self):
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
def sendError(self, errorCode, errorMessage):
self.send_response(errorCode, errorMessage)
self.send_header("Content-type", "text/plain")
self.send_header("Content-Length", str(len(errorMessage)))
self.end_headers()
self.wfile.write(errorMessage)
def do_GET(self):
scm, netloc, path, params, query, fragment = urlparse.urlparse(self.path, 'http')
if path.find(".ld") > 0:
filename = path.rpartition("/")[2]
try:
with open(filename, 'rb') as f:
self.send_response(200, "Ok")
self.send_header("Content-type","application/octet-stream")
total_size = os.path.getsize(filename)
self.send_header("Content-Length", total_size)
self.end_headers()
self.wfile.write(f.read())
except IOError:
self.sendError(404, "Not Found")
class ThreadedHTTPServer(ThreadingMixIn, BaseHTTPServer.HTTPServer):
def __init__(self, server_address, RequestHandlerClass, bind_and_activate=True):
BaseHTTPServer.HTTPServer.__init__(self, server_address, RequestHandlerClass, bind_and_activate)
def main():
Handler.close_connection = 0
Handler.protocol_version = 'HTTP/1.1'
global httpd
httpd = ThreadedHTTPServer(("", 8900), Handler)
httpd.daemon_threads = True
httpd.serve_forever()
if __name__ == "__main__":
main()
Error Trace:
Exception happened during processing of request from ('172.24.128.21', 19418)
Traceback (most recent call last):
File "C:\Python27\lib\SocketServer.py", line 593, in process_request_thread
self.finish_request(request, client_address)
File "C:\Python27\lib\SocketServer.py", line 334, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "C:\Python27\lib\SocketServer.py", line 649, in __init__
self.handle()
File "simple.py", line 10, in handle
BaseHTTPServer.BaseHTTPRequestHandler.handle(self)
File "C:\Python27\lib\BaseHTTPServer.py", line 342, in handle
self.handle_one_request()
File "C:\Python27\lib\BaseHTTPServer.py", line 310, in handle_one_request
self.raw_requestline = self.rfile.readline(65537)
File "C:\Python27\lib\socket.py", line 476, in readline
data = self._sock.recv(self._rbufsize)
error: [Errno 10054] An existing connection was forcibly closed by the remote ho
st
OK, so I tried your code after cleaning it up a bit:
def do_GET(self):
_, _, path, _, _, _ = urlparse.urlparse(self.path, 'http')
if path.find(".ld") > 0:
filename = path.rpartition("/")[2]
try:
with open(filename, 'rb') as f:
self.send_response(200, "Ok")
self.send_header("Content-type", self.headers.getheader("Content-type", ""))
total_size = os.path.getsize(filename)
self.send_header("Content-Length", total_size)
self.end_headers()
self.wfile.write(f.read())
except IOError:
self.sendError(404, "Not Found")
I put a foo.pl
file in the same folder; then, when doing curl http://localhost/foo.pl
, I get a nice response with the content of the file, and no errors whatsoever.
I must also say that it looks like you're just trying to get the file name without the path part using path.rpartition("/")[2]
, but for that, you should just use os.path.basename
:
>>> os.path.basename('foo/bar/baz.pl')
'baz.pl'
Also, path.find(".ld") > 0
should probably be path.endswith(".ld")
instead.
EDIT: to support large files (efficiently):
CHUNK_SIZE = 1024 * 100 # 100kB chunks
while True:
chunk = f.read(CHUNK_SIZE)
if not chunk:
break
self.wfile.write(chunk)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.