Massive code cleanup, added ServeFile class
This commit is contained in:
parent
c206724060
commit
fdac22b5d8
154
servefile
154
servefile
|
@ -56,6 +56,8 @@ class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
self.end_headers()
|
||||
|
||||
def do_GET(self):
|
||||
if self.checkAndDoRedirect():
|
||||
return
|
||||
myfile = open(self.filePath, 'rb')
|
||||
|
||||
# find out if this is a continuing download
|
||||
|
@ -178,7 +180,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
length = 0
|
||||
try:
|
||||
length = int(self.headers['Content-Length'])
|
||||
except ValueError:
|
||||
except (ValueError, KeyError):
|
||||
pass
|
||||
if length <= 0:
|
||||
self.sendResponse(400, "Content-Length was invalid or not set.")
|
||||
|
@ -211,7 +213,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
"""
|
||||
self.send_response(code)
|
||||
self.send_header('Content-Type', 'text/html')
|
||||
self.send_header('content-Length', str(len(msg)))
|
||||
self.send_header('Content-Length', str(len(msg)))
|
||||
self.end_headers()
|
||||
self.wfile.write(msg)
|
||||
|
||||
|
@ -240,55 +242,26 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
||||
pass
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Serve a single file via HTTP')
|
||||
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
|
||||
parser.add_argument('filename', metavar='file/directory', type=str)
|
||||
parser.add_argument('-p', '--port', type=int, default=8080, \
|
||||
help='port to listen on')
|
||||
parser.add_argument('-u', '--upload', action="store_true", default=False, \
|
||||
help="Enable uploads to a given directory")
|
||||
class ServeFileException(Exception):
|
||||
pass
|
||||
|
||||
args = parser.parse_args()
|
||||
class ServeFile():
|
||||
""" Main class to manage everything. """
|
||||
|
||||
handler = None
|
||||
dirCreated = False
|
||||
if not args.upload:
|
||||
try:
|
||||
testit = open(args.filename, 'r')
|
||||
testit.close()
|
||||
FileHandler.filePath = args.filename
|
||||
FileHandler.fileName = os.path.basename(args.filename)
|
||||
FileHandler.fileLength = os.stat(args.filename)[ST_SIZE]
|
||||
except IOError:
|
||||
print "Error: Could not open file!"
|
||||
sys.exit(3)
|
||||
handler = FileHandler
|
||||
else:
|
||||
if os.path.isdir(args.filename):
|
||||
print "Warning: Uploading to an already existing directory"
|
||||
elif not os.path.exists(args.filename):
|
||||
dirCreated = True
|
||||
try:
|
||||
os.mkdir(args.filename)
|
||||
except IOError, OSError:
|
||||
print "Error: Could not create directory '%s' for uploads" % (args.filename,)
|
||||
sys.exit(3)
|
||||
else:
|
||||
print "Error: Upload directory already exists and is a file"
|
||||
sys.exit(3)
|
||||
FilePutter.targetDir = args.filename
|
||||
handler = FilePutter
|
||||
(MODE_SINGLE, MODE_UPLOAD, MODE_DIRLIST) = range(3)
|
||||
|
||||
server = ThreadedHTTPServer(('', args.port), handler)
|
||||
if args.upload:
|
||||
print "Serving \"%s\" under port %d" % (args.filename, args.port)
|
||||
else:
|
||||
print "Serving \"%s\" for uploads under port %d" % (args.filename, args.port)
|
||||
def __init__(self, target, port=8080, serveMode=0):
|
||||
self.target = target
|
||||
self.port = port
|
||||
self.serveMode = serveMode
|
||||
self.dirCreated = False
|
||||
|
||||
# print urls with local network adresses
|
||||
print "\nSome addresses this will be available under:"
|
||||
if self.serveMode not in range(3):
|
||||
self.serveMode = None
|
||||
raise ValueError("Unknown serve mode, needs to be MODE_SINGLE, MODE_UPLOAD or MODE_DIRLIST")
|
||||
|
||||
def getIPs(self):
|
||||
""" Get IPs from all interfaces via ip or ifconfig. """
|
||||
# ip and ifconfig sometimes are located in /sbin/
|
||||
os.environ['PATH'] += ':/sbin:/usr/sbin'
|
||||
proc = Popen(r"ip addr|" + \
|
||||
|
@ -311,14 +284,29 @@ def main():
|
|||
print "Error: Could not locate any ips for you."
|
||||
proc = None
|
||||
if proc:
|
||||
ips = proc.stdout.read().strip()
|
||||
for ip in ips.split("\n"):
|
||||
if ip.find(":") >= 0:
|
||||
# ipv6
|
||||
ip = "[%s]" % ip
|
||||
ips = proc.stdout.read().strip().split("\n")
|
||||
# FIXME: When BaseHTTP supports ipv6 properly, delete this line
|
||||
continue
|
||||
print "http://%s:%d/" % (ip, args.port)
|
||||
ips = filter(lambda ip: ip.find(":") == -1, ips)
|
||||
return ips
|
||||
return None
|
||||
|
||||
def serve(self):
|
||||
self.handler = self._confAndFindHandler()
|
||||
server = ThreadedHTTPServer(('', self.port), self.handler)
|
||||
if self.serveMode != self.MODE_UPLOAD:
|
||||
print "Serving \"%s\" under port %d" % (self.target, self.port)
|
||||
else:
|
||||
print "Serving \"%s\" for uploads under port %d" % (self.target, self.port)
|
||||
|
||||
# print urls with local network adresses
|
||||
print "\nSome addresses this will be available under:"
|
||||
ips = self.getIPs()
|
||||
if not ips or len(ips) == 0 or ips[0] == '':
|
||||
print "Could not find any addresses"
|
||||
else:
|
||||
for ip in ips:
|
||||
print "http://%s:%d/" % (ip, self.port)
|
||||
print ""
|
||||
|
||||
try:
|
||||
server.serve_forever()
|
||||
|
@ -326,9 +314,65 @@ def main():
|
|||
server.socket.close()
|
||||
|
||||
# cleanup potential upload directory
|
||||
if dirCreated and len(os.listdir(args.filename)) == 0:
|
||||
if self.dirCreated and len(os.listdir(self.target)) == 0:
|
||||
# created upload dir was not used
|
||||
os.rmdir(args.filename)
|
||||
os.rmdir(self.target)
|
||||
|
||||
def _confAndFindHandler(self):
|
||||
handler = None
|
||||
if self.serveMode == self.MODE_SINGLE:
|
||||
try:
|
||||
testit = open(self.target, 'r')
|
||||
testit.close()
|
||||
FileHandler.filePath = self.target
|
||||
FileHandler.fileName = os.path.basename(self.target)
|
||||
FileHandler.fileLength = os.stat(self.target)[ST_SIZE]
|
||||
except IOError:
|
||||
raise ServeFileException("Error: Could not open file!")
|
||||
handler = FileHandler
|
||||
elif self.serveMode == self.MODE_UPLOAD:
|
||||
if os.path.isdir(self.target):
|
||||
raise ServeFileException("Warning: Uploading to an already existing directory")
|
||||
elif not os.path.exists(self.target):
|
||||
self.dirCreated = True
|
||||
try:
|
||||
os.mkdir(self.target)
|
||||
except IOError, OSError:
|
||||
raise ServeFileException("Error: Could not create directory '%s' for uploads" % (self.target,) )
|
||||
else:
|
||||
raise ServeFileException("Error: Upload directory already exists and is a file")
|
||||
FilePutter.targetDir = self.target
|
||||
handler = FilePutter
|
||||
return handler
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description='Serve a single file via HTTP')
|
||||
parser.add_argument('--version', action='version', version='%(prog)s ' + __version__)
|
||||
parser.add_argument('target', metavar='file/directory', type=str)
|
||||
parser.add_argument('-p', '--port', type=int, default=8080, \
|
||||
help='port to listen on')
|
||||
parser.add_argument('-u', '--upload', action="store_true", default=False, \
|
||||
help="Enable uploads to a given directory")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
# check for invalid option combinations
|
||||
|
||||
mode = None
|
||||
if args.upload:
|
||||
mode = ServeFile.MODE_UPLOAD
|
||||
#elif args.listdir:
|
||||
# mode = ServeFile.MODE_LISTDIR
|
||||
else:
|
||||
mode = ServeFile.MODE_SINGLE
|
||||
|
||||
server = None
|
||||
try:
|
||||
server = ServeFile(args.target, args.port, mode)
|
||||
server.serve()
|
||||
except ServeFileException, e:
|
||||
print e
|
||||
sys.exit(1)
|
||||
print "Good bye.."
|
||||
|
||||
if __name__ == '__main__':
|
||||
|
|
Loading…
Reference in New Issue