简体   繁体   中英

ssh connection on apache server python mod_wsgi flask

I have a python rest API with Flask launched in an Apache server with mod_wsgi. It opens some ssh connections with other machines to send some info and work with it in there, and before I changed my server ( I had it running on a Werkzeug server) it was working all fine. But now it seems it works but when I do a get request to my server, it freezes and does nothing, no log, no error, nothing.

init .py

from flask import Flask, jsonify,make_response,abort,g,request,url_for
from flask_httpauth import HTTPBasicAuth
from flask_sqlalchemy import SQLAlchemy
from flask_bson import accept_bson, bsonify
import base64
from controler import Controler
...
...
@app.route('/api/register', methods=['POST'])
def new_user():

    try:
        username = request.json.get('username')
        password = request.json.get('password')
    except:
        abort(400)


    user = users.new_user(username,password)
    return (jsonify({'username': user.username,'uri':url_for('get_user', username=user.username, _external=True)}), 201)
...
...

Controler.py

class Controler():   
ips = 2 #number of machines in the distributed service
    def _init_(self):

    self.con = []#ssh conn
    self.data = []#conn metadata
    self.__initConn()

def __initConn(self):

    i = 0
    config = SSHConfig()
    config.parse(open('./config'))

    for j in range(self.ips):
        i += 1
        o = config.lookup('server' + str(i)) 
        params = dict (
            ssh_address=o['ip'],
            ssh_port=22,
            ssh_host_key=None,
            ssh_username=o['user'],
            ssh_password=o['password'],
            ssh_private_key=None,
            remote_bind_address=('127.0.0.1', 5555),
        )
        a = open_tunnel(**params)
        a.start()#starting tunnel. *************************

It stops in the last line, when it should start the ssh tunnel.

Some info: Apache version 2.4 Python version 2.7

Launched on Ubuntu VM and child nodes on Ubuntu server

Under Apache your code isn't going to run as you, but as Apache user. If you are relying on SSH configuration/keys in your user home account, then it isn't going to work.

That relative path of ./config also isn't going to work as the current working directory of the processes running your application using mod_wsgi is not going to be where your code is. Try calculating the location of the config file as an absolute path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM