How to end TCP socket data while using readline()?
cs at zip.com.au
Sat Feb 27 05:11:15 CET 2010
On 26Feb2010 10:39, Arjun <arjun.chennu at gmail.com> wrote:
| Hi, I have a small script that runs a TCP server. A client connects to
| this server and transmits a stored file line-by-line, and then waits
| for a confirmation "done". However, when I run them the first loop
| never really ends -- as the TCP server keeps expecting more data. I am
| using a file-like-object, and so somehow I have to communicate to the
| server that it is the end-of-file.
| here is some server code
| sock1.bind(('', port))
| print "Listening at port: ", port
| sock1.listen(1) # listening socket
| (conn, addr) = sock1.accept() # connected socket
| print 'Client (localhost) port: ', addr
| cf = conn.makefile('r',0) # file like obj for socket
| print 'close'
Too many files. It's not that hard! Or shouldn't be.
| So what I am wondering is:
| 1. Using a file-like object means that the socket becomes uni-
| directional, until the mode of the file object is changed from 'r' to
| 'w' (or vice versa). This seems inefficient, and rather unPythonesque.
| Can somebody tell me if there is a more elegant way of receiving all
| the lines from the client, and then sending a "done" message to the
Get the socket. It is a file descriptor (or in may be a python "socket"
object with a file descriptor inside).
Open _two_ "file" objects on it using
from_file = os.fdopen(fd_of_socket, "r")
to_file = os.fdopen(fd_of_socket, "w").
Use the same:
print >>to_file, 'close'
method as you're using already.
Read from to_file as needed.
The same scheme should work in both server and client:
Don't look for EOF, watch the input line flow.
You might need to use readline() instead of the file-by-line iteration stuff,
which I seem to recall has some sort of problem handing out the "latest"
Cameron Simpson <cs at zip.com.au> DoD#743
It's better, when you're riding with someone you don't know so well, to stick
to the inside line - it's easier to avoid the bits...
- Barry Sheene
More information about the Python-list