import base64 import qrcode import argparse import flask; from flask import request import socket import sys import io def localwhoami(port): site = flask.Flask(__name__) my_ip = socket.gethostbyname(socket.gethostname()) print(my_ip) my_url = f'http://{my_ip}:{port}' url_qr = qrcode.make(my_url, border=0) png_bytes = io.BytesIO() url_qr.save(png_bytes, format='png') png_bytes.seek(0) png_bytes = png_bytes.read() png_base64 = base64.b64encode(png_bytes).decode('ascii') @site.route('/') def root(): ip = request.remote_addr if ip == '127.0.0.1': ip = my_ip return f'''
You are
{ip}
{my_url}
''' site.run(host='0.0.0.0', port=port) def localwhoami_argparse(args): return localwhoami(port=args.port) def main(argv): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('port') parser.set_defaults(func=localwhoami_argparse) args = parser.parse_args(argv) return args.func(args) if __name__ == '__main__': raise SystemExit(main(sys.argv[1:]))