Introduced argparse
This commit is contained in:
parent
c186d5f695
commit
6a2fea1465
47
servefile
47
servefile
|
@ -4,6 +4,7 @@
|
||||||
# written by seba (seba-geek.de) :)
|
# written by seba (seba-geek.de) :)
|
||||||
# v0.3
|
# v0.3
|
||||||
|
|
||||||
|
import argparse
|
||||||
import BaseHTTPServer
|
import BaseHTTPServer
|
||||||
import commands
|
import commands
|
||||||
import urllib
|
import urllib
|
||||||
|
@ -82,44 +83,40 @@ class ThreadedHTTPServer(SocketServer.ThreadingMixIn, BaseHTTPServer.HTTPServer)
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
if len(sys.argv) < 2 or len(sys.argv) > 3 or sys.argv[1] in ('-h', '--help'):
|
parser = argparse.ArgumentParser()
|
||||||
print "Usage: [path to file] <port=8080>"
|
parser.add_argument('filename', metavar='file/directory', type=str)
|
||||||
return
|
parser.add_argument('-p', '--port', type=int, default=8080, \
|
||||||
|
help='port to listen on')
|
||||||
port = 8080
|
|
||||||
if len(sys.argv) == 3:
|
|
||||||
try:
|
|
||||||
port = int(sys.argv[2])
|
|
||||||
except ValueError:
|
|
||||||
print "Error: Port should be an int!"
|
|
||||||
return
|
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
testit = open(sys.argv[1], 'r')
|
testit = open(args.filename, 'r')
|
||||||
testit.close()
|
testit.close()
|
||||||
FileHandler.filePath = sys.argv[1]
|
FileHandler.filePath = sys.argv[1]
|
||||||
FileHandler.fileName = os.path.basename(sys.argv[1])
|
FileHandler.fileName = os.path.basename(args.filename)
|
||||||
FileHandler.fileLength = os.stat(sys.argv[1])[ST_SIZE]
|
FileHandler.fileLength = os.stat(args.filename)[ST_SIZE]
|
||||||
except IOError:
|
except IOError:
|
||||||
print "Error: Could not open file!"
|
print "Error: Could not open file!"
|
||||||
return
|
sys.exit(3)
|
||||||
|
|
||||||
server = ThreadedHTTPServer(('', port), FileHandler)
|
server = ThreadedHTTPServer(('', args.port), FileHandler)
|
||||||
print "Serving:", sys.argv[1]
|
print "Serving \"%s\" under port %d" % (args.filename, args.port)
|
||||||
print "Port: ", port
|
|
||||||
print "Ready..."
|
|
||||||
server.address_family = 10
|
|
||||||
# print urls with local network adresses
|
# print urls with local network adresses
|
||||||
print "Some addresses this file will be available under:"
|
print "\nSome addresses this file will be available under:"
|
||||||
ips = commands.getoutput(r"/bin/ip addr|sed -n -e 's/.*inet6\? \([0-9.a-fA-F:]\+\)\/.*/\1/ p'|grep -v '^fe80\|^127.0.0.1\|^::1'")
|
|
||||||
|
ips = commands.getoutput(r"/bin/ip addr|" + \
|
||||||
|
"sed -n -e 's/.*inet6\? \([0-9.a-fA-F:]\+\)\/.*/\\1/ p'|" + \
|
||||||
|
"grep -v '^fe80\|^127.0.0.1\|^::1'")
|
||||||
for ip in ips.split("\n"):
|
for ip in ips.split("\n"):
|
||||||
if ip.find(":") >= 0:
|
if ip.find(":") >= 0:
|
||||||
# ipv6
|
# ipv6
|
||||||
ip = "[%s]" % ip
|
ip = "[%s]" % ip
|
||||||
# FIXME: When BaseHTTP supports ipv6 properly, delete this line
|
# FIXME: When BaseHTTP supports ipv6 properly, delete this line
|
||||||
continue
|
continue
|
||||||
print "http://%s:%d/" % (ip, port)
|
print "http://%s:%d/" % (ip, args.port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
except KeyboardInterrupt:
|
except KeyboardInterrupt:
|
||||||
|
|
Loading…
Reference in New Issue