Python telnetlib ending session - python

If I am using Python telnetlib, is there a way to close the telnet session if that device does not support nothing to terminate telnet session, so no ctrl+something or quit or anything like that.
I need this so that I could use read.all

Network sockets let you shutdown write and/or read channels to let the other side know that you have finished that part of the conversation. For a telnet server, shutting down the write channel is an exit. It should finish sending whatever is in the send pipeline and then close the connection completely. That close is an EOF and read_all should return. So, assuming you've already got a connection called tn
tn.get_socket().shutdown(socket.SHUT_WR)
data = tn.read_all()
tn.close()

Related

How to tell when client is done sending image

I am creating a file server in python using ftp sockets where multiple clients send images to a server. This is the method I am using to receive the image on the server side:
while True:
data = client.recv(512)
if not data:
break
file.write(data)
And this is the method I am using to send the image to the server:
while True:
data = file.read(512)
if not data:
break
server.send(data)
The problem I am running into is that on the server side the while loop is never exited which means the code is either stuck in the recv call or the if statement is never true. On the client side there are no problems, the loop is exited properly. I've heard that the client side will send something to the server to tell it to stop but the if statement doesn't seem to pick it up. How can I get the server to stop trying to receive without closing the connection?
https://docs.python.org/2/howto/sockets.html#disconnecting
On the client, close the socket to the server. On the server, check whether the recv returned 0 bytes.
Also from the documentation for using a socket:
When a recv returns 0 bytes, it means the other side has closed (or is
in the process of closing) the connection. You will not receive any
more data on this connection. Ever. You may be able to send data
successfully; I’ll talk more about this later.
Data will never be nothing unless the client closes the connection.
server.close()

Killing python program in Windows gracefully

I am running a Python script in Windows which is basically a simple UDP server. Most of the time the program will wait on recv_data, addr = server_socket.recvfrom(2048). I have two issues:
I am not able to kill this program without closing the console. This is not a big issue as I can close the console and restart the program. But still...
Even when I close the console, most of the time, the socket is not getting closed properly. I could see the program in a killed state. So I have to change the port every time I restart. Even when I do not see the program in killed state and holding the socket, the server is not able to receive any packets. (And yes I am using SO_REUSEADDR for my server socket.)
Any ideas?

Python Subprocess readline hangs() after reading all input

I am trying to readlines from the tcp server that I ran in the same script. I am able to send one command and reads its output but at the end (after reading all outputs) it looks like that program hangs in readline, I have tried almost all the solutions here and here but still it hangs.
Most of these solutions propose to check if output of readline is none or not but in my case program never returns from last read and just hangs in there.
tcp server is not in my control, or say I just have to test server script therefore I can not modify it. Also, is it possible to send commands to runing server using python without using subprocess? any better alternative?
def subprocess_cmd(command):
process=subprocess.Popen(command,stdin=subprocess.PIPE,stderr=subprocess.STDOUT,stdout=subprocess.PIPE, shell=True)
for cmd in ['python3 -u tcp_server.py 123 port1']:
subprocess_cmd(cmd)
process.stdin.write('command like print_list')
process.stdin.flush()
while True:
line=process.stdout.readline()
if line == '':
break
readline hangs because your TCP connection is still open and readline expects more data to come in. You must close the connection from server side to notify readline that there is nothing more to read. Usually it is done by closing socket on client side notifying the server that there will not be any more requests to it. When server finishes processing all your commands it closes socket too. And this is the signal for you that you have received all the data that server sent to you.
Or, alternatively, if you don't want to close the connection, you must invent delimiters which will mark end of response. So the client will stop calling readline when such delimiter is read.

Why is exit() or exec needed after telnetlib read_all()

A lot of resources, including the example in the official documentation at telnetlib suggest that at the end before you do a read_all(), you need to write exit after the command as:
tn.write("ls\n")
tn.write("exit\n")
Can someone please help me understand why is this needed ?
If I try doing it without the exit, the telnet connection hangs (or at least looks like it is hung) as the output of the command executed does not show on the terminal.
Also, another way of making it work, as I found in some resources was to use 'exec' to fire up the command and then you don't need the exit thing anymore.
Please help me understand this as well.
read_all() reads all the output until EOF. In other words, it waits until remote server closes connection and returns you all the data it has sent. If you have not previously notified the server with an "exit" command that you have no more commands for it, it will wait for them. And a deadlock occurs: you are holding open connection because you are waiting for server to tell you that it has sent everything it intended to say, and server waits for new orders from you and is ready to add more data to it's output.

Why does this twisted server interact wrongly with a process?

The server is at https://github.com/EmeraldHaze/Socketd/blob/master/Serv.py ; the process is at https://github.com/EmeraldHaze/QFTSOM/blob/master/main.py
A client too test this is at http://www.kongregate.com/games/EmeraldHaze/this-is-why-we-have-maps ; port forwarding and whatnot is set up correctly.
The point is that someone connecting too the server sends something like {"IP":"123.456.789.012"}, then a process is made for him, then the IO streams of the process and the user are connected. The reality is that the process outputs something, the user sees it, the user gives some input, the server gets it (and logs it), then nothing happens. Any ideas why? The buffers should be flushed.
Uh, I solved this. It was becouse sys.stdin.readline() stops blocking when it gets a \n, but either twisted or the client strip them off, meaning it will block indefinitly despite getting input.

Categories

Resources