1

I am running a script that telnets to a terminal server. Occasionally the script is launched while one instance is already running, which causes the already running script to fail with

EOFError: telnet connection closed

Is there a quick and easy and pythonic way to check if the required socket is already in use on the client computer before I try to open a connection with telnetlib?

SOLUTION:

I wanted to avoid making a subprocess call but since I do not control software on the client computers, and other programs may be using the same socket, the file lock suggestion below (a good idea) wouldn't work for me. I ended up using SSutrave's suggestion. Here is my working code that uses netstat in Windows 7:

# make sure the socket is not already in use
try:
    netstat = subprocess.Popen(['netstat','-nao'],stdout=subprocess.PIPE)
except:
    raise ValueError("couldn't launch netstat to check sockets. exiting")
ports = netstat.communicate()[0]
if (ip + ':' + port) in ports:
    print 'socket ' + ip + ':' + port + ' in use on this computer already. exiting'
    return
MrPat
  • 119
  • 1
  • 12

2 Answers2

3

You can check for open ports by running the following linux command netstat | grep 'port number' | wc -l by importing subprocess library in python.

SSutrave
  • 56
  • 4
1

There is not a standard way to know if a server has other opened connections before you attempt to connect to it. You must ask it either connecting to another service in the server that checks it, or by asking the other clients, if you know all of them.

That said, telnet servers should be able to handle more than one connection at a time, so it should not matter if there are more clients connected.

Alvaro Gutierrez Perez
  • 3,669
  • 1
  • 16
  • 24
  • The problem I'm having isn't on the server side. It's with accidentally running the script twice on the same client machine. My script calls `tn = telnetlib.Telnet(host=10.10.10.15,port=8001)`. If the script is run a second time before the first closes. The first instance gets `EOFError: telnet connection closed`. – MrPat Oct 01 '15 at 20:41
  • 1
    @MrPat that is a server side problem, as your server closes already existing connections when a new one arrives. You can workaround this with a file lock (create a file while the script is running and at start, if the file exist, it finishes without doing anything). – Alvaro Gutierrez Perez Oct 01 '15 at 20:58
  • Sadly, it is definitely a client side problem: If I run the script on computer A, and then on computer B while A is still running, the connection attempt by B is rejected by the server and A continues working great. However, if I run the script on computer A (call this A1), and then again on A (call this A2), then A1 fails and A2 starts a new connection. A2 will also fail, but for a different reason. – MrPat Oct 01 '15 at 21:25
  • @MrPat what such a strange server, maybe it only allows one connection per IP address? If the scripts do not communicate between them, it cannot be a client problem! – Alvaro Gutierrez Perez Oct 01 '15 at 23:23
  • 1
    I think I've got it nailed down: this is a terminal server, each network port (8001-8032) represents a physical serial port (1-32). Once a socket is open, the server denies connection attempts by any other host. BUT, if the server receives a second connection from the same host, it assumes there was a problem with the client and that the client restarted. The server closes the first connection and opens a new one. The server is designed to close the connection in this specific situation, so the client must check itself before trying to connect. Thanks Alvaro! – MrPat Oct 02 '15 at 18:13