apache - Flask Request within request fails on production WSGI environment -


i making request within request, in order accommodate service oriented design. locally, code works. on production using ubuntu libapache2-mod-wsgi-py3, nested request fails.

a key point both services exist within same <virtualhost> on server, suspect threading issue.

the second request called within newitem() fails connectionrefusederror(111, 'connection refused') on wsgi ubuntu box though api endpoint functions expected when post outside of context (for example curl post exact url).

another important point request within reqest design works on local when have 2 separate flask apps running on different ports. had exact issue on local until ran api server --threaded flag.

my server configured follows:

app2.wsgi:

#!env/bin/python3 import sys, os sys.path.append('/var/www/api_app')  app import create_app flask.ext.script import manager flask.ext.migrate import migrate, migratecommand  application = create_app(os.getenv('flask_config') or 'default')  if __name__ == "__main__":     manager.run() 

apache2/sites-available/default.conf:

    <virtualhost *:80>         documentroot /var/www/html          # ------ wsgi config ------         wsgidaemonprocess app1 user=www-data group=www-data threads=5         wsgidaemonprocess app2 user=www-data group=www-data threads=5         wsgiscriptalias /front_end /var/www/front_end/app2.wsgi         wsgiscriptalias /api_app /var/www/api_app/app2.wsgi         wsgiscriptreloading on              <directory /var/www/api_app>                     wsgiprocessgroup app2                     wsgiapplicationgroup %{global}                     order deny,allow                     allow             </directory>             <directory /var/www/front_end>                     wsgiprocessgroup app1                     wsgiapplicationgroup %{global}                     order deny,allow                     allow             </directory> </virtualhost> 

the following code works great on local environment when start api service --threaded flag fails on wsgi apache2 on requests.post() line.

@plant_material.route('/plant_material/new', methods=['get', 'post']) def newitem():     form = plantmaterialform()     if form.validate_on_submit():         api_uri = current_app.config['api_server_uri']         url = api_uri + "api/plant_material/new"         headers = {'content-type': 'application/json'}         print(request.form)         r = requests.post(url, data=json.dumps(request.form), headers=headers)         if r.status_code == 200:             return redirect(url_for('plant_material.index'))         elif r.status_code == 401:             flash('whiteboard api unauthorized')         elif r.status_code == 403:             flash('whiteboard api forbidden')         elif r.status_code == 500:             flash('internal server error')     return render_template('plant_material/new.html', form=form) 

how can make request within request on production server?

apache , mod_wsgi take care of concurrency here; there no reason well-formed request 1 wsgi app on same server fail here.

  • make sure there no firewall blocking requests server localhost. test same url command line curl example.
  • triple-check assumptions; use live value of current_app.config['api_server_uri'] when testing if connections work, not assume be.
  • swap out http methods; try running curl subprocess call, or use urllib2 access api

Comments