Fixes and additional methos for ip-gathering
This commit is contained in:
parent
929da24f4c
commit
be8ad72b31
41
servefile
41
servefile
|
@ -14,6 +14,7 @@ import os
|
||||||
import SocketServer
|
import SocketServer
|
||||||
import socket
|
import socket
|
||||||
from stat import ST_SIZE
|
from stat import ST_SIZE
|
||||||
|
from subprocess import Popen, PIPE
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
class FileHandler(BaseHTTPServer.BaseHTTPRequestHandler):
|
||||||
|
@ -108,16 +109,36 @@ def main():
|
||||||
# print urls with local network adresses
|
# print urls with local network adresses
|
||||||
print "\nSome addresses this file will be available under:"
|
print "\nSome addresses this file will be available under:"
|
||||||
|
|
||||||
ips = commands.getoutput(r"/bin/ip addr|" + \
|
# ip and ifconfig sometimes are located in /sbin/
|
||||||
"sed -n -e 's/.*inet6\? \([0-9.a-fA-F:]\+\)\/.*/\\1/ p'|" + \
|
os.environ['PATH'] += ':/sbin:/usr/sbin'
|
||||||
"grep -v '^fe80\|^127.0.0.1\|^::1'")
|
proc = Popen(r"ip addr|" + \
|
||||||
for ip in ips.split("\n"):
|
"sed -n -e 's/.*inet6\? \([0-9.a-fA-F:]\+\)\/.*/\\1/ p'|" + \
|
||||||
if ip.find(":") >= 0:
|
"grep -v '^fe80\|^127.0.0.1\|^::1'", shell=True, stdout=PIPE)
|
||||||
# ipv6
|
if proc.wait() != 0:
|
||||||
ip = "[%s]" % ip
|
# ip failed somehow, falling back to ifconfig
|
||||||
# FIXME: When BaseHTTP supports ipv6 properly, delete this line
|
oldLang = os.environ.get("LC_ALL", None)
|
||||||
continue
|
os.environ['LC_ALL'] = "C"
|
||||||
print "http://%s:%d/" % (ip, args.port)
|
proc = Popen(r"ifconfig|" + \
|
||||||
|
"sed -n 's/.*inet6\? addr: \?\([0-9a-fA-F.:]*\).*/" + \
|
||||||
|
"\\1/p'|" + \
|
||||||
|
"grep -v '^fe80\|^127.0.0.1\|^::1'", \
|
||||||
|
shell=True, stdout=PIPE, stderr=PIPE)
|
||||||
|
if oldLang:
|
||||||
|
os.environ['LC_ALL'] = oldLang
|
||||||
|
else:
|
||||||
|
del(os.environ['LC_ALL'])
|
||||||
|
if proc.wait() != 0:
|
||||||
|
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
|
||||||
|
# FIXME: When BaseHTTP supports ipv6 properly, delete this line
|
||||||
|
continue
|
||||||
|
print "http://%s:%d/" % (ip, args.port)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
server.serve_forever()
|
server.serve_forever()
|
||||||
|
|
Loading…
Reference in New Issue