Paramiko failes with Authentication timeout when using it with mod_wsgi

  apache, mod-wsgi, paramiko, python

I’m using a paramiko to ssh to an external server from django. In debug mode there’s no problem, all connections are ok. Once in production in the apache24 with mod_wsgi the paramiko authentication times out

is there anything there missing or something that needs to be changed? I can’t figure it out and can’t find any relevant information in the web

mod-wsgi 4.9.0

Django 3.2.5

paramiko 2.7.2

python 3.9

apache2.4.46

views.py

import paramiko
from paramiko import BadHostKeyException, AuthenticationException, SSHException,ChannelException

host = "hostname"
port = 22
username = "username"
password = "password"
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
command = "ls -l"
try:
    ssh.connect(host, port, username, password)
    print(datetime.datetime.now(), 'Connected to remote end', ssh ) #this never shows in apache logs
    stdin, stdout, stderr = ssh.exec_command(command)
    lines = stdout.readlines()
    print(lines)
except (BadHostKeyException, AuthenticationException, SSHException,
        ChannelException) as ex:
    print(ex)

apache-error.log

[Thu Oct 14 17:33:16.135511 2021] [wsgi:error] [pid 24700:tid 1996] [client 127.0.0.1:52744] Authentication timeout.r, referer: http://newsite.com/thisview/

httpd-vhost.conf

<VirtualHost *:80>
    LoadModule wsgi_module "C:/virtualenv/Lib/site-packages/mod_wsgi/server/mod_wsgi.cp39-win_amd64.pyd"
    WSGIApplicationGroup %{ENV:ONE_GROUP}
    ServerName newsite.com
    WSGIScriptAlias / "C:/virtualenv/proj/proj/wsgi.py" application-group=%{ENV:ONE_GROUP}
    WSGIImportScript "C:/virtualenv/proj/proj/wsgi.py" application-group=%{ENV:ONE_GROUP}
    
    <Directory "C:/virtualenv/proj/proj">
        <Files wsgi.py>
            Require all granted
        </Files>
    </Directory>

    Alias /templates "C:/virtualenv/proj/templates"
    <Directory "C:/virtualenv/proj/templates">
            Require all granted
    </Directory>
    
    Alias /static "C:/virtualenv/proj/mainsite/static"
    <Directory "C:/virtualenv/proj/mainsite/static">
            Require all granted
    </Directory>common

</VirtualHost>

Source: Python Questions

LEAVE A COMMENT