python socket doesn't work after restart application - python

I have a python script server->client, and for some reasons i used py2exe to make an exe from py so i can use on machines without python installed.
And i have and update system:
I send command update to server and server start a bat file:
suprocess.Popen("C:/Server/server.bat", shell=False) #
and server.bat contains:
#echo off
taskkill /f /im "dServer.exe"
echo D | xcopy /s /y \\netpath\share\Server\c:\Server
start dServer.exe
exit
First time when i start dServer.exe it's working, i can send command from client to server and recieve answer. But after update and dServer.exe starts again, server will not work, i got socket.timeout error and can't send commands.
Does anybody know why it's not working second time?
p.s I have command : server.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
Thanks!

Problem was that socket server doesn't close so quickly and second server start to fast and can't because port it's used.
I modified dServer.py and now before i call server.bat i close the socket.

Related

ERRNO 111 Connection refused when starting server over SSH

I would like to start a rpyc server on a machine I'm connected with over ssh. The general SSH connection is working but when starting the server I receive ERRNO 111 Connection refused. When starting the server manually, by logging in via SSH on the machine and starting the py file, I can connect. I tried:
ssh.exec_command("python3 /tmp/recalibration/rpc_service_pictures.py")
ssh.exec_command("python3 /tmp/recalibration/rpc_service_pictures.py &")
ssh.exec_command("nohup python3 /tmp/recalibration/rpc_service_pictures.py &")
ssh.exec_command("nohup python3 /tmp/recalibration/rpc_service_pictures.py > /dev/null 2>&1 &")
but nothing is changing the Connection Problem, any ideas?
Thanks in advance!
Turns out you cant be connected via SSH at the same time.
I had an open SSH session at the same time to debug, and because of that I couldnt connect. Seems obvious when you know it, but if you dont you are completely lost :D

Python - error with a simple socket script

So I wrote a script in Python with the module socket but finally he doesn't work. After searching a few hours for an error (I'm new in Python so it's long for me), I didn't find any so instead I wrote another simple script which works with netcat to have a reverse shell between my host machin (Linux) and my VM (Windows).
My command netcat with Linux :
nc -nvlp 4444
My script in Windows :
import socket
connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
connection.connect(("192.168.1.81", 4444))
And after start the listener (netcat), he's listening and after a few seconds, in my Windows machin, the scripts closed itself because he doesn't find anything connection.
I don't know where is the problem, my VM is on the same subnet than my host, I've flushed my iptables in Linux just in case, I'm lost :)
Thanks for your answers !
I just send the script to a friend for trying and it's working, so I think so the problem is in my settings, or somewhere else..

SSH to a server and have session running with all commands executed to have them saved in local file

I basically want to SSH to a server and then the commands executed in a server and its outputs be saved into a local file. I thought below one would save the output to a file but is exiting once commands are executed *i can understand its done with its execution so its exiting but i do not wish to exit unless i exit from the server
SSH to server via script
Save the output of all execution in server in local file
Come out , if exit found
Here is the basic code:
#! /usr/bin/bash
ssh user#server > file.log << EOF
pwd;hostname;
EOF
If you're just trying to save the output of an interactive SSH session, do this:
ssh userid#server | tee logfile
Your example implies that you want to run some commands (pwd, hostname) from your shell script, and then keep the ssh connection open for interactive use. I'd suggest instead adding those commands to your login script on the server (.profile or whatever.)
If you really want to use your method, take a look at https://unix.stackexchange.com/questions/103885/piping-data-to-a-processs-stdin-without-causing-eof-afterward

Shutdown Flask server from scheduled job

I'm running a flask server and an apscheduler background scheduler. I want the program to periodically restart itself.
I restart the application using the following shell script:
#!/bin/bash
kill $(ps aux | grep 'python' | awk '{print $2}')
sleep 5
cd "$( dirname "${BASH_SOURCE[0]}" )"
python server.py
When I start the server without this script, then execute this script from another terminal it successfully kills the other process and starts the server.
But when the scheduler calls this script, flask gives me an 'error, socket in use'. Why does executing this from another terminal shutdown the server and free the socket, but executing it from inside the scheduler leave the socket in use?
How can I make the script my scheduler runs totally shutdown my server?
1 month later and I still haven't gotten an answer to this question :(
To clarify, I do not want a workaround that makes it so I don't need to restart the server. I want the server to have the ability to restart itself.
For now I am going to use the workaround of setting the server to start on boot, and then restarting the entire computer.

Python BaseHTTP gets struck while starting nginx or other services

I am using BaseHTTP since its very simple. Please advice if I'm using it wrong.
if self.path.endswith("nginxstart"):
xxxxxx
status=os.system("/home/karthik/nginx/nginx-1.0.4/sbin/nginx"); #tried ls -l and works
self.wfile.write("nginx restarted")
nginx gets started but doesn't write "nginx restarted" to browser until I kill the python server.
When I do netstat -anp | grep 'nginx's pid' there are two listening ports :
python server was listening (port 8000: which I have killed)
on which nginx was supposed to be running.
Works great if i run simple shell commands os.system("ls -l") and so on.
Works great if I run the same as a normal python script, not as a web server.
Tried starting some other services also not working.
I tried with try catch, catch part is not getting executed.
The browser is in connecting/receiving state forever.
Any help on this please ?
Your code is stuck because os.system block until the command is finished. So if the nginx command is not going to background, the process will not end, and your code will stuck.
An alternative is to use subprocess module:
from subprocess import Popen
# start a new command
cmd = Popen("/home/.../nginx", shell=True)
# you can regulary check if the command is still running with:
cmd.poll()
if cmd.returncode is None:
print "process is still running !"
else:
print "process exited with code %d" % cmd.returncode
# or even kill it
cmd.kill()

Categories

Resources