简体   繁体   English

apache服务器上的ssh连接python mod_wsgi flask

[英]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. 我在带有mod_wsgi的Apache服务器中启动了带有Flask的python rest API。 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. 它打开了与其他计算机的ssh连接,以发送一些信息并在其中使用它,并且在我更改服务器(使它在Werkzeug服务器上运行)之前,一切正常。 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. 但是现在看来,它可以工作,但是当我对服务器执行get请求时,它冻结并且不执行任何操作,没有日志,没有错误,没有任何反应。

init .py 初始化 .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. 当它应该启动ssh隧道时,它在最后一行停止。

Some info: Apache version 2.4 Python version 2.7 一些信息:Apache版本2.4 Python版本2.7

Launched on Ubuntu VM and child nodes on Ubuntu server 在Ubuntu VM和Ubuntu服务器上的子节点上启动

Under Apache your code isn't going to run as you, but as Apache user. 在Apache下,您的代码不会以您的身份运行,而是以Apache用户的身份运行。 If you are relying on SSH configuration/keys in your user home account, then it isn't going to work. 如果您依靠用户主帐户中的SSH配置/密钥,那么它将无法正常工作。

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. ./config相对路径也不会起作用,因为使用mod_wsgi运行应用程序的进程的当前工作目录不会在您的代码中。 Try calculating the location of the config file as an absolute path. 尝试计算配置文件的位置作为绝对路径。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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