forked from seba/servefile
Code reformatting
* replace tabs with spaces * replace some of the != None with is not None etc. * more whitespace fixes * remove all the newline \
This commit is contained in:
parent
c7af20388d
commit
96e9e76ff4
|
@ -42,11 +42,13 @@ try:
|
|||
except ImportError:
|
||||
pass
|
||||
|
||||
|
||||
def getDateStrNow():
|
||||
""" Get the current time formatted for HTTP header """
|
||||
now = datetime.datetime.fromtimestamp(time.mktime(time.gmtime()))
|
||||
return now.strftime("%a, %d %b %Y %H:%M:%S GMT")
|
||||
|
||||
|
||||
class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||
fileName = None
|
||||
blockSize = 1024 * 1024
|
||||
|
@ -141,7 +143,7 @@ class FileBaseHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
# now we can wind the file *brrrrrr*
|
||||
myfile.seek(fromto[0])
|
||||
|
||||
if fromto != None:
|
||||
if fromto is not None:
|
||||
self.send_response(216)
|
||||
self.send_header('Content-Range', 'bytes %d-%d/%d' % (fromto[0], fromto[1], fileLength))
|
||||
fileLength = fromto[1] - fromto[0] + 1
|
||||
|
@ -246,7 +248,7 @@ class TarFileHandler(FileBaseHandler):
|
|||
# give the process a short time to find out if it can
|
||||
# pack/compress the file
|
||||
time.sleep(0.05)
|
||||
if tarCmd.poll() != None and tarCmd.poll() != 0:
|
||||
if tarCmd.poll() is not None and tarCmd.poll() != 0:
|
||||
# something went wrong
|
||||
print("Error while compressing '%s'. Aborting request." % self.target)
|
||||
self.send_response(500)
|
||||
|
@ -610,7 +612,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
env = os.environ
|
||||
env['REQUEST_METHOD'] = "POST"
|
||||
fstorage = cgi.FieldStorage(fp=self.rfile, headers=self.headers, environ=env)
|
||||
if not "file" in fstorage:
|
||||
if "file" not in fstorage:
|
||||
self.sendResponse(400, "No file found in request.")
|
||||
return
|
||||
|
||||
|
@ -677,7 +679,8 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
self.sendResponse(411, "Content-Length was invalid or not set.")
|
||||
return -1
|
||||
if self.maxUploadSize > 0 and length > self.maxUploadSize:
|
||||
self.sendResponse(413, "Your file was too big! Maximum allowed size is %d byte. <a href=\"/\">back</a>" % self.maxUploadSize)
|
||||
self.sendResponse(413, "Your file was too big! Maximum allowed size is %d byte. <a href=\"/\">back</a>" %
|
||||
self.maxUploadSize)
|
||||
return -1
|
||||
return length
|
||||
|
||||
|
@ -714,6 +717,7 @@ class FilePutter(BaseHTTPServer.BaseHTTPRequestHandler):
|
|||
return extraDestFileName
|
||||
# never reached
|
||||
|
||||
|
||||
class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer):
|
||||
def handle_error(self, request, client_address):
|
||||
_, exc_value, _ = sys.exc_info()
|
||||
|
@ -789,6 +793,7 @@ class SecureHandler():
|
|||
self.rfile = socket._fileobject(self.request, "rb", self.rbufsize)
|
||||
self.wfile = socket._fileobject(self.request, "wb", self.wbufsize)
|
||||
|
||||
|
||||
class ServeFileException(Exception):
|
||||
pass
|
||||
|
||||
|
@ -951,7 +956,8 @@ class ServeFile():
|
|||
server = SecureThreadedHTTPServer(self._getCert(), self._getKey(),
|
||||
(listenIp, self.port), handler, bind_and_activate=False)
|
||||
except SSL.Error as e:
|
||||
raise ServeFileException("SSL error: Could not read SSL public/private key from file(s) (error was: \"%s\")" % (e[0][0][2],))
|
||||
raise ServeFileException("SSL error: Could not read SSL public/private key "
|
||||
"from file(s) (error was: \"%s\")" % (e[0][0][2],))
|
||||
else:
|
||||
server = ThreadedHTTPServer((listenIp, self.port), handler,
|
||||
bind_and_activate=False)
|
||||
|
@ -984,7 +990,7 @@ class ServeFile():
|
|||
print("Serving \"%s\" for uploads at port %d." % (self.target, self.port))
|
||||
|
||||
# print urls with local network adresses
|
||||
print("\nSome addresses %s will be available at:" % \
|
||||
print("\nSome addresses %s will be available at:" %
|
||||
("this file" if (self.serveMode != self.MODE_UPLOAD) else "the uploadform", ))
|
||||
ips = self.getIPs()
|
||||
if not ips or len(ips) == 0 or ips[0] == '':
|
||||
|
@ -1060,6 +1066,7 @@ class ServeFile():
|
|||
AuthenticationHandler.authString = self.auth
|
||||
if self.authrealm:
|
||||
AuthenticationHandler.realm = self.authrealm
|
||||
|
||||
class AuthenticatedHandler(AuthenticationHandler, handler):
|
||||
pass
|
||||
handler = AuthenticatedHandler
|
||||
|
@ -1115,32 +1122,32 @@ def main():
|
|||
parser = argparse.ArgumentParser(prog='servefile', 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, \
|
||||
parser.add_argument('-p', '--port', type=int, default=8080,
|
||||
help='Port to listen on')
|
||||
parser.add_argument('-u', '--upload', action="store_true", default=False, \
|
||||
parser.add_argument('-u', '--upload', action="store_true", default=False,
|
||||
help="Enable uploads to a given directory")
|
||||
parser.add_argument('-s', '--max-upload-size', type=str, \
|
||||
parser.add_argument('-s', '--max-upload-size', type=str,
|
||||
help="Limit upload size in kB. Size modifiers are allowed, e.g. 2G, 12MB, 1B")
|
||||
parser.add_argument('-l', '--list-dir', action="store_true", default=False, \
|
||||
parser.add_argument('-l', '--list-dir', action="store_true", default=False,
|
||||
help="Show directory indexes and allow access to all subdirectories")
|
||||
parser.add_argument('--ssl', action="store_true", default=False, \
|
||||
parser.add_argument('--ssl', action="store_true", default=False,
|
||||
help="Enable SSL. If no key/cert is specified one will be generated")
|
||||
parser.add_argument('--key', type=str, \
|
||||
parser.add_argument('--key', type=str,
|
||||
help="Keyfile to use for SSL. If no cert is given with --cert the keyfile will also be searched for a cert")
|
||||
parser.add_argument('--cert', type=str, \
|
||||
parser.add_argument('--cert', type=str,
|
||||
help="Certfile to use for SSL")
|
||||
parser.add_argument('-a', '--auth', type=str, metavar='user:password', \
|
||||
parser.add_argument('-a', '--auth', type=str, metavar='user:password',
|
||||
help="Set user and password for HTTP basic authentication")
|
||||
parser.add_argument('--realm', type=str, default=None,\
|
||||
parser.add_argument('--realm', type=str, default=None,
|
||||
help="Set a realm for HTTP basic authentication")
|
||||
parser.add_argument('-t', '--tar', action="store_true", default=False, \
|
||||
parser.add_argument('-t', '--tar', action="store_true", default=False,
|
||||
help="Enable on the fly tar creation for given file or directory. Note: Download continuation will not be available")
|
||||
parser.add_argument('-c', '--compression', type=str, metavar='method', \
|
||||
default="none", \
|
||||
parser.add_argument('-c', '--compression', type=str, metavar='method',
|
||||
default="none",
|
||||
help="Set compression method, only in combination with --tar. Can be one of %s" % ", ".join(TarFileHandler.compressionMethods))
|
||||
parser.add_argument('-4', '--ipv4-only', action="store_true", default=False, \
|
||||
parser.add_argument('-4', '--ipv4-only', action="store_true", default=False,
|
||||
help="Listen on IPv4 only")
|
||||
parser.add_argument('-6', '--ipv6-only', action="store_true", default=False, \
|
||||
parser.add_argument('-6', '--ipv6-only', action="store_true", default=False,
|
||||
help="Listen on IPv6 only")
|
||||
|
||||
args = parser.parse_args()
|
||||
|
@ -1156,7 +1163,7 @@ def main():
|
|||
sys.exit(1)
|
||||
|
||||
if args.max_upload_size:
|
||||
sizeRe = re.match("^(\d+(?:[,.]\d+)?)(?:([bkmgtpe])(?:(?<!b)b?)?)?$", args.max_upload_size.lower())
|
||||
sizeRe = re.match(r"^(\d+(?:[,.]\d+)?)(?:([bkmgtpe])(?:(?<!b)b?)?)?$", args.max_upload_size.lower())
|
||||
if not sizeRe:
|
||||
print("Error: Your max upload size param is broken. Try something like 3M or 2.5Gb.")
|
||||
sys.exit(1)
|
||||
|
@ -1254,4 +1261,3 @@ def main():
|
|||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
|
||||
|
|
Loading…
Reference in New Issue