0

i want to create a simple web application who use a connection to a vCenter Server, and i want to pass the variable connection between pages, instead of recreate this connection on every page.

This is the code:

#!/bin/env python
from flask import Flask, request, redirect, render_template, session
from flask import Flask, request, redirect, render_template, session
from flask_wtf import FlaskForm
from wtforms import StringField, PasswordField
from wtforms.validators import DataRequired
from modulo import MyForm
from pyVim import connect
from pyVim.connect import SmartConnectNoSSL, Disconnect
from pyVmomi import vim


app = Flask(__name__)



@app.route('/')
def index():
 return redirect('/submit')

@app.route('/submit', methods=('GET', 'POST')) #ENTER USERNAME AND PASSWORD, SAVE ON /SUCCESS
def submit():
    form = MyForm()
    if form.validate_on_submit():
        return redirect('/success')
    return render_template('submit.html', form=form)

@app.route('/success', methods=('GET', 'POST'))    #ESTABILISH CONNECTION USING USERNAME AND PASSWORD CREDENTIALS
def success():
  form = MyForm()
 username = form.username.data
 password = form.password.data
 c = SmartConnectNoSSL(host='10.116.xxx.xxx', user=username, pwd=password)
 datacenter = c.content.rootFolder.childEntity[0]
 clusters = datacenter.hostFolder
 cluster = clusters.childEntity[0]
 esxi = cluster.host
 return render_template('success.html', esxi=esxi)

@app.route('/hosts', methods=('GET', 'POST'))
def hosts():
 macchine = request.form.getlist('host')
 for i in esxi: 
  for x in macchine:
   if i.name == x:
    do something..
     return FINISH


if __name__ == '__main__':
    app.secret_key='Secret'
    app.debug = True
    app.run(host = '0.0.0.0', port = 3000)

i want to reuse the c variable (the connection to the server) in other pages, and the object who derived from this variable for example esxi (list of object).

if i run the code, flask say: global name 'esxi' is not defined

How can i do this?

  • Move from `c = SmartConnect....` to `esxi = ....` block outside of function, after import statements. So that `esxi` will be available in all functions – REDDY PRASAD Jun 17 '18 at 16:31
  • But the c = SmartConnect.. use the variables username and password derived from submit.html. – GabrielMeg Jun 17 '18 at 17:14
  • You can use `session` , depending on how big the size of `esxi` is – Mekicha Jun 17 '18 at 17:16
  • @REDDYPRASAD It seems like you are suggesting to make the variable a global variable? While technically that may solve the problem not sure that is a good solution. If you have multiple users of this app now other users can potentially access the contents of this global variable that may contain the results of another user. Don't think that is what you'd want. Imagine that this variable contained search results would you want other user to see search results of another user? – user2101068 Jul 01 '20 at 18:53

1 Answers1

1

In flask you can store variables on your app object and reuse them later. Example:

    app = Flask(__name__)
    app.c = SmartConnectNoSSL(host='10.116.xxx.xxx', user=username, pwd=password)

    # You can now reuse the connection like so
    datacenter = app.c.content.rootFolder.childEntity[0]
haibeey
  • 41
  • 1
  • 1