# -*- coding: gbk
import http.server
import time
import socketserver
import os
import threading
import socket
#下面的导入从SimpleHTTPServer.py复制:
import posixpath
import urllib.parse
import cgi
import sys
import shutil
import mimetypes
import io
import re
PORT = 9000
class MyThreadingHTTPServer(socketserver.ThreadingTCPServer):
allow_reuse_address = 1
def server_bind(self):
"""Override server_bind to store the server name."""
socketserver.TCPServer.server_bind(self)
host, port = self.socket.getsockname()[:2]
self.server_name = socket.getfqdn(host)
self.server_port = port
#Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
class MyHTTPRequestHandler(http.server.SimpleHTTPRequestHandler):
def dumpRequestHeaders(self):
pass #print 'dumpRequestHeaders: raw_requestline=%s \nheaders=\n%s' % (self.raw_requestline,self.headers)
def copyfile_by_range(self, fin, fout, start, end):
print( "copyfile_by_range: start=%d end=%d" % (start, end))
READ_BUFFER_SIZE = 4*1024;
fin.seek(start, os.SEEK_SET)
if end<0: #代表原始Range请求未指定完整范围,只指定了开始位置
buf = fin.read(READ_BUFFER_SIZE) #FIXME:健壮性fix,如果读到内容小于size参数?需要判断len(buf)
if len(buf)!=READ_BUFFER_SIZE:
pass #print "copyfile_by_range: len(buf)!=READ_BUFFER_SIZE 1 len(buf)=%d" % (len(buf))
while buf:
fout.write(buf)
fout.flush()
buf = fin.read(READ_BUFFER_SIZE)
if len(buf)==0:
break #??
if len(buf)!=READ_BUFFER_SIZE:
pass #print "copyfile_by_range: len(buf)!=READ_BUFFER_SIZE 2 len(buf)=%d" % (len(buf))
fout.write(buf)
break
else:
bytes_left = end-start+1
while bytes_left >= READ_BUFFER_SIZE:
buf = fin.read(READ_BUFFER_SIZE)
if len(buf)!=READ_BUFFER_SIZE:
pass #print "copyfile_by_range: len(buf)!=READ_BUFFER_SIZE 3 len(buf)=%d" % (len(buf))
fout.write(buf)
bytes_left = bytes_left - READ_BUFFER_SIZE
if bytes_left>0:
buf = fin.read(bytes_left)
if len(buf)!=bytes_lef
Python3 http服务器脚本,支持range请求头部(因此可以用它来在线看mp4视频)
最新推荐文章于 2023-03-20 10:18:38 发布
![](https://img-home.csdnimg.cn/images/20240711042549.png)