
from flask import Flask, request, render_template, redirect, url_for
db = {}
app = Flask(__name__)

@app.route('/')
def get():
    ip = request.remote_addr
    return render_template("index.html", text = db.get(ip, ''))

@app.route('/', methods=['POST'])
def post():
    ip, content = request.remote_addr, request.form.get('text')
    if len(content) == 0:
        del db[ip]
    else:
        db[ip] = content
    return redirect(url_for('get'))

if __name__ == "__main__":
    app.run(host="0.0.0.0")

from flask import Flask, request, render_template, redirect, url_for
db = {}
app = Flask(__name__)

def get_client_ip(request):
    # PythonAnywhere.com calls our service through a loabalancer
    # the remote_addr is therefore the IP of the loaabalancer, PythonAnywhere stores Client IP in header
    if request.headers['X-Real-IP']: return request.headers['X-Real-IP']
    return request.remote_addr


if __name__ == "__main__":
    app.run(host="0.0.0.0")

@app.route('/')
def get():
    ip = get_client_ip(request)
    return render_template("index.html", text = db.get(ip, ''))

@app.route('/', methods=['POST'])
def post():
    ip, content = get_client_ip(request), request.form.get('text')
    if len(content) == 0:
        del db[ip]
    else:
        db[ip] = content
    return redirect(url_for('get'))

if __name__ == "__main__":
    app.run(host="0.0.0.0")
Fix for PythonAnywhere LoadBalancer IP
Fix for PythonAnywhere LoadBalancer IP

  
  from flask import Flask, request, render_template, redirect, url_for
  db = {}
  app = Flask(__name__)
  
+ def get_client_ip(request):
+     # PythonAnywhere.com calls our service through a loabalancer
+     # the remote_addr is therefore the IP of the loaabalancer, PythonAnywhere stores Client IP in header
+     if request.headers['X-Real-IP']: return request.headers['X-Real-IP']
+     return request.remote_addr
+ 
+ 
+ if __name__ == "__main__":
+     app.run(host="0.0.0.0")
+ 
  @app.route('/')
  def get():
-     ip = request.remote_addr
+     ip = get_client_ip(request)
      return render_template("index.html", text = db.get(ip, ''))
  
  @app.route('/', methods=['POST'])
  def post():
-     ip, content = request.remote_addr, request.form.get('text')
+     ip, content = get_client_ip(request), request.form.get('text')
      if len(content) == 0:
          del db[ip]
      else:
          db[ip] = content
      return redirect(url_for('get'))
  
  if __name__ == "__main__":
      app.run(host="0.0.0.0")
Fix for PythonAnywhere LoadBalancer IP
## Code Before:

from flask import Flask, request, render_template, redirect, url_for
db = {}
app = Flask(__name__)

@app.route('/')
def get():
    ip = request.remote_addr
    return render_template("index.html", text = db.get(ip, ''))

@app.route('/', methods=['POST'])
def post():
    ip, content = request.remote_addr, request.form.get('text')
    if len(content) == 0:
        del db[ip]
    else:
        db[ip] = content
    return redirect(url_for('get'))

if __name__ == "__main__":
    app.run(host="0.0.0.0")
## Instruction:
Fix for PythonAnywhere LoadBalancer IP
## Code After:

from flask import Flask, request, render_template, redirect, url_for
db = {}
app = Flask(__name__)

def get_client_ip(request):
    # PythonAnywhere.com calls our service through a loabalancer
    # the remote_addr is therefore the IP of the loaabalancer, PythonAnywhere stores Client IP in header
    if request.headers['X-Real-IP']: return request.headers['X-Real-IP']
    return request.remote_addr


if __name__ == "__main__":
    app.run(host="0.0.0.0")

@app.route('/')
def get():
    ip = get_client_ip(request)
    return render_template("index.html", text = db.get(ip, ''))

@app.route('/', methods=['POST'])
def post():
    ip, content = get_client_ip(request), request.form.get('text')
    if len(content) == 0:
        del db[ip]
    else:
        db[ip] = content
    return redirect(url_for('get'))

if __name__ == "__main__":
    app.run(host="0.0.0.0")