slight cleaning for webserver

This commit is contained in:
ville rantanen
2018-11-20 23:00:43 +02:00
parent d8c6a73fab
commit c35fee7989
2 changed files with 14 additions and 9 deletions

View File

@@ -119,11 +119,11 @@ def add_single(conn,filename,change=False,hash=None,minsize=0,fullfile=False):
if hash==None: if hash==None:
hash=get_md5(filename,fullfile) hash=get_md5(filename,fullfile)
ftime=os.path.getmtime(filename) ftime=os.path.getmtime(filename)
mime=MIME.file(filename.encode('UTF-8')) mime=MIME.file(str(filename.encode('UTF-8')))
except IOError: except IOError:
print("File '%s' not found. Bad link?"%(filename,)) print("File '%s' not found. Bad link?"%(filename,))
return return
except UnicodeDecodeError: except (UnicodeDecodeError, TypeError):
mime="NA" mime="NA"
if change: if change:

View File

@@ -1,6 +1,6 @@
#!/usr/bin/env python #!/usr/bin/env python
import sys,os import sys, os
import BaseHTTPServer import BaseHTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler from SimpleHTTPServer import SimpleHTTPRequestHandler
@@ -16,7 +16,9 @@ def setup_options():
parser.add_argument("rootpath",type=str,action="store",default=os.path.abspath('.'),nargs='?', parser.add_argument("rootpath",type=str,action="store",default=os.path.abspath('.'),nargs='?',
help="Root path of the server") help="Root path of the server")
options=parser.parse_args() options=parser.parse_args()
if not os.path.exists(options.rootpath):
parser.error("Path does not exist")
options.rootpath = os.path.abspath(options.rootpath)
return options return options
@@ -27,6 +29,7 @@ def serve(options):
Protocol = "HTTP/1.0" Protocol = "HTTP/1.0"
server_address = (options.address, options.port) server_address = (options.address, options.port)
os.chdir(options.rootpath) os.chdir(options.rootpath)
HandlerClass.protocol_version = Protocol HandlerClass.protocol_version = Protocol
@@ -37,11 +40,13 @@ def serve(options):
try: try:
httpd.serve_forever() httpd.serve_forever()
except KeyboardInterrupt: except KeyboardInterrupt:
pass httpd.server_close()
except OSError:
httpd.server_close() httpd.server_close()
options=setup_options() if __name__ == "__main__":
serve(options) options=setup_options()
serve(options)