Python connection refused from localhost when runningn on cron - python

This is funny because most questions I found are the other way around.
I have a flask server that I start on debugging mode, listening on port 6000.
I have a script that accesses the url through localhost which sends back a JSON reply.
When I do an ssh tunnel using ssh -L and run the script that sends a request in my machine to localhost everything works fine. When I run the script directly from the server it also works fine. However, since I want to ultimately use a big data set, I setup a cron job. When the cron runs, the script crashes with connection refused error. I can see that the server is running after the script crashes and the port is correct on both.
I have no idea why this can happen. Any ideas are appreciated.
Solution
I couldn't figure out the reason or how to solve it, so I decided to run manually and put it in the background.
How to make a programme continue to run after log out from ssh?

Related

When stopping SSHTunnel server the application hangs

I have very simple script that establishes connection to another host and then creates the tunnel through which another application connects to SQL Server.
self.dicSession['server'] = sshtunnel.SSHTunnelForwarder(
('remote_host', 22),
ssh_username="username",
ssh_password="password",
ssh_private_key="key_rsa",
remote_bind_address=('remote_host', 53425),
local_bind_address=('localhost', 1433),
)
self.dicSession['server'].start()
It works fine. However the command to stop the server. When the connection is established I am using tunnel (between 53425 port of remote host and 1433 port of localhost) to connect to a SQL Server that operate on the remote host. Up to this point everything works fine. It seems however that stopping the SSH connection with command
self.dicSession['server'].stop()
does not work properly. Despite the command to stop the connection I am still able to execute SQL statement and get results. Moreover when I execute the SQL query after the command
self.dicSession['server'].stop()
the app - which us build with usage of PyQT5 - freezes. To be honest I have not tried to run the script without GUI but I will need the GUI as the app is not only for me but for other people who are not familiar with CLI.
This happens because your client is still open.
It will allow you to stop server, once you closed your client.

Windows 10 not allowing Python server.py to connect with client.py

I'm working a tutorial in Python 3.8 that involves sockets and networking. There is a server.py file and a client.py file. I took example code straight out of the Python doc for sockets to see if that would work, and it does not. The server starts and creates a socket and listens for the connection, but I get WinError 10061, the one where the target machine refuses the connection. My OS is Windows 10 and I'm using IDLE. I've looked at my Firewall and set a permission for pythonw.exe to be allowed through, but that has not helped. Anybody have any fixes for me to try? I can't really proceed until I can get the client and server connected.
I think I know what I’ve been doing wrong. I have been running both server and client files in the same console. I think I have to open two consoles and run one file in each so they can communicate.
(Doh!)
I’m at work so I can’t test it right now. Just in case anyone else has been befuddled by this .
Yes, I did not realize that each file had to run in its own instance of IDLE, but that makes perfect sense now. A socket won’t connect to itself!

How to connect to ssh server from another server

I've been trying to connect to first an out-of-band management server (in my case, since I'm connecting to DELL-servers, an iDRAC) and through that connect to the main server itself. I've got it to work when I do it manually, using, in the (windows) terminal:
putty.exe -ssh 'username'#'iDRAC-IP'
followed by PuTTY window opening where I type in the password, followed by
connect
which connects to the server itself, and then I type in the username and password for the server, completing the process.
When I've been writing my script in python, I'm using paramiko, http://www.paramiko.org/, suggested here on stackoverflow, and following this example: https://www.ivankrizsan.se/2016/04/24/execute-shell-commands-over-ssh-using-python-and-paramiko/, and it works just splendid for the iDRAC (the first server I connect to). It also works when I type in
stdin, stdout, stderr = ssh_client.exec_command('connect')
because I am still in my first server (ssh_client) (I can tell this is working because when I try to connect to the server manually afterwards, it is occupied). But after that it stops working, since when doing 'connect' I am no longer in ssh_client, but in a different server.
So my question is - how do I connect to a server from another server (in this case being the out-of-band management server) and log in to this one?
You can use ssh tunnel to do so.
this post may resolve your problem:
PyCharm: Configuring multi-hop remote Interpreters via SSH

Socket programming python on real server

I'm following this http://www.raywenderlich.com/3932 for socket programming in iOS where the server coding is in PYTHON, however, I just want to know that according to this tutorial, the author used localhost and run the code from terminal such that python server.py to execute and listen for socket.
What I'm confusing is that, how can I make this command on real server, such that after putting the code of python in CGI-BIN, how can I run that from shell/terminal of a shared web hosting.
Here's my SSH Screenshot, where I tried to run that command to bind and listen for socket, but Here i'm failed as no JAVA LOGIN section is appearing in my case as the video tutorial shows.
My Question is, How can I run the command so that the server will listen for the port, as on my localhost.
The command is: python server.py
On a shared web hosting server you probably have a running web server for which you write scripts which generate some output for the web server to return to the client.
server.py however is no such script. It contains the code for an actual server. Running the command starts the server. Therefore you won't get this working by simply putting the file in a CGI-BIN folder. You do need to run the command.

Connection interrupted when run "indirectly". Bottle.py on Ubuntu

I'm running a local web service on Ubuntu on localhost:8090, written with bottle.py.
The connection uses SSL.
If I execute the main.py file from Nautilus or the terminal and connect to https://localhost:8090 everything works fine.
When I execute it from a link to the file, an .sh script or a .desktop file the server starts running fine, but when I browse to the address firefox says "The connection to localhost:8090 was interrupted while the page was loading"
$telnet 127.0.0.1 8090 gives this:
Trying 127.0.0.1...
Connected to 127.0.0.1...
Escape character is '^]'.
Connection closed by foreign host.
$sudo netstat -ntlupp | grep 8090 gives this:
tcp 0 0 127.0.0.1:8090 0.0.0.0:* LISTEN
iptables is default
I've got the feeling it's blocking the connection when the server is executed "indirectly" (link, script or .desktop), since when I actually click on the file or run it through terminal it runs fine.
I don't have a clue on where to prevent it from blocking the connection, though. Any help is greatly appreciated.
Any workaround will do, even just pretending the file is being run directly from the user.
Thanks in advance
Watch the server logs.
The major difference between the different methods of invocation probably is the current working directory.
I think that it is unlikely that the network configuration is involved in what you are observing.
Depending on the complexity of your web application it might be that a Python import fails if the main script is not run from the right directory. This would trigger a Python exception, which might lead to an immediate connection reset. I have not worked with bottle, but other Python web frameworks distinguish a development mode in which Python tracebacks are shown in the browser, and a production mode in which an HTTP error is sent to the client.
This is what you should do in order to debug your issue: run your server from a terminal (cd to the right directory, then run python application.py). Carefully watch stdout and stderr of that server process while connecting to the web application with your browser.
Ok, problem solved.
It was actually depending on the current working directory not being the same as the python file running the WSGI server.
If I run the .sh script or the link from the same directory everything works fine, and if I give a cd command in the script everything works smoothly.
Thanks for the help Jan-Philip!

Categories