获取错误:[Errno 10053]尝试在http响应中发送文件

时间:2013-10-01 10:45:40

标签: python http basehttpserver

我试图通过在Python中写入wfile BaseHTTPRequestHandler变量来发送http响应中的大文件,当我尝试这样做时,我的Python代码总是以下面的异常结束

error: [Errno 10053] An established connection was aborted by the software in your machine

任何人都可以帮我解决这个问题吗?为什么会收到错误? 如果在HTTP响应中发送大文件的方式不是很好,请建议我可以参考的地方。

提前致谢!!!

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()

错误追踪:

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

1 个答案:

答案 0 :(得分:0)

好的,所以我在清理了一下之后尝试了你的代码:

    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")

我将foo.pl个文件放在同一个文件夹中;然后,在执行curl http://localhost/foo.pl时,我对文件的内容得到了很好的回复,并且没有任何错误。

我还必须说,您似乎只是尝试使用path.rpartition("/")[2]获取没有路径部分的文件名,但为此,您应该只使用os.path.basename

>>> os.path.basename('foo/bar/baz.pl')
'baz.pl'

此外,path.find(".ld") > 0可能应该是path.endswith(".ld")

编辑以支持大型文件(高效):

CHUNK_SIZE = 1024 * 100  # 100kB chunks
while True:
    chunk = f.read(CHUNK_SIZE)
    if not chunk:
        break
    self.wfile.write(chunk)